Clearly I'm not doing this right, what am I missing?
Code:/* printing the average grade for every student in every class. printing out the average grade per class; printing out the average grade per student over all classes. Your college administration wants this task accomplished by a program which allocates memory for the array to hold the student grades in the classes. Then, each printing must be accomplished by a separate function with the array or portions thereof passed as parameters. */ #include <stdio.h> #include <stdlib.h> #define NUMBER_OF_STUDENTS 11 //globals for array variables #define NUMBER_OF_CLASSES 4 #define NUMBER_OF_GRADES 5 float gradeAve(int ***c); void freeArray(int ***a); int main() { int i, j, student, class, grade; int*** array; array=malloc(NUMBER_OF_STUDENTS*sizeof(int**)); for (i=0; i<NUMBER_OF_STUDENTS; i++) { array[i]=malloc(NUMBER_OF_CLASSES*sizeof(int*)); for (j=0;i<NUMBER_OF_CLASSES;j++) { array[i][j]=malloc(NUMBER_OF_GRADES*sizeof(int));} } for(student=0; student<NUMBER_OF_STUDENTS; student++) {//for loop for populating 3 dimensional array for(class=0;class<NUMBER_OF_CLASSES;class++) { for(grade=0; grade<NUMBER_OF_GRADES; grade++) { if(grade==0){ printf("Enter in the grade for student %d in subject %d:\n", student+1, class+1); scanf("%d", &array[student][class][grade]); }else scanf("%d",&array[student][class][grade]); } } } printf("Average of all Grades: %.2f\n", gradeAve(array)); freeArray(array); } float gradeAve(int ***c) { float sum=0; int grade;//local variables for(grade=0; grade<NUMBER_OF_GRADES; grade++) { sum+=c[NUMBER_OF_STUDENTS][NUMBER_OF_CLASSES][NUMBER_OF_GRADES];//only takes sum of the grades and divides by number of grades } return (sum/NUMBER_OF_GRADES); } void freeArray(int ***a) { int i, j; for (j=0; j< NUMBER_OF_CLASSES; ++j) free(a[j]); for (i = 0; i < NUMBER_OF_GRADES; ++i) { free(a[j][i]); } free(a); }



5Likes
LinkBack URL
About LinkBacks



