Average of all the elements in two dimensional array

Hi! I am having problems in averaging all the elements in my two dimensional array. The first array size is stated but the second is user defined. For example

int Temp[5][User defined]

that's my array...so how do I average all the elements in the array?

Thanks!
Hi try:

1
2
3
4
5
//...


int Avrg = Total_Of_Array / (5 + User_Defined);


I didn't do the loops you would use to add the values of the array together but I can if you want help with that.

EDIT: I see where in your post you asked for the loops. I am writting it now.
Last edited on
That would be a solution, but might easily overflow.

1
2
3
4
5
6
7
8
9
10
11
...
int Temp[5][h];
...
int average=0;

for (int y=0;y<h;y++) {
for (int x=0;x<5;x++) {
average+=Temp[x][y];
}
}
average/=5*h


Edit: Oh, loaded the page before Computergeeks answer, he was faster ;)
Last edited on
JoR posted what I would have so I won't follow up but enjoy.
Thanks! so I did that then called it. let's say the name of the function is:

1
2
3
4
double calAvg(int Temp[5][h])
...

printf("The average is %f.", calAvg(Temp));

and I am getting weird results, it's definitely not the average cos I calculated it... what do you think is wrong???

Last edited on
Topic archived. No new replies allowed.