Post your new code
Post your new code
Fact - Beethoven wrote his first symphony in C
Code:/* The program includes functions for: Printing an average of all grades Finding average grade for each subject for each student Finding the average grade for each student in both subjects You should now rewrite the code to work with 2(subjects)-3(students)-4(exams) array and do the same things as described above and in the attached code.*/ #include <stdio.h> float ave(float (*stu)[][3][4]); void sAve(float (*stu)[][4]); int main() { int i, j, k; float stu[2][3][4]; for(i=0; i<2; i++){ for(j=0;j<3;j++){ for(k=0; k<4; k++){ if(k==0){ printf("Enter in the grade for student %d in subject %d", j+1, k+1); scanf("%f", &stu[i][j][k]); }else scanf("%f", &stu[i][j][k]); } } } printf("All Averages: %f", ave(*stu)); sAve(*stu); return 0; } float ave(float (*stu)[][3][4]) { float sum=0; int i, j, k; for(i=0; i<2; i++){ for(j=0;j<3;j++){ for(k=0; k<4; k++){ sum+=(*stu)[i][j][k]; } } } return (sum/24); } void sAve(float (*stu)[][4]) { float sum=0; int i, j; for(i=0; i<3; i++){ for(j=0;j<3;j++){ sum+=(*stu)[i][j]; } } printf("The Student Average is: %f", sum/12); }
A few pointers
1) Fix your indentation.
2) Rename of i to subject, j to student and k to grade -> There is a few obvious mistakes that can be solved.
3) "ave(*stu)" is wrong -> It should be "ave(&stu)" - The "address" of the array.
4) You haven't changed %d to %f
Fact - Beethoven wrote his first symphony in C
the only %d that is in there refers to an integer it's correct
Code:/* The program includes functions for: Printing an average of all grades Finding average grade for each subject for each student Finding the average grade for each student in both subjects You should now rewrite the code to work with 2(subjects)-3(students)-4(exams) array and do the same things as described above and in the attached code.*/ #include <stdio.h> float ave(float (*stu)[][3][4]); void sAve(float (*stu)[][4]); int main() { int i, j, k; float stu[2][3][4]; for(i=0; i<2; i++){ for(j=0;j<3;j++){ for(k=0; k<4; k++){ if(k==0){ printf("Enter in the grade for student %d in subject %d", j+1, k+1); scanf("%f", &stu[i][j][k]); }else scanf("%f", &stu[i][j][k]); } } } printf("All Averages: %f", ave(&stu)); sAve(&stu); return 0; } float ave(float (*stu)[][3][4]) { float sum=0; int i, j, k; for(i=0; i<2; i++){ for(j=0;j<3;j++){ for(k=0; k<4; k++){ sum+=(*stu)[i][j][k]; } } } return (sum/24); } void sAve(float (*stu)[][4]) { float sum=0; int i, j; for(i=0; i<3; i++){ for(j=0;j<3;j++){ sum+=(*stu)[i][j]; } } printf("The Student Average is: %f", sum/12); }
C:\Users\j\Desktop\>gcc Lab8a.c -o Lab8a
Lab8a.c: In function 'main':
Lab8a.c:29:1: warning: passing argument 1 of 'sAve' from incompatible pointer ty
pe [enabled by default]
Lab8a.c:9:6: note: expected 'float (*)[][4]' but argument is of type 'float (*)[
2][3][4]'
You did fix it, sorry.
You haven't fixed your indentation
You haven't renamed your variables i,j and k
The error comes from the way you are calling sAve -> You are putting a pointer to a 3D array into a pointer to a 2D.
I changed your function sAve, because it wasn't doing what you thought it was doing
Code:#define NUMBER_OF_SUBJECTS 2 #define NUMBER_OF_STUDENTS 3 #define NUMBER_OF_GRADES 3 ... float sAve(float (*stu)[NUMBER_OF_SUBJECTS][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES], int student) { float sum=0; int subject, grade; if (student >= NUMBER_OF_STUDENTS) { printf("\nError: No student %d found", student + 1); return 0.0; } for(subject=0; subject<NUMBER_OF_SUBJECTS; subject++) { for(grade=0; grade<NUMBER_OF_GRADES; grade++) { sum+=(*stu)[subject][student][grade]; } } return (sum/ (NUMBER_OF_SUBJECTS * NUMBER_OF_GRADES)); }
Last edited by Click_here; 11-15-2012 at 06:59 PM.
Fact - Beethoven wrote his first symphony in C
The excercise is to call only the [3][4] elements into the function lol
So you want to calculate the class average, not student?
Fact - Beethoven wrote his first symphony in C
Now say I want to find the individual subject for each student
Alright this is where I'm at, I think it's correct
Code:/* The program includes functions for: Printing an average of all grades Finding average grade for each subject for each student Finding the average grade for each student in both subjects You should now rewrite the code to work with 2(subjects)-3(students)-4(exams) array and do the same things as described above and in the attached code.*/ #include <stdio.h> #define NUMBER_OF_SUBJECTS 2 #define NUMBER_OF_STUDENTS 3 #define NUMBER_OF_GRADES 4 float ave(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES]); float sAve(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES], int student); float gAve(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES]); int main() { int i, j, k, student=0, subject; float stu[2][3][4]; for(i=0; i<2; i++){ for(j=0;j<3;j++){ for(k=0; k<4; k++){ if(k==0){ printf("Enter in the grade for student %d in subject %d:\n", j+1, k+1); scanf("%f", &stu[i][j][k]); }else scanf("%f", &stu[i][j][k]); } } } for(student=0; student<3; student++){ printf("Subject Averages for student %d %.2f\n", student+1, sAve(&stu, student)); } printf("Average of all Grades: %.2f", ave(&stu)); for(student=0; student<3; student++){ for(subject=0; subject<NUMBER_OF_SUBJECTS; subject++){ printf("The Average of Student %d in Subject %d: %f", student+1, subject+1, gAve(&stu)); } } return 0; } float ave(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES]) { float sum=0; int i, j, k; for(i=0; i<2; i++){ for(j=0;j<3;j++){ for(k=0; k<4; k++){ sum+=(*stu)[i][j][k]; } } } return (sum/(2*3*4)); } float sAve(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES],int student) { float sum=0; int subject, grade; for(subject=0; subject<NUMBER_OF_SUBJECTS; subject++){ for(grade=0; grade<NUMBER_OF_GRADES; grade++) { sum+=(*stu)[subject][student][grade]; } } return (sum/(NUMBER_OF_GRADES * NUMBER_OF_SUBJECTS)); } float gAve(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES]) { float sum=0; int grade; for(grade=0; grade<NUMBER_OF_GRADES; grade++) { sum+=(*stu)[NUMBER_OF_SUBJECTS][NUMBER_OF_STUDENTS][grade]; } return (sum/NUMBER_OF_GRADES); }
Thing that bugs me is this is not how I'm supposed to do it, I could have done this, I guess I'll have to ask about the way they actually wanted to do it. I'm only supposed to be calling 2 of the 3 elements and etc.
Trust me on this one: Change the names of i, j and k in your main function to subject, student and grade -> You'll see it straight away!
Your indentation is still extremely bad.
Also,
Only use the defines for the numbers 2, 3 and 4 -> That way you can change your code very quickly and easily.
You gAve function is a good attempt, but the reason it won't work is because NUMBER_OF_SUBJECTS/NUMBER_OF_STUDENTS are defined as 2 and 3. I'd make them parameters for the function, like student in sAve.
Fact - Beethoven wrote his first symphony in C
Code:/* The program includes functions for: Printing an average of all grades Finding average grade for each subject for each student Finding the average grade for each student in both subjects You should now rewrite the code to work with 2(subjects)-3(students)-4(exams) array and do the same things as described above and in the attached code.*/ #include <stdio.h> #define NUMBER_OF_SUBJECTS 2 //globals for array variables #define NUMBER_OF_STUDENTS 3 #define NUMBER_OF_GRADES 4 float ave(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES]); float sAve(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES], int student); float gAve(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES], int subject, int grade);//function prototypes int main() { int subject, student, grade; float stu[NUMBER_OF_SUBJECTS][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES];//variable declarations for(subject=0; subject<2; subject++){//for loop for populating 3 dimensional array for(student=0;student<3;student++){ for(grade=0; grade<4; grade++){ if(grade==0){ printf("Enter in the grade for student %d in subject %d:\n", student+1, subject+1); scanf("%f", &stu[subject][student][grade]); }else scanf("%f", &stu[subject][student][grade]); } } } for(student=0; student<3; student++){ //for loop to print off Subject averages by looping through students with a function call printf("Subject Averages for student %d %.2f\n", student+1, sAve(&stu, student)); } printf("Average of all Grades: %.2f\n", ave(&stu)); //Print out all averages by function call for(student=0; student<NUMBER_OF_STUDENTS; student++){//nested for loop to print off student averages per subject as loops through for(subject=0; subject<NUMBER_OF_SUBJECTS; subject++){ printf("The Average of Student %d in Subject %d: %.2f\n", student+1, subject+1, gAve(&stu, subject, student)); } } return 0; } float ave(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES]) //function definition { float sum=0; int i, j, k;//local variables for(i=0; i<2; i++){//for loop to add all the values together for(j=0;j<3;j++){ for(k=0; k<4; k++){ sum+=(*stu)[i][j][k]; } } } return (sum/(2*3*4));//print out the sum / number of subjects, students, exams } float sAve(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES],int student)//function definition { float sum=0; int subject, grade;//local variables for(subject=0; subject<NUMBER_OF_SUBJECTS; subject++){ for(grade=0; grade<NUMBER_OF_GRADES; grade++)//for loop only loops through subject and grade { sum+=(*stu)[subject][student][grade]; } } return (sum/(NUMBER_OF_GRADES * NUMBER_OF_SUBJECTS));//returns sum } float gAve(float (*stu)[][NUMBER_OF_STUDENTS][NUMBER_OF_GRADES], int subject, int student)//function definition for grade average { float sum=0; int grade;//local variables for(grade=0; grade<NUMBER_OF_GRADES; grade++) { sum+=(*stu)[subject][student][grade];//only takes sum of the grades and divides by number of grades } return (sum/NUMBER_OF_GRADES);//returns value }