I'm trying to calculate a student's GPA in C programming language with the code below but it's not returning the correct corresponding grade based on each score, feels like it just picks a random letter.
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
float gpa_ICT(int a){
float point[a], unit[a], score[a];
char C_code[12][6][a];
char grade[a];
//Read Course code, score and unit
for(int x = 0; x < a; x++){
printf("Enter Course Code: ");
scanf("%6s", &C_code[x]);
printf("Enter course score: ");
scanf("%f", &score[x]);
printf("Enter course unit: ");
scanf("%f", &unit[x]);
//Assign values to Course point and Grade based on Score.
if(score[x] >= 70){
grade[x]= "A";
point[x] = 5;
}
else if (score[x] >= 60 && score[x] <=69){
grade[x]= "B";
point[x] = 4;
}
else if (score[x] >= 50 && score[x] <=59){
grade[x]= "C";
point[x] = 3;
}
else if (score[x] >= 45 && score[x] <=49){
grade[x]= "D";
point[x] = 2;
}
else if (score[x] <=44){
grade[x]= "F";
point[x] = 0;
}
else{
printf("ERROR!");
}
}
//Check score for each course
//Return the Course Code and grade based on score
for(int i = 0; i < a; i++){
printf("%s : %c\n",C_code[i] ,grade[i]);
}
//Multiply each point by corresponding course unit and sum them up
float sum = 0;
float Total;
for(int y = 0; y < a; y++){
Total = point[y]*unit[y];
sum = sum + Total;
}
//Calculate the total Course unit
float totalUnit = 0;
for(int z = 0; z < a; z++){
totalUnit = totalUnit + unit[z];
}
//Calculate the GPA
float GPA = sum / totalUnit;
return GPA;
}
int main()
{
int course;
printf("How many courses? ");
scanf("%d", &course);
float result = gpa_ICT(course);
printf("Your GPA is %0.2f", result);
return 0;
}
Here is the output How many courses? 3
Enter Course Code: MTS202
Enter course score: 75
Enter course unit: 3
Enter Course Code: ICT202
Enter course score: 58
Enter course unit: 2
Enter Course Code: MEE202
Enter course score: 65
Enter course unit: 2
MTS202 : D
ICT202 : H
MEE202 : F
Your GPA is 4.14
Process returned 0 (0x0) execution time : 32.212 s Press any key to continue.**