/*
* Name: Jim Santana
* Collaborator(s):
* Instructor: Dr. Gary Watson
* Date: 2/27/2004
* Assignment:Program 6 -- Working With Arrays
* Description:This program will read some grade data from a file into an array.
* Then calculate some statistical values and display them.
*/
#include
#include
#include
#define MAX_GRADES 200
int readGrades(int grades[]); /*This function opens the file, and reads the grades into the array, grades[].
/*It returns the number of grades that were read. If the file cannot be opened
/*or there are too many grades in the file, print an appropriate message to the screen and exit.*/
double mean( int grades[], int num); /*This function takes as input, num, the number of grades in the array, and grades,
/*a pointer to the array in main. It finds the mean or average value of these grades and returns this value.*/
double stdDev(int grades[], int num); /*This function takes as input, num, the number of grades in the array, and grades, a pointer to the array
/*in main. It finds the standard deviation of the grades in the array and returns this value.*/
double median(int grades[], int num); /*This function takes as input, num, the number of grades in the array, and grades, a pointer to the array
/*in main. It finds the median grade in the array and returns this value. The simplist way of finding the
/*median is to sort the grades by value. Then you can find the index for the median grade by using the
/*definition for median above (it will be the middle index if there are an odd number of grades, or you
/*will have to average the two middle grades if there are an even number). You can use the bubble sort
/*function from lecture to do the sorting.*/
void bubble(int grades[], int num);
void swap(int *p, int *q);
/*mean = (g1 + g2 + ... + gN)/N where N is the number of grades*/
/*standard deviation = sqrt( (g12 +g22 +... + gN2)/N - mean*mean) */
/*median - there is no equation for median. It it the value such
that there are an equal number of values larger and smaller.
For an odd number of values, if they are sorted, it is value in
the middle of the list. If there are an even number of values,
it is the mean of the middle two grades.*/
int main(void)
{
int grades[MAX_GRADES];
int num;
num=readGrades(grades);/* get all the grades */
printf("cs151 winter 2004 midterm grade statistics.\n\n");
printf("Number of grades in file = %d\n",num);
printf("The mean value is: %0.1f\n", mean(grades,num));
printf("Standard Deviation = %0.1f\n", stdDev(grades,num));
printf("Median grade = %d\n",median( grades, num));
return 0;
}
int readGrades(int grades[])
{
int num=0;
FILE *fp;
fp=fopen("gradedata.txt","r");
if(fp==NULL )
{
printf("No file called gradedata.txt exists.");
exit(1);
}
while(fscanf(fp,"%d",&grades[num++])!=EOF)/* read each grade */
if(num>MAX_GRADES)
{
printf("Too many grades in the grade list file!");
exit(1);
}
fclose(fp);
return --num;
}
double mean(int grades[],int num)
{
/*mean = (g1 + g2 + ... + gN)/N where N is the number of grades*/
double sum=0;
int i;
for(i=0; i