Write a file-oriented C program that will process the student exam scores given
ID: 3841543 • Letter: W
Question
Write a file-oriented C program that will process the student exam scores given in Prob. 6.69(k). Read the data from the data file created in the previous problem. Then create a report containing the name, exam scores and average grade for each student. A class of students earned the following grades for the six examinations taken in a C programming course. Write an interactive C program that will accept each student's name and exam grades as input, determine an average grade for each student, and then display the student's name, the individual exam grades and the calculated average.Explanation / Answer
First Program (Each student data is received interactively from the user)
#include<stdio.h>
struct student {
char name[50];
int grades[6];
float avg;
struct student *next;
}
void main(){
char choice;
struct student *head, *p;
int sum;
head = null;
struct student data;
do {
printf("Enter student name grades(y/n) ");
scanf("%c", &choice);
if (choice == 'y') {
scanf("%s %d %d %d %d %d %d", data.name, &data.grades[0], &data.grades[1], data.grades[2], data.grades[3]
data.grades[4], data.grades[5]);
if (head == null){
head = &data;
head->next = NULL;
}
else {
p = head;
while (p->next != NULL)
p = p->next;
p->next = (struct student *)malloc(sizeof(struct student));
p = p->next;
*p = data;
p->next = NULL;
}
}
} while (choice != 'n');
p = head;
printf("Name ExamScores1-1 Average ");
while (p != NULL) {
sum = 0;
printf("%s %s",p->name, " ");
for (int i = 0; i<6; i++){
sum = sum + p->grades[i];
printf("%d %s",p->grades[i], " ");
}
printf("%lf ", sum/6 ),
}
}
Second Program (Students data is read from the file. Assuming data is written in the same format in the file as shown in the question)
void main(){
FILE fp;
struct student *head, *p;
head = null;
int sum;
struct student data;
fp = fopen("Input.txt", "r");
if (fp == NULL){
printf("File opening error ");
}
while (fscanf(fp, "%s %d %d %d %d %d %d", data.name, &data.grades[0], &data.grades[1], data.grades[2], data.grades[3]
data.grades[4], data.grades[5]) != EOF) {
if (head == null){
head = &data;
head->next = NULL;
}
else {
p = head;
while (p->next != NULL)
p = p->next;
p->next = (struct student *)malloc(sizeof(struct student));
p = p->next;
*p = data;
p->next = NULL;
}
}
}
p = head;
printf("Name ExamScores1-1 Average ");
while (p != NULL) {
sum = 0;
printf("%s %s",p->name, " ");
for (int i = 0; i<6; i++){
sum = sum + p->grades[i];
printf("%d %s",p->grades[i], " ");
}
printf("%lf ", sum/6 ),
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.