Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment you are to write a program that reads a set of scores (where

ID: 3636925 • Letter: I

Question

In this assignment you are to write a program that reads a set of scores (where each
score is a floating point number between 0 and 100, and the last score is a negative
number which should not be counted in the results) stored in the file
“/ece/www/ism2008/hw3/scores.dat” and prints the following on the screen:
1. the number of scores read,
2. the average of all scores (as a floating point number), and
3. the highest score among all scores.

Output Example:
Your output should be somewhat like:

Number of scores read: 66
The average score is 88.888888
The highest score is 100.000000

Explanation / Answer

#include int main() { int counter; float grade, sum; float average, highest; FILE *fPtr; /*file pointer will be pointing to scores.dat */ counter = 0; highest = 0; sum = 0; if ((fPtr = fopen("/ece/www/ism2008/hw3/scores.dat","r")) == NULL) { printf("File could not be opened "); } else { while( 1 ) /* keep reading scores until the file ends */ { fscanf(fPtr, "%f", &grade); /* keep reading grade from file until -1 was read */ if(grade < 0) { break; /* stop the while loop when read -1 from the file */ } counter = counter +1; /* after read one grade, increment counter */ sum = sum + grade; /* adds up all grades to get the total of all */ if(grade > highest) { highest = grade; /* compare and update the highest score */ } } /* after finish reading all scores, calculate average, print out info */ average = sum / counter; printf("Number of scores read: %d ", counter); printf("The average score is %f ", average); printf("The highest score is %f ", highest); fclose(fPtr); } return 0; }