Solved

2-D array totals

Posted on 2000-02-20
23
180 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 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