Hey guys. I put together this program that is suppose to count up and average the grades of a class of 5 people. In it, I have
'A'=4,'B'=3, and so on. I wrote a function that I thought would average them out, but it keeps coming up with 0.00000. just like that. Can anyone see something that I am doing wrong. possibly way wrong? here is the code in the .h file:
Code:#include <stdio.h> #define CLASS_SIZE 100 struct student{ char *last_name; int student_id; char grade; };
in the .c file:
any help is greatly appreciated.Code:#include "cl_info.h" void average(struct student class[]); int main(void) { struct student temp, class[CLASS_SIZE]; temp.grade = 'A'; temp.last_name = "Bushker"; temp.student_id = 590017; temp.grade = 'B'; temp.last_name = "Donald"; temp.student_id = 590117; temp.grade = 'C'; temp.last_name = "Trump"; temp.student_id = 590217; temp.grade = 'A'; temp.last_name = "Lewis"; temp.student_id = 590317; temp.grade = 'B'; temp.last_name = "Smith"; temp.student_id = 590417; average(class); return 0; } void average(struct student class[]) { double sum=0.0,avg; int i; for(i=0;i<5;++i){ if(class[i].grade == 'A') sum+=4; if(class[i].grade == 'B') sum+=3; if(class[i].grade == 'C') sum+=2; if(class[i].grade == 'D') sum+=1; if(class[i].grade == 'F') sum+=0; } avg = (sum/5); printf("The average for the class is %f.\n", avg); }



LinkBack URL
About LinkBacks


