Write a C program roster.c that enters a class roster from a file roster.dat. Th
ID: 3864826 • Letter: W
Question
Write a C program roster.c that enters a class roster from a file roster.dat. The class roster contains records for some (not known in advance) number of students. The record for each student consists of the last name, first name, and some scores for homeworks separated by spaces. The scores are integer numbers in the range [1..99].
Example:
Blue Larry 78 93 67 95
Brown Smith 87 56 90 49
Green Erin 90 78 64 87
Silver Eric 86 70 74 87
Orange John 96 89 75 85
Yellow Sarah 99 67 98 67
Your program must read the records and compute the average score for each student rounded off to the nearest integer and output these averages. the above example the output should be:
Blue Larry 78 93 67 95 83
Brown Smith 87 56 90 49 71
Green Erin 90 78 64 87 80
Silver Eric 86 70 74 87 79
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main() {
int k;
double average;
FILE *fp = fopen("roster.dat", "r");
char firstName[50], lastName[50];
int score1,score2,score3,score4;
while(!feof(fp))
{
fscanf(fp,"%s",firstName);
fscanf(fp,"%s",lastName);
fscanf(fp,"%d",&score1);
fscanf(fp,"%d",&score2);
fscanf(fp,"%d",&score3);
fscanf(fp,"%d",&score4);
average = (score1 + score2 + score3 + score4)/(double)4;
printf("%s %s %d %d %d %d %lf ", firstName, lastName,score1,score2,score3,score4, round(average) );
}
fclose(fp);
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Blue Larry 78 93 67 95 83.000000
Brown Smith 87 56 90 49 71.000000
Green Erin 90 78 64 87 80.000000
Silver Eric 86 70 74 87 79.000000
Orange John 96 89 75 85 86.000000
Yellow Sarah 99 67 98 67 83.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.