Solved

# 2-D array totals

Posted on 2000-02-20
183 Views
I need to display a total from the columns on a 2-D array. I already have the totals on the rows but cant figure out how to get the column totals. Using DJGPP rhide v1.4. Thanks in advance.
0
Question by:sl_17b
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 5

Expert Comment

ID: 2541136
Ain't the same?
0

LVL 2

Expert Comment

ID: 2541176
I don't really understand what you're asking. As Wyn said, what's the difference between adding up the rows and adding up the columns? Both are 'for' loops, ain't they?
0

Expert Comment

ID: 2541288
I understood your question this way: You have a two dimensional array
to represent a physical table. And you want to add up columns. It this is
the question , then, you can work out this way.

int Array[Cols][Rows];

int ColSum[Rows];  // to store the col sums.
int sum=0;
for ( int i=0; i < Rows; i++)
{
sum =0;
for ( int j=0; j < Cols; j++)
sum=sum + Array[i][j];
colSum[i]=sum;
}

0

LVL 3

Expert Comment

ID: 2541297

int array[maxcolumn][maxrow];

to sum up the culumns in a two dimensional array:
int maxrow = whatever;
int maxcolumn = whatever;

int array[maxcolumn][maxrow]
int sum;

for(int col = 0; col < maxcolumn; col++)
{
sum = 0;
for(int row = 0; row < maxrow; row++;
{
sum += array[col][row];
}
//  Do something with sum of column before starting on the next column

cout << " Sum of column # " << col << " = " << sum;
}

to sum up the rows:

for (int row = 0; row < maxrow; row++)
{
sum = 0;
for (int col = 0; col < maxcolumn; col++)
{
sum += array[col][row];
}
// do something with sum of row before starting on the next row
cout << " Sum of row # " << row << " = " << sum;
}

Of course you don't have to output the sum of each column or row, you could store it or whatever.
0

LVL 3

Expert Comment

ID: 2541301
dang, posted at close to the same time as chinni, so I didn't see their post before I posted mine, else I wouldn't have bothered.
0

LVL 3

Expert Comment

ID: 2541309
chinni:  correct me if I'm wrong, but it seems to me as if your code adds up the row totals, not the column totals.
0

Expert Comment

ID: 2541325
You r right tefkasg;
I wrongly typed the order of i and j in the array.
It should be
Thanks.
It is a mistake, and i deserve to withdraw my answer  to promote urs.
0

Expert Comment

ID: 2541328
TEfkasg,
0

LVL 3

Accepted Solution

TEFKASG earned 50 total points
ID: 2541338

int array[maxcolumn][maxrow];

to sum up the culumns in a two dimensional array:
int maxrow = whatever;
int maxcolumn = whatever;

int array[maxcolumn][maxrow]
int sum;

for(int col = 0; col < maxcolumn; col++)
{
sum = 0;
for(int row = 0; row < maxrow; row++;
{
sum += array[col][row];
}
//  Do something with sum of column before starting on the next column

cout << " Sum of column # " << col << " = " << sum;
}

to sum up the rows:

for (int row = 0; row < maxrow; row++)
{
sum = 0;
for (int col = 0; col < maxcolumn; col++)
{
sum += array[col][row];
}
// do something with sum of row before starting on the next row
cout << " Sum of row # " << row << " = " << sum;
}

Of course you don't have to output the sum of each column or row, you could store it or whatever.
0

LVL 3

Expert Comment

ID: 2541340
chinni:  Thanks for being a good sport :>)
0

Expert Comment

ID: 2541348
tefkasg,
please check both the parts once again. I am afraid, which you wrote as
gives u some or rows is actually giving sum of col and viceversa.
rows/cols  1      2     3

1            3      4     5

2           1      1      1

3          5      6      7

In Array it is represented as
a[0][]=  3,4,5
a[1] []= 1,1,1
a[2] []= 5,6,7
Then your program part which returns the sum of cols is adding up all the
elements of a[0]  --> 3 + 4 + 5   which is nothing but the sum of row 1.
It shoud be the otherway, which you write for the sum of rows.
It is doing the actual column total --> 3 + 1 + 5 .
Pl check and if i am wrong correct me too.
0

LVL 3

Expert Comment

ID: 2541388
Assuming your array representation is correct, the visual display would look like as follows:

a[0][0] = 3  a[1][0] = 1  a[2][0] = 5
a[0][1] = 4  a[1][1] = 1  a[2][1] = 6
a[0][2] = 5  a[1][2] = 1  a[2][2] = 7

remember we chose the first index as the column index so a[0][row] equals the first column
therefore the sum of column 0 is 3 + 4 + 5
0

LVL 3

Expert Comment

ID: 2541397
if your visual display is correct, the array would be:

a[0][] = 3, 1, 5
a[1][] = 4, 1, 6
a[2][] = 5, 1, 7

if you plug this array into my algorithm, you will get the correct results.
0

LVL 3

Expert Comment

ID: 2541401
chinni: basically what I am saying is that your array storage does not match your visual representation.
0

Expert Comment

ID: 2541405
tefkasg,
i am sorry to disagree with u.
When the array representation of mine is correct and from the physical
matrix i have shown, what should be columns?
i think the columns, should be as they are in the physical table.
then for the first column the sum of columns should be 3 + 1 + 5 , but
not 3 + 4 + 5 .
Check out the physical matrix i want to represent. The concept and
relationship of columns and rows shoud be taken from the physical view but not from the way it is represented in a lang.

0

Expert Comment

ID: 2541406
tefkasg,
When the array representation of mine is correct and from the physical
matrix i have shown, what should be columns?
i think the columns, should be as they are in the physical table.
then for the first column the sum of columns should be 3 + 1 + 5 , but
not 3 + 4 + 5 .
Check out the physical matrix i want to represent. The concept and
relationship of columns and rows shoud be taken from the physical view but not from the way it is represented in a lang.

0

LVL 3

Expert Comment

ID: 2541423
The correct array[column][row] values for the table you have shown are:

a[0][0] = 3  a[1][0] = 4  a[2][0] = 5
a[0][1] = 1  a[1][1] = 1  a[2][1] = 1
a[0][2] = 5  a[1][2] = 6  a[2][2] = 7

Use this array with my code and you will get the correct output.
0

LVL 3

Expert Comment

ID: 2541469
for good measure:

if your 2D array is of the form :
array[row}[column]
the only difference is the line where you sum up the column or row values at the center of the for loops,  which should then be:

sum += array[row][col];

0

LVL 3

Expert Comment

ID: 2541471
Sorry, slight typo there. it should read:
for good measure:

if your 2D array is of the form :
array[row][column]
the only difference is the line where you sum up the column or row values at the center of the for loops,  which should then be:

sum += array[row][col];

0

Expert Comment

ID: 2542479
tefkasg,
It's been too much disc on this. Fine! Programs are matching to our
individual representations of matrix. The code and its working purely depends on how u store it inside.

I am from Elec engg back ground and
i never heard of that kind of representation. All standard Matrices libraries
requires  this format to opeerate on matrices:
Suppose this is ur physical matrix:
1 2 3
4 5 6
7 8 9
The it is represented in matrix, then A[3][3]
A[0][0]=1   A[0][1]=2   A[0][2]=3 and so on.

a physical matrix is not standard one, though it works fine.
chinni

0

Author Comment

ID: 2542492
Thanks,
Once I put it in the portion I needed it ended up like this:

cout << "\n\n college total: ";

for(college=0;college<column;college++)
{
college_total = 0;
for (size = 0; size < row; size++)
{
college_total = sweats[size][college]
}

cout << setw(5)<< college_total;
}
0

LVL 3

Expert Comment

ID: 2543454
chinni:  it all depends on how you label your array.  The array you are describing is of the form:

array[row][column]

by definition.  The first index is the row, the second is the column. This is fine, but your code specifies an array of the form:

array[column][row]

which will not work correctly, if the array is input correctly.

int array[row][col];

in your code it would have been correct.

0

LVL 3

Expert Comment

ID: 2543466
Let me rephrase that last bit.  If your array was input as :
int array[row][column] and your inside for loop were on the outside, it would then be correct.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base classâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month8 days, 9 hours left to enroll