this is a c program. i have to calculate gpa.Grade point average is calculated by multiplying the number of credits earned for a course by the point equivalent of the letter grade for that course. That product is computed for each course and summed together as the total number of points earned. That total is then divided by the number of total credits attempted to give you the GPA.
Below is a example of how GPA is calculated.
Chemistry B 4: 3 points for grade x 4 credits = 12
Math A 3: 4 points for grade x 3 credits = 12
Gym C 1: 2 points for grade x 1 credit = 2
=====================================
This is a total of 26points. The 26 points is then divided by total number of credits to get GPA.
GPA = 26/8 = 3.25
i am getting wrong answer for totalpoints and gpa.
totalpoint should be =49
gpa should be=3.2
this is the scores i am using
Ariel Lewis
Biology A 4.0
Statistics C 4.0
History A 3.0
Tennis A 1.0
English B 3.0
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Local Declarations
FILE* spIn;
float credits; // number of credits
float gpa; // calculate GPA
char name; // Student name
char grade; // letter grade earned
char course[25]; // subject
char string[60];
float count=0;
float totalPoints = 0;
float totalCredits=0;
void main()
{
// Statements
spIn = fopen("grades.txt", "r");
if (!spIn)
{
printf("Could not open file\a\n");
exit (101);
} // if open fail
fgets(string, 60, spIn ); // read in name
printf("%s\n", string );
printf(" Ariel Lewis Grade Point Average Calculator\n\n");
printf(" Course Name Grade Earned Number of Credits Earned\n\n");
while(fscanf(spIn, "%s %c %f \n",course, &grade, &credits )!=EOF) // read in name, course name, grade, credits.
printf("%15s %3c %12.1f \n\n",course, grade, credits );
fclose(spIn);
/* close the file after reading in data */
/* calculate gradepoint average of student */
for(int i = 0; i <= 4; i++)
{
if (grade == 'A'){ // check if grade is= to A
count ++; // then point earned = 4
totalPoints += 4.0;
}
else if (grade == 'B') { // check if grade is= to B
count++; // then point earned = 3
totalPoints += 3.0;
}
else if (grade == 'C'){ // check if grade is= to C
count++; // then point earned = 2
totalPoints += 2.0;
}
else if (grade == 'D'){ // check if grade is= to D
count++; // then point earned = 1
totalPoints += 1.0;
}
else if (grade == 'F'){ // check if grade is= to F
count++; // then point earned = 0
totalPoints += 0.0;
}
}
printf("\ncredits: %.1f \n\n\n",credits);
totalCredits=credits*totalPoints;
gpa=totalCredits/totalPoints;
printf("----------------------------------------------------------------------\n");
printf("\nThe Total Credits earned by this student are: %.1f \n",totalCredits);
printf("\nThe GPA for this Student is: %.1f \n\n\n",gpa);
printf("----------------------------------------------------------------------\n");
system("pause");
return ;
} // main