Say I have a multidimensional array, I populated it with for's. Now if I just want the colummn and depth parts to be passed to the function is that possible?
Printable View
Say I have a multidimensional array, I populated it with for's. Now if I just want the colummn and depth parts to be passed to the function is that possible?
You can pass parts of an array to a function - what you normally would do is either pass one element/value of the array to the function, or pass one pointer to say, the top 100 elements of the function.
You're only passing a pointer, in any case. All the data is not copied over. C assumes that you will be modifying the array's values.
See I tried using pointers but it gives me an error when compiling
> See I tried using pointers but it gives me an error when compiling
See?
We can't see any of your code, so we can't say what you got wrong, or how to fix it.
I'm not writing any code it's more of a question for learning purposes.
So if I do say int array[][4][5];
and I want to just take 4 and 5 I would make a prototype
int Ave(array[j][k]);
then in int main declare j and k as 4 and 5?
Quote:
See I tried using pointers but it gives me an error when compiling
... ?Quote:
I'm not writing any code it's more of a question for learning purposes.
...I was writing code before, but went another route. I'm just trying to figure out how I would go about it correctly via pseudo example
Ok so for example.
Code:#include <stdio.h>
float ave(float stu[][3][4]);
float sAve(float stu[j][k]);
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("%d", &stu[i][j][k]);
}else
scanf("%d", &stu[i][j][k]);
}
}
}
printf("All Averages: %f", ave(stu));
}
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);
}
float sAve(float stu[j][k])
{
float sum=0;
int i, j;
for(i=0; i<2; i++){
for(j=0;j<3;j++){
sum+=stu[i][j];
}
}
return (sum/12);
}
J and K undeclared not in a function. How do I point correctly?
Well the way I like to visualize a 3-d array is like a cube. So, think of it like a matrix on one side that represents one students grades, and the depth vector represents all of your students. This way you can slice the cube, in a manner of speaking, and get the grades of each student.
A sane way to build such a data structure is:
This way isn't insanely popular, but you have to remember what all the dimensions mean. So to pass in one student, you need a matrix of floats; to pass in the whole class you need a cube, etc. To actually print, it might be easier to code a function that prints one students courses, and then loops over the whole class, calling the function.Code:typedef float grades[2];
typedef grades courses[3];
typedef courses students[4];
students class_room; // definition
Yeah I know how a 3 dimensional array is however not supposed to do it that way
It is the same thing. You just have an easier time remembering what each dimension means.
Plus, I didn't quite get what you were asking. The code you were complaining about here was in a separate post:
This is about the most incompetent way to ask this question.
The errors are (as indicated):Code:#include <stdio.h>
float ave(float stu[][3][4]);
float sAve(float stu[j][k]);
...
float sAve(float stu[j][k])
{
float sum=0;
int i, j;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
sum+=stu[i][j];
}
}
return (sum/12);
}
You get these errors because you are specifying the dimensions of a multi-dimensional array. This is restricted to constant integral expressions (i.e. numbers), a variable name here will not pass the compiler.Code:-------------- Build: Debug in foo ---------------
mingw32-gcc.exe -Wall -ansi -g -pedantic -w -Wextra -Wall -g -std=gnu99 -D_UNICODE -DUNICODE -c C:\Users\Josh2\Documents\foo\main.c -o obj\Debug\main.o
C:\Users\Josh2\Documents\foo\main.c:3: error: 'j' undeclared here (not in a function)
C:\Users\Josh2\Documents\foo\main.c:3: error: 'k' undeclared here (not in a function)
C:\Users\Josh2\Documents\foo\main.c:48: error: 'j' undeclared here (not in a function)
C:\Users\Josh2\Documents\foo\main.c:48: error: 'k' undeclared here (not in a function)
Process terminated with status 1 (0 minutes, 0 seconds)
4 errors, 0 warnings
Next time, I expect you to provide the actual error text, or I will just ignore you.
So how would I do it correctly?
Here is an example of sending a pointer to a 2D array to a subroutine
Code:#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#define MAX_S 10
void print_s(char (*arr2D_ptr)[][MAX_S]);
int main (void)
{
char arr2D[][MAX_S] = {
"string 1",
"string 2"
};
print_s(&arr2D);
return EXIT_SUCCESS;
}
void print_s(char (*arr2D_ptr)[][MAX_S])
{
puts((*arr2D_ptr)[0]);
puts((*arr2D_ptr)[1]);
}
That doesn't help me much because my issue isn't passing the array, it's when I have a 3 dimensional array I only want to pass the 2nd and 3rd dimension to the function
If I do
Code:#include <stdio.h>
float ave(float stu[][3][4]);
float 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("%d", &stu[i][j][k]);
}else
scanf("%d", &stu[i][j][k]);
}
}
}
printf("All Averages: %f", ave(stu));
printf("Student Averages: %f", sAve(stu));
}
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);
}
float sAve(float stu[][4])
{
float sum=0, l=3, m=4;
int i, j;
for(i=0; i<2; i++){
for(j=0;j<3;j++){
sum+=stu[i][j];
}
}
return (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:7: note: expected 'float (*)[4]' but argument is of type 'float (*)[3]
[4]'
Really...
It does if you think about it...
Code:#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#define MAX_S 10
void print_s(char (*arr2D_ptr)[][MAX_S]);
int main (void)
{
char arr2D[2][2][MAX_S] = {
{
"string 1",
"string 2"
} ,
{
"string 3",
"string 4"
}
};
print_s(&arr2D[0]);
print_s(&arr2D[1]);
return EXIT_SUCCESS;
}
void print_s(char (*arr2D_ptr)[][MAX_S])
{
puts((*arr2D_ptr)[0]);
puts((*arr2D_ptr)[1]);
}
1. Pass the whole thing to the function and only use the second and third dimension inside the function.
2. Create a temporary two dimensional array, copy the desired elements from the real array, and pass the temporary array to the function.
I think the best that you can do is.
float sAve(float stu[3][4]);
sAve(stu[x]); where x is a number of your choice (0 or 1).
I'm honestly confused, I thought I could just call 2 parts of the array to the function
I'd be tempted to make a subroutine like this:
Code:
void get_y_z_slice( int const_x_val, /* The value of x for the yz plain*/
char (*input)[MAX_X][MAX_Y][MAX_Z], /* The input 3D array */
char (*output)[MAX_Y][MAX_Z]); /* Where the resulting plain is stored */
Yes I could use two arrays, but that's not the point of doing this
Everything in my notes says this should work the way it is without having to use 2 arrays
Quote:
Originally Posted by Sornix
What is the point of doing this? What are you trying to achieve?Quote:
Originally Posted by Sornix
I showed you how to send pointers to arrays into a function/subroutine correctly.Quote:
Originally Posted by Sornix
I'm sorry, but I just can't gauge what you are after...
I thought I made that clear my mistake. The point is to use 1 multi dimensional array, then to call that array to functions to compute different averages
so I want averages of stu[2][3][4] stu[3][4] and stu[4]
scanf's with %d need to be float (%f)
main didn't return 0 at the end
Variables m and l and unused
You can NOT pass a multidimensional array to a function/subroutine -> You must use a pointer.
I got your code working changing these things.
And by pointer to the arrays, I mean this:
Code:float ave(float (*stu)[][3][4]);
float sAve(float (*stu)[][4]);
I get the same exact error...
Post your new code
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
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));
}
The excercise is to call only the [3][4] elements into the function lol
So you want to calculate the class average, not student?
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.
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
}
I'll have to look into this more on the weekend have another problem I have to finish sadly
Perhaps it might help to look at it a different way.
Assume you have STUDENTS students, COURSES courses, and PARTS parts to each course, with each part graded:
The order of the dimensions determines what you can overall refer to.Code:#define STUDENTS 20
#define COURSES 5
#define PARTS 3
For example, if you define
then grade[student] is a two-dimensional array, double[COURSES][PARTS], specifying the grades for one student over all courses and parts, and grade[student][course] is an one-dimensional array, double[PARTS], specifying the grades for each part for one student on one course.Code:double grade[STUDENTS][COURSES][PARTS];
If, however, you define
then grade[course] is a two-dimensional array, double[PARTS][STUDENTS], specifying the grades for all students and all parts of that course, and grade[course][part] is an one-dimensional array, double[STUDENTS], specifying the grades for a specific course and part for all students.Code:double grade[COURSES][PARTS][STUDENTS];
So, the way you define your array indices, define the ways you can use your arrays in C.
For general-purpose one-, two-, or three-dimensional arrays, I would use
The functions to handle this are straightforward, but require detailed knowledge about structures and pointers.Code:struct data {
struct data *next;
long refcount;
size_t size;
double data[];
};
struct array3d {
long step[3];
long size[3];
double *origin;
struct data *owner;
};
struct array2d {
long step[2];
long size[2];
double *origin;
struct data *owner;
};
struct array1d {
long step;
long size;
double *origin;
struct data *owner;
};
#define ELEMENT3D(array, x, y, z) \
((array)->origin[(x)*(array)->step[0] + (y)*(array)->step[1] + (z)*(array)->step[2]])
#define ELEMENT2D(array, x, y) \
((array)->origin[(x)*(array)->step[0] + (y)*(array)->step[1]])
#define ELEMENT1D(array, x) \
((array)->origin[(x)*(array)->step])
The point of the structures is that actual data is stored in the first structure, and the others just reference the data. (Counting the number of references to each instance of the first structure allows the program/library to free the data when it is no longer needed, as long as the programmer remembers to free each array when no longer needed.)
(I use long for the size and step fields, because int is not large enough on 64-bit architectures.)
Given a 3D array, a 2D array can be any regular plane within the array. (Regular in the sense that the coordinates for the elements must be specified with a linear integer equation.)
Given a 3D or 2D array, an 1D array can be any regular line within the array. (Again, regular in the sense that the coordinates for the elements must be specified with an linear integer equation.)
In other words, you can mirror, flip, and/or pick every N'th element from another array (having equal or greater number of dimensions).
(It is also possible to expand certain 1D arrays to 2D and 3D, and 2D arrays to 3D, if the same data is repeated. For example, you can create a 3D array with any number of elements, with all the elements always having the exact same value, since they all share the same data.)
There is a very small additional cost to every element access, because all coordinates are multiplied to get the actual address, but it should be neglible in practice. In fact, Fortran does something very, very similar internally, to support arrays and array slicing.
Once again, the voice of reason: Nominal animal. Can you give a feash prospect to this conundrum?
I'm not sure if asking me is a good idea, Click_here. I seem to have a knack of raising hackles, even if I'm sincirely just trying to help.
Let's start at the beginning, the assignment:
First, define the constants and the grade array.Quote:
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
2 subjects, 3 students, 4 exams
The grade per student per subject is then average(grade[student][subject]), where the parameter is of type double[EXAMS].Code:#define STUDENTS 3
#define SUBJECTS 2
#define EXAMS 4
double grade[STUDENTS][SUBJECTS][EXAMS];
The grade per student is then average(grade[student]), where the parameter is of type double[SUBJECTS][EXAMS].
The average grade is average(grade).
However, you need to output the intermediate averages too.
So, declare separate arrays for the averages, and an averaging function that takes an one-dimensional array as a parameter:
(I'm sure most of the readers have now realized my point.)Code:double subject_grades[STUDENTS][SUBJECTS];
double student_grades[STUDENTS];
double average_grade;
double average(const double value[], const size_t values[]);
To populate the subject_grades array, you do a double loop, and calculate the average over the exams in that subject for that student.
Instead of calculating the student grades (student_grades array) from the grades array, you can calculate the averages of averages, i.e. averaging the subject_grades.
Finally, instead of calculating an average over the entire array to get the average grade overall, you average over the student_grades array.
I'll do the unthinkable and show the actual solution, because it really is this trivial:
If you wanted the average grade over all students for each subject, you cannot use the same function: you just cannot slice the existing arrays that way. This is the point I was trying to make earlier: the order of indices determines how you can slice the array.Code:int student, subject;
for (student = 0; student < STUDENTS; student++) {
/* Average grade per subject for this student: */
for (subject = 0; subject < SUBJECTS; subject++)
subject_grades[student][subject] = average(grades[student][subject], EXAMS);
/* Average grade for this student overall: */
student_grades[student] = average(subject_grades[student], SUBJECTS);
}
/* Average grade over all students (and subjects): */
average_grade = average(student_grades, STUDENTS);
Of course, calculating the average grade of all students per subject would be easy to do using two for loops:
If you look at the indexing, you'll realize that the inner loop accesses nonconsecutive members in the subject_gradesarray. Again, that means you cannot "slice" them to a function, the way we did earlier for the other arrays.Code:double average_subject[SUBJECTS];
for (subject = 0; subject < SUBJECTS; subject++) {
average_subject[subject] = 0.0;
for (student = 0; student < STUDENTS; student++)
average_subject[subject] += subject_grades[student][subject];
average_subject[subject] /= (double)STUDENTS;
}
I sincirely hope this clears the question. If not, please be gentle with your rebukes.