### Need help averaging a 2d array

Hey guys, I found some code online in order to average a 2d array but I can't seem to find the problem. What I am wanting to do is create a program that has random temperatures for every hour of the day and every day of the week, then I want to take those temperatures and get an average for any day I chose. Any help is greatly appreciated. Thanks!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` `````` /* Author: Brandon Duncan Date: 08/28/2013 Purpose: Create an array that randomly generates a temperature for every hour of a week. */ #include #include #include #include #include using namespace std; int avg(int avg0,int avg1, int avg2, int avg3, int avg4, int avg5, int avg6) { int temp[7][24]; int a,total=0; for(a=0;a<7;a++) total = total + temp[0][a]; avg0 = (int) total/4; return avg0; for(a=0;a<7;a++) total = total + temp[1][a]; avg1 = (int) total/4; return avg1; for(a=0;a<7;a++) total = total + temp[2][a]; avg2 = (int) total/4; return avg2; for(a=0;a<7;a++) total = total + temp[3][a]; avg3 = (int) total/4; return avg3; for(a=0;a<7;a++) total = total + temp[4][a]; avg4 = (int) total/4; return avg4; for(a=0;a<7;a++) total = total + temp[5][a]; avg5 = (int) total/4; return avg5; for(a=0;a<7;a++) total = total + temp[6][a]; avg6 = (int) total/4; return avg6; } int main() { int temp[7][24]; int average0,average1,average2,average3,average4,average5,average6; srand((unsigned)time(NULL)); // This section is how the array is randomly generated. for (int i = 1; i < 7; i++) { for (int j = 1; j < 24; j++) { temp[i][j] = 20+ rand() % 75; } } avg(average0,average1,average2,average3,average4,average5,average6); cout << "The average Noon tempature for this week is: " << average0 << endl; system ("PAUSE"); return 0; }``````
Last edited on
Are you familiar with nested for loops?

 ``123456789101112`` ``````const int rows = 3, columns = 4; double average(int array[][columns]) { double sum = 0; for (int i = 0; i < rows; i++) for (int j = 0; j < columns; j++) sum += array[i][j]; return sum / (rows * columns); }``````
Somewhat, its been awhile since I have programmed and school just started up again. The thing is if I use those loops will it still randomly generate a temperature?
Doesn't really help with your question, but I'm just pointing out:

http://www.cplusplus.com/forum/articles/17108/
There is no reason you cannot both use nested loops and generate random temperatures. In fact you don't even need arrays, you can generate the random temperatures as you need them for the calculations ;)
(unless, of course, the user needs to be able to get the same result twice from the same day)
Last edited on
Well unfortunately it is required by my professor to use arrays for this particular program. Thanks
I never said you could not use arrays, just mentioned it was possible not to ;)
@L B
You're starting to worry me, you've been winking at people a lot lately. Do you need a hug?
Most of my statements seem harsh without smiley text and I don't want people to think I'm trying to be mean to them ;p
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````/* Author: Brandon Duncan Date: 08/28/2013 Purpose: Create an array that randomly generates a temperature for every hour of a week. */ #include #include #include #include #include using namespace std; const int rows = 7, columns = 24; double average(int array[][columns]) { double sum = 0.0; for (int o = 0; o < rows; o++) for (int p = 0; p < columns; p++) sum += array[o][p]; return sum / (rows * columns); } int main() { int temp[7][24]; srand((unsigned)time(NULL)); // This section is how the array is randomly generated. for (int i = 1; i < 7; i++) { for (int j = 1; j < 24; j++) { temp[i][j] = 20+ rand() % 75; } } cout << "The average Noon temperature for this week is: " << average << endl; system ("PAUSE"); return 0; }``````

Okay what am I doing wrong here anyone? The answer I keep getting is random (for ex: 011911F9) Any help is truly appreciated!!!
Anyone? I am struggling here =\
You are printing the memory address of the function. To call average(), you must include the parentheses and any arguments needed.
 `` `` ``cout << average(temp) << endl;``

There's a huge bug in your program: in main, you're setting both indexes, i and j, at 1 instead of 0.

Edit: moreover, a two dimensional array is unecessary. Better design would've led you to the following solution:

 ``12345678910111213141516171819`` ``````#include #include using namespace std; int main() { srand((unsigned int)time(NULL)); int days_per_week = 7, hours_per_day = 24; int hours_per_week = days_per_week * hours_per_day; double sum = 0; for (int i = 0; i < hours_per_week; i++) sum += 20 + rand() % 75; cout << sum / hours_per_week; return 0; }``````
Last edited on
Topic archived. No new replies allowed.