Please help! 2-d arrays!

Hello. I'm a bit stuck on a code for class. The assignment asks for you to create an int array of random numbers between 50 and 100 using 10 rows and 3 columns. It also asks you to output the average of each column. My code does everything except outputs my averages as a 10th of each number next to it. Anyone guide me?

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
  int main()
{
	//declare/inititialize variables
	srand (time(NULL));
	int numArr[10][3];
	double sum;
	double average;
	//
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			double sum = 0;
			double average = 0;
			numArr[i][j] = rand() % 50 + 50;
			cout << numArr[i][j] << " ";
			sum += numArr[i][j];
			average = sum/10;
			cout << average << endl;
		}
		
	
		cout << "\n";
		
	}
	
	return 0;
}
Last edited on
Moving some lines of code and adding an array to hold the sums of every column will change the logic:
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
int main()
{
	//declare/inititialize variables
	srand (time(NULL));
	int numArr[10][3];
	double sum[3]; //sum array that holds a sum for each column
	//
	sum[0] = 0;
	sum[1] = 0;
	sum[2] = 0;
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			numArr[i][j] = rand() % 50 + 50;
			cout << numArr[i][j] << " ";
			sum[j] += numArr[i][j];
		}
		cout << "\n\n";
	}
	for (int j = 0; j < 3; j++)
	{
		cout << "average of column " << j << ": " << sum[j] / 10.0 << "\n";
	}
	
	return 0;
}
Topic archived. No new replies allowed.