Summing Contents of a 2 Dimensional Array

Hey guys, I'm completely lost on what to do next, I'm writing a program that takes numbers from 4 separate columns in two rows and adding those numbers at the bottom. It should look something like this...with "x" being the sum of the numbers in the columns. (Dashes are cosmetically for the forum to align right, not in actual program)

25 -------- 14 -------- 18 --------- 12
15 -------- 19 -------- 48 --------- 58
22 -------- 36 -------- 15 --------- 88
22 -------- 25 -------- 18 --------- 08
------------------------------------------------
84----------94----------99----------167


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 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
}
Last edited on
Your text and example data does not match your code.

In your code it looks like you want to calculate:
* a sum for each row
* a sum for each column
* a sum of all array elements [1]

[1] a sum of all elements equals to the sum of row-sums and also to the sum of column-sums


Your functions print_array, total_cols, and total_rows can modify the array. They should not. Store sums elsewhere. Actually, you don't need to store them at all, if it is enough to print them.

Given array
x y z
h k l

you can print the values of the first row (x,y,z) and their sum x+y+z
you can repeat the operation for the second row (h,k,l) and (h+k+l)
you could repeat for more rows in same manner

Now you would have first part printed. (Let say that w==x+y+z and m==h+k+l.)
x y z w
h k l m

Then you can iterate over columns one more time, but now calculate the sum of the element of each column and print it. At the same time you can accumulate a (total)sum of the values that you print on this last row. Once you have printed the three sums, you have the "over all elements" sum ready to print.
closed account (E0p9LyTq)
http://www.cplusplus.com/forum/beginner/94118/
Topic archived. No new replies allowed.