2D ARRAYS

Hello everyone
I want to sum up rows in a 2D and put the sum at the end of the rows and also do the same for each of the colums. I have a code but the row sum for the first is the same sum showing up for the other rows; same thing is happening for the columns. I dont know where the error is; please help me check it out.Also note that i am only allowed to use functions.


#include <iostream>
#include <iomanip>
using namespace std;

const int NUM_ROWS = 3;
const int NUM_COLS = 5;
int sales[NUM_ROWS][NUM_COLS]={{88,97,79,86,94},
{86,91,78,99,82},
{84,78,81,77,93}};
int rowsum[NUM_ROWS];
int colsum[NUM_COLS];

int Rowsum(int arr[][NUM_COLS],int[]);

int Colsum(int arr[][NUM_COLS],int[]);

void transpose(int arr[][NUM_COLS],int);

int main()
{
cout<<"The transpose of the matrix is"<<endl;
transpose(sales,NUM_COLS);
cout<<endl;
rowsum[NUM_ROWS]= Rowsum(sales,rowsum);
for (int i = 0; i<NUM_ROWS;i++)
{
for(int j=0;j<NUM_COLS;j++)

cout<< setw(5)<<sales[i][j];

cout<<"|"<<setw(5)<<Rowsum(sales,rowsum)<<endl;

}
for (int j=0;j<NUM_COLS;j++)
cout<<"---"<<setw(5)<<Colsum(sales,colsum)<<endl;

return 0;
}

int Rowsum( int arr[][NUM_COLS], int rsum[])
{
for ( int row = 0; row < NUM_ROWS; row++)
{
int total = 0;

for ( int col = 0; col<NUM_COLS; col++)

total += arr[row][col];

return total;
}
}
int Colsum( int arr[][NUM_COLS], int csum[])
{
for (int col=0; col<NUM_COLS;col++)
{
int total=0;
for (int row = 0; row<NUM_ROWS; row++)
total += arr[row][col];

return total;
}
}
void transpose( int arr[][NUM_COLS],int size)
{
for (int row = 0; row<size; row++)
{
for (int cols = 0; cols<NUM_ROWS;cols++)
{
cout<<setw(5)<<arr[cols][row]<<" ";
}
cout<<endl;
}
}

--------------------Configuration: MATRIX SUM - Win32 Debug--------------------
Compiling...
MATRIX SUM.CPP
C:\Documents and Settings\DR.ABELEGA\MATRIX SUM.CPP(52) : warning C4715: 'Rowsum' : not all control paths return a value
C:\Documents and Settings\DR.ABELEGA\MATRIX SUM.CPP(63) : warning C4715: 'Colsum' : not all control paths return a value

MATRIX SUM.OBJ - 0 error(s), 2 warning(s)
In your functions Rowsum and Colsum you seem to return before the outside loop finishes.

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
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <iomanip>
using namespace std;

const int NUM_ROWS = 3;
const int NUM_COLS = 5;
int sales[NUM_ROWS][NUM_COLS] =
{
{ 88, 97, 79, 86, 94 },
{ 86, 91, 78, 99, 82 },
{ 84, 78, 81, 77, 93 } };
int rowsum[NUM_ROWS];
int colsum[NUM_COLS];

int Rowsum(int arr[][NUM_COLS], int[]);

int Colsum(int arr[][NUM_COLS], int[]);

void transpose(int arr[][NUM_COLS], int);

int main()
{
	cout << "The transpose of the matrix is" << endl;
	transpose(sales, NUM_COLS);
	cout << endl;
	rowsum[NUM_ROWS] = Rowsum(sales, rowsum);
	for (int i = 0; i < NUM_ROWS; i++)
	{
		for (int j = 0; j < NUM_COLS; j++)

			cout << setw(5) << sales[i][j];

		cout << "|" << setw(5) << Rowsum(sales, rowsum) << endl;

	}
	for (int j = 0; j < NUM_COLS; j++)
		cout << "---" << setw(5) << Colsum(sales, colsum) << endl;

	return 0;
}

int Rowsum(int arr[][NUM_COLS], int rsum[])
{
	for (int row = 0; row < NUM_ROWS; row++)
	{
		int total = 0;

		for (int col = 0; col < NUM_COLS; col++)

			total += arr[row][col];

		return total;  // You are returning before the loop has ended
	}
	// Do you want to return here instead?
}
int Colsum(int arr[][NUM_COLS], int csum[])
{
	for (int col = 0; col < NUM_COLS; col++)
	{
		int total = 0;
		for (int row = 0; row < NUM_ROWS; row++)
			total += arr[row][col];

		return total; // You are returning before the loop has ended
	}
	// Do you want to return here instead?
}
void transpose(int arr[][NUM_COLS], int size)
{
	for (int row = 0; row < size; row++)
	{
		for (int cols = 0; cols < NUM_ROWS; cols++)
		{
			cout << setw(5) << arr[cols][row] << " ";
		}
		cout << endl;
	}
}
Well that doesnt work actually it not only gives a wrong sum but the row and column summation problem(in the question) still remains. if i place the return outside, total will be an undeclared identifer and if i define total outside the loop the answer will be totally wrong......
Looking at your function int Rowsum(), it looks like you are not using one of the parameters: int rsum[]?

Also, if you are adding up EVERY ROW then surely your answer is going to be an array, not a single int?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int Rowsum(int arr[][NUM_COLS], int rsum[])
{
	for (int row = 0; row < NUM_ROWS; row++)
	{
		int total = 0;

		for (int col = 0; col < NUM_COLS; col++)

			total += arr[row][col];

		return total;  
	}
	// Why are you returning an int and ignoring int rsum[]?
}


Also I don't think this does what you think it does:

 
rowsum[NUM_ROWS] = Rowsum(sales, rowsum);


All that does is set ONE element of rowsum[] to a value. In fact it also goes beyond the end of the array, which could cause a crash.

Here is how you address arrays:
1
2
3
4
5
6
7
8
9
10
11
12
13
const int SIZE = 3;

int array[SIZE]; // A three element array has elements 0, 1 & 2.

array[0] = 0;
array[1] = 0;
array[2] = 0;

// Remember SIZE = 3 so THIS is illegal!!

array[SIZE] = 0; // BAD, there are only 3 elements: 0, 1 and 2.
// But SIZE = 3 which is one more than the last element.


Going back to your problem, I think you need to be putting the results in the parameter int rsum[]. Because there will be one for each row:

1
2
3
4
5
6
7
8
9
10
11
12
void Rowsum(int arr[][NUM_COLS], int rsum[])
{
	for (int row = 0; row < NUM_ROWS; row++)
	{
		rsum[row] = 0;

		for (int col = 0; col < NUM_COLS; col++)
		{
			rsum[row] += arr[row][col];
		}	
	}
}


Then the values in each element of rsum[] will contain the sum of each row.
yeah thanks Galik that was the error... I actually thought of that before, in fact i actually wrote it like that before but it was givin' me zero as the sum but now i figured it out.. Thanks again i'm grateful
Topic archived. No new replies allowed.