Solved

2-D array totals

Posted on 2000-02-20
23
178 Views
Last Modified: 2010-04-10
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
Comment
Question by:sl_17b
23 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2541136
Ain't the same?
0
 
LVL 2

Expert Comment

by:bbousquet
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

by:chinni021900
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;
}

Hope I answered your question, if your question is what i understood.
0
 
LVL 3

Expert Comment

by:TEFKASG
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

by:TEFKASG
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

by:TEFKASG
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

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

Expert Comment

by:chinni021900
ID: 2541328
TEfkasg,
Change ur comments to answer.
0
 
LVL 3

Accepted Solution

by:
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

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

Expert Comment

by:chinni021900
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Expert Comment

by:TEFKASG
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

by:TEFKASG
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

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

Expert Comment

by:chinni021900
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

by:chinni021900
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

by:TEFKASG
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

by:TEFKASG
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

by:TEFKASG
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

by:chinni021900
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.

Your code works fine and your arg is also right. But your representation of
a physical matrix is not standard one, though it works fine.
Adios amigo
chinni

0
 

Author Comment

by:sl_17b
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

by:TEFKASG
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.

  Had you specified

int array[row][col];

 in your code it would have been correct.
 
0
 
LVL 3

Expert Comment

by:TEFKASG
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now