2. 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);
}```

3. A few pointers

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

4. 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]'

5. You did fix it, sorry.

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

...

{
float sum=0;

if (student >= NUMBER_OF_STUDENTS)
{
printf("\nError: No student %d found", student + 1);
return 0.0;
}

for(subject=0; subject<NUMBER_OF_SUBJECTS; subject++)
{
{
}
}
}```

6. The excercise is to call only the [3][4] elements into the function lol

7. So you want to calculate the class average, not student?

8. Now say I want to find the individual subject for each student

9. Originally Posted by Sorinx
Now say I want to find the individual subject for each student
Look at the code in post #35 and think...

10. 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

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 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 sum=0;

for(subject=0; subject<NUMBER_OF_SUBJECTS; subject++){
{
}
}

}

{
float sum=0;

{
}

}```

11. 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.

12. 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!

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.

13. Originally Posted by Sorinx
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.
I'd like to say for the record, I did show you how to send a 2D array from a 3D array as a parameter in post #17. You didn't seem very interested though.

14. 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

int main()
{

for(subject=0; subject<2; subject++){//for loop for populating 3 dimensional array
for(student=0;student<3;student++){
printf("Enter in the grade for student %d in subject %d:\n", student+1, subject+1);
}else
}
}
}

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 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 sum=0;

for(subject=0; subject<NUMBER_OF_SUBJECTS; subject++){
{
}
}

}

{
float sum=0;