Finding the sum of the third row in 2D arrays

Apr 29, 2014 at 9:32pm
Okay, so I am developing a program that does many things. One of the is finding the sum of the third row in an array.

Here is my program

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
#include <iostream>
#include <string>

using namespace std;

void changeNum (int qArray2D [5][5]);
void printNum (int qArray2D [5][5]);
void calcNum (int qArray2D [5][5], int sum);

int main (){
	int qArray2D [5][5] = {
		{0.0, 0.1, 0.2, 0.3, 0.4},
		{1.0, 1.1, 1.2, 1.3, 1.4},
		{2.0, 2.1, 2.2, 2.3, 2.4,},
		{3.0, 3.1, 3.2, 3.3, 3.4},
		{4.0, 4.1, 4.2, 4.3, 4.4}
	};
	system ("pause");
	return 0;
}
void changeNum (int qArray2D [5][5]){
	qArray2D [2][1] = 8.5;
}
void printNum (int qArray2D [5][5]){
	for (int i = 0; i < 5; i++){
		for (int j = 0; j < 5; j++){
			cout << qArray2D [i][j] << endl;
		}
	}
}
void calcNum (int qArray2D [5][5], int sum){
	int sum = 0;
	for (int a = 0; a < 5; a++)
		sum = 


As you can see, I am clearly stuck. Can someone please help me?
Apr 29, 2014 at 9:48pm
Please someone help me. I need to know what to do
Apr 29, 2014 at 9:55pm
How does one access the first element of the third row?
How does one access the second element of the third row?
...
Apr 29, 2014 at 9:58pm
closed account (j3Rz8vqX)
Someone mentioned accumulators?
http://www.cplusplus.com/forum/beginner/130722/
Apr 29, 2014 at 10:04pm
@KesKiverto would it be qArray2D [0][1]? I am sorry. I am just learning about arrays and my teacher gives us a programming assignment like we have been learning all about it. I tried asking her questions but she said she will take off 5 points for each question so I am just 2 seconds away to just sending her this. I am a little fustrated
Apr 29, 2014 at 10:15pm
closed account (j3Rz8vqX)
Your array has two indexes.

The first, on the left, being the rows.
The second, on the right, being the columns.

myArray[?][?]

You want to call your arrays value at

total = myArray[third row: counting from 0 to 5, means 2][all of the elements at that row: 0 to 5(exclusive): sounds like "a"]
Apr 29, 2014 at 10:22pm
so basically it is qArray2D [0, 1, 2, 3, 4][2.0, 2.1, 2.2, 2.3, 2.4]? I'm confused
Apr 29, 2014 at 10:27pm
Apr 29, 2014 at 10:31pm
closed account (j3Rz8vqX)
Hint:
This is the first value in your array at row 3 column 1: (index 2,0)

myArray[2][0];
Apr 29, 2014 at 11:26pm
first of all, change the int to float because you are dealing with decimals..
After use this code below to find the sum;

float sum;
sum =0;
for (int a = 0; i < 5; i++)
{
for (int b = 2; j < 3; j++)
sum = sum + a [a][b];
}
cout <<sum<< endl;

Is my answer helpful
Apr 30, 2014 at 12:18am
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
#include <iostream>
#include <string>

using namespace std;

int main (){
	float qArray2D [5][5] = {
		{0.0, 0.1, 0.2, 0.3, 0.4},
		{1.0, 1.1, 1.2, 1.3, 1.4},
		{2.0, 2.1, 2.2, 2.3, 2.4,},
		{3.0, 3.1, 3.2, 3.3, 3.4},
		{4.0, 4.1, 4.2, 4.3, 4.4}
	};
	//step 4
	qArray2D [2][1] = 8.5;
	//step 5
	for (int i = 0; i < 5; i++){
		for (int j = 0; j < 5; j++){
			cout << qArray2D [i][j];
		}
	}
	float sum = 0;
	for (int a = 0; a < 5; a++){
		for (int b = 0; b < 2; b++){
		sum = sum + qArray2D [a][b];
		cout << sum;
	}
}
	qArray2D [4][0] = sum;
	system ("pause");
	return 0;
}


So I changed it and it doesn't do the table thing so I still have some work to go.
@DPut: thanks for the help :)
@Keskiverto thanks for the link. Still confused but it helped a little :)
@Alfredokang thank you for the help as well :)
Now all I have to do is figure out how to do the table thing and what not v.v
Apr 30, 2014 at 1:37am
closed account (j3Rz8vqX)
This will give you the sum of the whole table:
1
2
3
4
5
	for (int a = 0; a < 5; a++){
		for (int b = 0; b < 2; b++){
		sum = sum + qArray2D [a][b];
		cout << sum;
	}


I believe what you wanted was the sum of the third row:
1
2
3
4
5
6
7
	//for (int a = 0; a < 5; a++){
		for (int b = 0; b < 2; b++)
                {
		    sum = sum + qArray2D [2][b];
		    cout << sum;
                }//You were missing this prior O_O
	//} 

Not sure why you are inserting the sum into index[4][0] of the array, but if you want to see the value, you could do this:
 
cout<<"The sum: "<<sum<<endl;
Apr 30, 2014 at 1:48am
I have to change a specific row to the sum ^^ Okay I changed it >.<

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
float qArray2D [5][5] = {
		{0.0, 0.1, 0.2, 0.3, 0.4},
		{1.0, 1.1, 1.2, 1.3, 1.4},
		{2.0, 2.1, 2.2, 2.3, 2.4,},
		{3.0, 3.1, 3.2, 3.3, 3.4},
		{4.0, 4.1, 4.2, 4.3, 4.4}
	};
	//step 4
	qArray2D [2][1] = 8.5;
	//step 5
	for (int i = 0; i < 5; i++){
		for (int j = 0; j < 5; j++){
			cout << qArray2D [i][j];
		}
		cout << endl;
	}
	//step 6
	float sum = 0;
	for (int a = 0; a < 5; a++){
		for (int b = 0; b < 2; b++){
		sum = sum + qArray2D [2][b];
		cout << sum;
	}
		cout << endl;
}
	//step 7
	qArray2D [4][0] = sum;
	//step 8
	for (int a = 0; a < 5; a++){
		for (int b = 0; b < 5; b++){
			cout << "Enter a number:" << endl;
			cin >> qArray2D [a][b];
		}
	}
Apr 30, 2014 at 1:50am
closed account (j3Rz8vqX)
Yeah, make sure to comment out line 19.
Apr 30, 2014 at 3:15am
While that works, I think you are going about it the hard way.

Each 'row' of a 2D array is itself a 1D array. So if you have a function that can sum the elements of a 1D array, you can use it on any row of your 2D array.

1
2
3
4
5
6
7
8
9
float sum( float a[], int size )
{
  ...
}

int main()
{
  float xys[ 5 ][ 5 ] = { ... };
  cout << "Sum of row 2 = " << sum( xys[ 2 ], 5 ) << "\n";

Hope this helps.
Topic archived. No new replies allowed.