I am trying to complete an example in my book and I dont want to go any further until I complete it. It wants me write a function that prints the average for each student in a class and prints the class average. Let an A grade have Value 4, a B grade have value 3, and so forth. I have been pulling my hair out and I got t to print out the class average but I am lost as to how to get the student average. Here is the code:
in the .h file
can someone lead me out of the darkness. It will print the class average now all i have to do is get it to print out the student and his average with a=4, b=3, c=2, d=1Code:#include <stdio.h> #define CLASS_SIZE 100 struct student{ char *last_name; int student_id; char grade; }; in the .c file #include "class_info.h" #include <stdio.h> void average(struct student class[], int length); int main(void) { struct student class[class_size]; class[0].grade = 'A'; class[0].last_name = "Walker"; class[0].student_id = 590017; class[1].grade = 'B'; class[1].last_name = "Smith"; class[1].student_id = 590118; class[2].grade = 'C'; class[2].last_name = "jones"; class[2].student_id = 590219; class[3].grade = 'A'; class[3].last_name = "Bubba"; class[3].student_id = 590320; class[4].grade = 'B'; class[4].last_name = "johns"; class[4].student_id = 590421; class[5].grade = 'C'; class[5].last_name = "Walker"; class[5].student_id = 590017; class[6].grade = 'D'; class[6].last_name = "Smith"; class[6].student_id = 590118; class[7].grade = 'B'; class[7].last_name = "jones"; class[7].student_id = 590219; class[8].grade = 'A'; class[8].last_name = "Bubba"; class[8].student_id = 590320; class[9].grade = 'C'; class[9].last_name = "johns"; class[9].student_id = 590421; average( class, 10 ); return 0; } void average(struct student class[], int length) { double sum=0.0,avg; int i; // int length = sizeof(class)/sizeof(student); for( i=0;i<length;++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/length); printf("The average for the class is %f.\n", avg) }



LinkBack URL
About LinkBacks




