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

Objective: Keep practicing file reading, while loops and if/else conditions. For

ID: 3636927 • Letter: O

Question

Objective: Keep practicing file reading, while loops and if/else conditions.

For each requirement, before righting code, think how a program can solve the problem,
for example (not necessarily related to this homework) :
? to calculate the highest score you cannot “see which one is highest”, you need to
compare each score with the highest, if the new one is higher ? highest=score
? to count the number of lines you cannot “see how many lines are in the file”, you need
to use a variable as a counter, and increment it every time you read a new valid score

Instruction:
In this assignment you are to write a program that reads student IDs and scores from a file
whose format is as following:

2369
1188
4786
6846
1695

70
40
97
70
98

94
95
62
94
14

?
?
?
?

Each line starts with the student ID of a student, followed by 5 grades.
The student ID is a 4 digit number greater than 1000.
The number of lines is unknown
Consider the grades to be floating numbers

The file is in “/ece/www/ism2008/hw4/scoresHw4b.dat”

For each student:
calculate and print the average grade;
/*THINK! Print average for each student...inside or outside a loop? */

Produce as output:
- SID and average score of each student
- SID and average score of the student who has the highest average grade.
If multiple students have the same highest grade outputting one is
sufficient.

87
90
43
87
37

93
59
47
93
46

17
6
92
17
82

Explanation / Answer

#include #define NGRADES 5 int main() { int grade=0; int studID; int studIDmax, studID2nd, studID3rd; int i=0; float stud_sum=0; float stud_avg=0; float max_avg=0; float snd_avg=0; float trd_avg=0; FILE *fPtr; /*file pointer will be pointing to scores.dat */ if ((fPtr = fopen("/ece/www/ism2008/hw4/scoresHw4b.dat","r"))== NULL) { printf("File could not be opened "); return 0; } while(fscanf(fPtr, "%d", &studID)!=EOF) { while( i++trd_avg) //it was considered correct also the solution in which { //the highest 3 distint averages where found if(stud_avg>snd_avg) //This solution finds the 3 students with highest avg, { //even if they are the same if(stud_avg>max_avg) { trd_avg=snd_avg; //update scores snd_avg=max_avg; max_avg=stud_avg; studID3rd=studID2nd; studID2nd=studIDmax; studIDmax=studID; //update students } else //higher than 2nd, but not than first { trd_avg=snd_avg; //update scores snd_avg=stud_avg; studID3rd=studID2nd; studID2nd=studID; } } else //higher than 3rd, but not than 2nd { trd_avg=stud_avg; studID3rd=studID; } } stud_avg=0; //reset for next student stud_sum=0; } fclose(fPtr); printf("Student %d has the highest average: %f out of 100 ", studIDmax, max_avg); printf("Student %d has the 2nd highest average: %f out of 100 ", studID2nd, snd_avg); printf("Student %d has the 3rd highest average: %f out of 100 ", studID3rd, trd_avg); return 0; }