Write a program to compute the aritmetic mean (average), median, and mode for up to 50 test scores. The data are contained in a text file. The program will also print a histogram of the scores.
The program should start with a function to read the data file and fill the array. Note that there may be fewer than 50 scores. This will require that the read function return the index for the last element in the array.
To determine the average. To determine the median, you mst first sort the array. The median is the score at last / 2 if last is an even inde and by averaging the scores at the floor and ceiling of last / 2 if last is odd. The mode is the score that occurs the most often. It can be determined as a byprodcut of building which score occurred most often. (Note that two scores can occur the same number of times.)

This is what I came up using Bloodshed Dev-C++:

``````#include <stdio.h>
#include <stdlib.h>

double average (int ary[ ]);
void printAverage (int data[ ], int size, int lineSize);

int main (void)
{
double avg;
int base[50] = {30, 70, 20, 40, 50, 10, 90, 80, 40, 100,
90, 30, 60, 80, 20, 90, 100, 50, 30, 20,
70, 10, 50, 70, 90, 20, 10, 90, 50, 80,
40, 90, 20, 20, 30, 40, 60, 50, 70, 90,
100, 20, 30, 40, 50, 60, 70, 80, 90, 90};

avg = average(base);
printAverage (nums, size, 10);

system ("pause");
return 0;
}

double average (int ary[ ])
{
int sum = 0;
for (int base = 0; base < 50; base++)
sum += ary[base];

return (sum / 5.0);
}

void printAverage (int data[], int size, int lineSize)
{
int numPrinted = 0;

printf("\n\n");
for (int i = 0; i < size; i++)
{
numPrinted++;
printf("%2d ", data[i]);
if (numPrinted >= lineSize)
{
printf("\n");
numPrinted = 0;
}
}
printf("\n\n");
return;
}``````

Any advice or tip will help out. Thank you

## All 8 Replies

Welcome jusmeehh.

Your source code must be surrounded by BB code tags. You must have to How to use BB Code tags?

You should post your question in C forum.

>>Any advice or tip will help out. Thank you

Your program contains hard-coded data, but the requiremenets state it must read the data from a data file. That's the first thing you need to do -- get that working first then continue on with the next item in the requirements statement: sort the data.

What is that "size" you are passing?????? It is a local variable and it is not defined. And by the way average is sum/50.0 and not 5.0.

commented: correct +10

As AD said, your program contains a lot of magic numbers which is not a good thing to do.
Do not use system("pause") it makes your program consume heavy resources. ( and it makes your program non-portable)

Generally as a rule of thumb: you should always pass the size of an array to the function in which you are passing the array itself as there is no way you can determine the size of the array by just its name (inside a function).

0 And by the way average is sum/50.0 and not 5.0.

Its actually sum/(number of elements). If the array is full then it will be 50, otherwise it will be however many numbers are read from the file. So sum/5 might be correct if there are only 5 numbers.

And that average function is wrong too -- you need to pass it the number of items read from the file which may be less than 50.

commented: more correct +10

average =sum/howmany why dont you after you read the array davide the array directly by i instead of just writing up the number yourself will make it more compact

I said that because he defined marks beforehand and didn't take it as input.

thanks guys for the tips.. im still working on it.. its looking better than before

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.