I’m trying to complete a lab in which I have to calculate the total Grade Point Average (GPA) from course information given in a linked list of structures. I’m trying to define each letter grade with its appropriate grade point (‘A’ = 4.0, “A-” = 3.7 …). The course grades are stored in arrays of chars. I’m able to use the #define derivative to define the letter grades A,B,C,D,E, but I am having trouble defining the +/- grades. Is using the #define derivative the proper way to achieve this task? and if so, would someone be able to show me the proper syntax.
/* Definition of a data node holding course information */
struct course {
int term;
char name[15];
char abbrev[20];
float hours;
char grade [4];
char type[12];
struct course *next;
};
float gpa ( struct course *ptr )
{
float totalhours;
float gpa;
float gradepoints;
while (ptr != NULL )
{
totalhours += (ptr->hours);
gradepoints = (ptr->hours * ptr->grade);
}
gpa = (gradepoints / totalhours);
}
What you are looking for is a map, or a dictionary, which is not natively supported in C. You can implement a simple map for your use case as an array of
structs as such:Then loop over this array inside your for loop (fixing a few more bugs):