Summing Rows of 2 Dimensional Array

Hey guys, I'm completely lost on what to do next, I'm writing a program that takes numbers, and sorts them into columns, then calculate the sum of the 4 separate columns. (Bottom part)
Now that part, I have figured out, what I'm having trouble with is that I also want to take those numbers, again, and calculate the sum of the rows (top part).
This is what I'm having trouble with I just can't seem to find a solution that works for me. (Dashes are cosmetically for the forum to align right, not in actual program)

25 -------- 14 -------- 18 --------- 12 - 69
15 -------- 19 -------- 48 --------- 58 - 140
22 -------- 36 -------- 15 --------- 88 - 161
22 -------- 25 -------- 18 --------- 08 - 73




24 -------- 13 -------- 17 --------- 15
15 -------- 19 -------- 48 --------- 58
22 -------- 36 -------- 15 --------- 88
22 -------- 25 -------- 18 --------- 08
------------------------------------------------
83----------93----------98----------169


I've got the array set up right I believe I just can't figure out how to sum all the numbers in the separate rows without getting a goofy answer.

Thanks for any help you have!


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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

int const row = 4, col = 4;
void print_array(double[][col]);
void total_cols(double[][col]);
void total_rows(double[][col]);

int main()
{
	
	double num_arr[row][col]={{4.2,5.1,6.8},{5.5,4.7,8.2},{2.7,3.9,6.5}};

	print_array(num_arr);

	cout << endl;

	total_cols(num_arr);
	print_array(num_arr);
}

void print_array(double n[][col])
{
	for(int i=0;i<row-1;i++)
	{
		for(int j=0;j<col;j++)
			cout << setw(8) << n[i][j];
		cout << endl;
	}

	for(int k=0;k<col;k++)
		cout << setw(8) << "-------";
	cout << endl;

	for(int j=0;j<col;j++)
		cout << setw(8) << n[row-1][j];
	cout << endl;

}

void total_cols(double n[][col])
{
	for(int j=0;j<col;j++)
		for(int i=0;i<row-1;i++)
		  n[row-1][j]+=n[i][j];

}

void total_rows(double n[][col])
{
// Having Problems here
}
Try this...
1
2
3
4
5
6
7
8

void total_rows(double n[][col])
{
    for(int i=0;i<row ;i++)
		for(int j=0;j<col-1;j++)
		  n[i][col - 1]+=n[i][j];
}
I tried that before I published this on the foru and it just didn't calculate the rows at all. I dont know why its doing this! :/
No way!
I have tried it and it has given me these results ...


4.2-----5.1----6.8----0
5.5-----4.7----8.2----0
2.7-----3.9----6.5----0
--------------------------------------
0-------0------0------0



4.2----5.1----6.8----16.1
5.5----4.7----8.2----18.4
2.7----3.9----6.5----13.1
--------------------------------------
12.4---13.7---21.5---47.6

Last edited on
Really?! It isn't working on my end. Mine shows exactly this...

4.2-----5.1----6.8----0
5.5-----4.7----8.2----0
2.7-----3.9----6.5----0
--------------------------------------
0-------0------0------0



4.2----5.1----6.8----0
5.5----4.7----8.2----0
2.7----3.9----6.5----0
--------------------------------------
12.4---13.7---21.5---0



I don't understand why mine is acting differently!!! Ahhh! Do u have any ideas?
This is the code that has given me the results above ...
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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

int const row = 4, col = 4;
void print_array(double[][col]);
void total_cols(double[][col]);
void total_rows(double[][col]);

int main()
{
	
	double num_arr[row][col]={{4.2,5.1,6.8},{5.5,4.7,8.2},{2.7,3.9,6.5}};

	print_array(num_arr);

	cout << endl;

	total_cols(num_arr);
	total_rows(num_arr);
	print_array(num_arr);
}

void print_array(double n[][col])
{
	for(int i=0;i<row-1;i++)
	{
		for(int j=0;j<col;j++)
			cout << setw(8) << n[i][j];
		cout << endl;
	}

	for(int k=0;k<col;k++)
		cout << setw(8) << "-------";
	cout << endl;

	for(int j=0;j<col;j++)
		cout << setw(8) << n[row-1][j];
	cout << endl;

}

void total_cols(double n[][col])
{
	for(int j=0;j<col;j++)
		for(int i=0;i<row-1;i++)
		  n[row-1][j]+=n[i][j];

}

void total_rows(double n[][col])
{
    for(int i=0;i<row ;i++)
	for(int j=0;j<col-1;j++)
	    n[i][col - 1]+=n[i][j];
}
Last edited on
Copy and pasted into a new program and it works perfectly...Maybe my file was corrupt! No idea! Lol thank you for your help Obscure!!!:)
No problem..!
I think that you have forgotten to call (total_rows) after (total_cols)...
It's better to know what is wrong to avoid it next time ..

Ah yes you're right! I don't know how I missed that one...Thanks for helping!
Topic archived. No new replies allowed.