The grade distribution of a certain course is according to the following table:
ID: 3777703 • Letter: T
Question
The grade distribution of a certain course is according to the following table:
Evaluation Method
Weight
Lab
25%
Coursework
15%
Midterm Exam
30%
Final Exam
30%
Each line in a text-file scores.txt file contains five values: ID#, lab grade out of 100, coursework grade out of 100, midterm exam grade out of 100, and the final exam grade out of 100. If the letter grade a student gets is according to:
Grade range
Letter Grade
[85 , 100]
A
[70, 85)
B
[60, 70)
C
[40, 60)
D
[0, 40)
F
write a C program that reads scores.txt and writes to each line of a text-file results.txt the ID of a student, his total score out of 100 and his letter grade.
Finding the letter grade should be implemented by writing a function that receives the score as double and returns the letter grade as char.
Note: Your program must be general, it must work for any number of lines in the input file.
Sample input file scores.txt:
201100010 95.0 80.0 75.0 80.0
201100020 45.0 50.0 30.0 50.0
201100030 97.0 90.0 90.0 95.0
201100040 60.0 80.0 75.0 75.0
201100050 62.0 60.0 70.0 60.0
Sample output file results.txt:
ID SCORE GRADE
201100010 82.3 B
201100020 42.8 D
201100030 93.3 A
201100040 72.0 B
201100050 63.5 C
Evaluation Method
Weight
Lab
25%
Coursework
15%
Midterm Exam
30%
Final Exam
30%
Explanation / Answer
#include <stdio.h>
int main(void) {
FILE *fp;
fp = fopen("scores.txt", "r");
fs = fopen("results.txt","w+")
while(fscanf(fp, "%d%d%d%d%d", &id,&lab,&course,&mid,&final))
{
double score = 0.25*(lab/100)+0.15*(course/100)+0.003*(mid+final);
char grade = calculateGrade(score);
fprintf(fs, "%d%f%c",id,score,grade);
}
fclose(fp);
fclose(fs);
return 0;
}
char calculateGrade(double score) {
if(score>=0.85 && score<=1) return 'A';
else if(score<0.85 && score>=0.70) return 'B';
else if(score<0.70 && score>=0.60) return 'C';
else if(score<0.6 && score>=0.40) return 'D';
else return 'F';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.