[So I have to change a 3 dimensional array around, and I am almost there, but not quite! So my problem is as follows:
Problem 1
We tackled this problem in class: keeping track of 3 students and their 4 exam grades for each of
their 2 subjects. Hence, it uses a 3(students)-2(subjects)-4(exams) array. The code for the
following is attached.
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.
Here is the original code:
Code:
#include <stdio.h>
#define MaxP 3
#define MaxR 2
#define MaxC 4
float ave(float points[3][2][4]);
float subj(float points[4]);
float stud(float points[2][4]);
void main(void)
{
int i, j;
float grades[3][2][4]={{{3, 4, 5, 6},
{1, 2, 3, 4}},
{{2, 4, 5, 7},
{1, 3, 5, 7}},
{{4, 6, 8, 9},
{2, 4, 6, 8}}};
printf("The total average of all students/subjects/exams is %.2f\n",ave(grades));
for(i=0;i<MaxP;i++)
for(j=0;j<MaxR;j++)
printf("The average for student %d in subject %d is %.2f\n", i, j, subj(grades[i][j]));
for(i=0;i<MaxP;i++)
printf("The average for student %d is %.2f\n", i, stud(grades[i]));
}
float ave(float points[MaxP][MaxR][MaxC])
{
int i,j,k;
float total=0;
for(i=0;i<MaxP;i++)
for(j=0;j<MaxR;j++)
for(k=0;k<MaxC;k++)
total+=points[i][j][k];
return total/(MaxP*MaxR*MaxC);
}
float subj(float points[MaxC])
{
int i;
float total=0;
for(i=0;i<MaxC;i++)
total+=points[i];
return total/MaxC;
}
float stud(float points[MaxR][MaxC])
{
int i,j;
float total=0;
for(i=0;i<MaxR;i++)
for(j=0;j<MaxC;j++)
total+=points[i][j];
return total/(MaxR*MaxC);
}
Here is what I have so far:
Code:
#include <stdio.h>
//MaxP and MaxR values are switched
#define MaxP 2
#define MaxR 3
#define MaxC 4
//Dimensions 1 and 2 are flip flopped
float ave(float points[2][3][4]);
float subj(float points[4]);
float stud(float points[3][4]);
void main(void)
{
int i, j;
float grades[2][3][4]={{{3, 4, 5, 6},{2, 4, 5, 7},{4, 6, 8, 9}},
{{1, 2, 3, 4},{1, 3, 5, 7},{2, 4, 6, 8}}};
printf("The total average of all students/subjects/exams is %.2f\n",ave(grades));
//Order of for loops is changed
for(i=0;i<MaxP;i++)
for(j=0;j<MaxR;j++)
printf("The average for student %d in subject %d is %.2f\n", i, j, subj(grades[i][j]));
for(i=0;i<MaxP;i++)
printf("The average for student %d is %.2f\n", i, stud(grades[i]));
}
float ave(float points[MaxP][MaxR][MaxC])
{
int i,j,k;
float total=0;
for(i=0;i<MaxP;i++)
for(j=0;j<MaxR;j++)
for(k=0;k<MaxC;k++)
total+=points[i][j][k];
return total/(MaxP*MaxR*MaxC);
}
float subj(float points[MaxC])
{
int i;
float total=0;
for(i=0;i<MaxC;i++)
total+=points[i];
return total/MaxC;
}
float stud(float points[MaxR][MaxC])
{
int i,j;
float total=0;
for(i=0;i<MaxR;i++)
for(j=0;j<MaxC;j++)
total+=points[i][j];
return total/(MaxR*MaxC);
}
What my program does is it prints out the following:
The total average of all students/subjects/exams is 4.54
The average for student 0 in subject 0 is 4.50
The average for student 0 in subject 1 is 2.50
The average for student 1 in subject 0 is 4.50
The average for student 1 in subject 1 is 4.00
The average for student 2 in subject 0 is 6.75
The average for student 2 in subject 1 is 5.00
The average for student 0 is 3.00
The average for student 1 is 2.17
The average for student 2 is 0.00
Everything is correct except for the last 3 lines. The average for student 0 should be 3.50, for student 1 4.25, and for student 2, 5.88
I've been trying to mess around with my stud function but I cannot seem to get it working correctly. If you guys could offer me any help/guidance it would be greatly appreciated. C is a very powerful language but can be very tedious at times. Thanks in advance