Grade Point Average Calculator Objective: Write a program that reads from a file
ID: 3678631 • Letter: G
Question
Grade Point Average Calculator Objective: Write a program that reads from a file a list of course names, letter grades for each course and number of credits each course is worth. The program will read each course name, grade earned and number of credits from the file and print out the course name, grade earned and number of credits for each course to the screen. After the program has read in all of the courses, grades earned and credits, the program should calculate the grade point average based on all of the courses read in by the program. Notes: Students who receive an "F” grade earn zero credits for the course but the grade is still used as part of the GPA calculation · When displaying the GPA, only display up to two decimal points . Sample Input File:A data file named grades.txt will be attached as part of the assignment. It will contain sample course names, grades earned and number of credits attempted. Your program should read this file in as input. Make sure you put this file in the same directory as your project. Below is a sample of the input file Jane Doe Chemistry A4.0 Calculus B4.0 HistoryC3.0 Gym English A 3.0 A 1.0Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedefstruct
{
char fname[30];
char minitial[1];
char lname[100];
int q1, q2, q3, q4, mid, final;
} Student;
int main()
{
const int STUDENTSMAX = 100;
Student students[STUDENTSMAX];
int i = 0;
char fname[30];
FILE *ifp, *ofp;
printf("Enter input .txt file name ");
scanf("%s",fname);
strcat(fname,".txt");
ifp = fopen(fname,"r");
ofp = fopen("output.txt", "w");
if(ifp == NULL)
{
printf("Error while opening the file. ");
exit(EXIT_FAILURE);
}
if (ofp == NULL)
{
printf("An error has been generated while attempting to open the output file");
fclose(ofp);
exit(1);
}
while(!feof(ifp))
{
fscanf(ifp, "%s, %s, %d, %d, %d, %d, %d, %d", students[i].fname, students[i].lname, &students[i].q1, &students[i].q2, &students[i].q3, &students[i].q4, &students[i].mid, &students[i].final);
printf("%s %s %d, %d, %d, %d, %d, %d ", students[i].fname,students[i].lname,students[i].q1,students[i].q2,students[i].q3,students[i].q4,students[i].mid,students[i].final);
}
printf(" ");
printf(" ");
fclose(ifp);
}
if (ofp == NULL)
{
printf("An error has been generated while attempting to open the output file");
fclose(ofp);
exit(1);
}
while(!feof(ifp))
{
fscanf(ifp, "%s, %s, %d, %d, %d, %d, %d, %d", students[i].fname, students[i].lname, &students[i].q1, &students[i].q2, &students[i].q3, &students[i].q4, &students[i].mid, &students[i].final);
printf("%s %s %d, %d, %d, %d, %d, %d ", students[i].fname,students[i].lname,students[i].q1,students[i].q2,students[i].q3,students[i].q4,students[i].mid,students[i].final);
}
printf(" ");
printf(" ");
fclose(ifp);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.