Need help averaging a 2d array

Aug 28, 2013 at 9:18pm
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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  /*  Author: Brandon Duncan
    Date: 08/28/2013
    Purpose: Create an array that randomly generates a temperature for every hour of a week.
*/
#include <iostream>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <cstdlib>
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 Aug 28, 2013 at 9:20pm
Aug 28, 2013 at 9:22pm
Are you familiar with nested for loops?
Aug 28, 2013 at 9:33pm
Dude, loops are your friends:

1
2
3
4
5
6
7
8
9
10
11
12
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);
}
Aug 28, 2013 at 9:59pm
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?
Aug 28, 2013 at 10:11pm
closed account (N36fSL3A)
Doesn't really help with your question, but I'm just pointing out:

http://www.cplusplus.com/forum/articles/17108/
Aug 29, 2013 at 12:31am
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 Aug 29, 2013 at 12:32am
Aug 29, 2013 at 3:07am
Well unfortunately it is required by my professor to use arrays for this particular program. Thanks
Aug 29, 2013 at 3:08am
I never said you could not use arrays, just mentioned it was possible not to ;)
Aug 29, 2013 at 3:47am
@L B
You're starting to worry me, you've been winking at people a lot lately. Do you need a hug?
Aug 29, 2013 at 4:35am
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
Sep 2, 2013 at 1:22am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*  Author: Brandon Duncan
    Date: 08/28/2013
    Purpose: Create an array that randomly generates a temperature for every hour of a week.
*/
#include <iostream>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <cstdlib>
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!!!
Sep 3, 2013 at 1:03am
Anyone? I am struggling here =\
Sep 3, 2013 at 1:47am
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;


Sep 3, 2013 at 3:13pm
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>

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 Sep 3, 2013 at 3:36pm
Topic archived. No new replies allowed.