Ok This is my first post on this forum, and would really appreciate some help on an assignment...
I have a 2d array and I need to add up all the numbers within the rows and the columns of the array. I'm really not to familiar with arrays or for loops...
I apologize for not using tags/comments in the code, but I'm not really a customs to doing so.

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


void call( const int [][ 6 ] );
void call( const int a[][ 6 ] )
{
   for ( int i = 0; i < 5; i++ )
   {
      for ( int j = 0; j < 6; j++ ){
         cout << a[ i ][ j ] << ' ';
      }
      cout << endl;
   }
}
int main()
{
   int array[ 5 ][ 6 ] = { { 141,   567,   434,   100,   269,   324 },
                            { 578,   458,   562,   564,   305,   245 },
                            { 381,   427,   561,   591,   595,   542 },
                            { 427,   536,   491,   204,   502,   253 },
                            { 392,   482,   521,   316,   318,   495 } };


   cout << "original array" << endl;
   call( array );

   return 0;
}

an example out put would be this...

Example Input Table
   100   101   102   103   104   105
   106   107   108   109   110   111
   112   113   114   115   116   117
   118   119   120   121   122   123
   124   125   126   127   128   129
Example Output Table with rows summed,
columns summed and the grand total printed.
   100   101   102   103   104   105   615
   106   107   108   109   110   111   651
   112   113   114   115   116   117   687
   118   119   120   121   122   123   723
   124   125   126   127   128   129   759
   560   565   570   575   580   585  3435

Edited 5 Years Ago by Narue: Fixed code tags

>>adding rows and columns of a multi array

For a second I was happy because I though you were using boost::multi_array, but oh well.

As for a solution to your problem, I suggest you create an array that holds the
sum of each column. Here is some code hints :

void foo(int array[][COL], const int ROW){
  int colRowSum[COL] = {0};

  foreach(row in array){
     foreach( col in array){
        colRowSum[col] += array[row][col]
      }
  }

  printArray2D( array );
  printArray1D( colRowSum );
}

Thank you for replying :) Well I tried to use what you gave me, but I can't quite get it... I'm very bad with arrays and for loops :( here's what I put:
I got two errors

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void call(int array[][6], const int i){
  int colRowSum[6] = {0};

  for(int i = 0; i < 5; i++){
     for( int j = 0; j < 6; j++){
        colRowSum[i] += array[i][j];
      }
      cout << array [ 5 ][ 6 ] << "";
  }
}

int main(){

    int array[  ][ 6 ] = { { 141,   567,   434,   100,   269,   324 },
                            { 578,   458,   562,   564,   305,   245 },
                            { 381,   427,   561,   591,   595,   542 },
                            { 427,   536,   491,   204,   502,   253 },
                            { 392,   482,   521,   316,   318,   495 } };

call (array);
  return 0;
  }

vectors look allot easier, but the assignment specifically says I need to use multi dem arrays or 2d arrays...

first thing that I spotted is that you only passed in an array when you invoke call fuction

You almost got it, all you had to do was print the result correctly and call the function correctly :

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void call(int array[][6], const int i){
  int colRowSum[6] = {0};

  //sum to column row
  for(int i = 0; i < 5; i++){
     for( int j = 0; j < 6; j++){
        colRowSum[j] += array[i][j];
      }      
  }

  //print array
  for(int i = 0; i != 5; ++i){
	  for(int j = 0; j != 6; ++j){
		  cout.width(10);
		  cout << array[i][j] << " ";
	  }
	  cout << endl;
  }

  cout << "-----------------------------------";
  cout << "-----------------------------------\n";

  //print result
  for(int n = 0; n != 6; ++n){
	  cout.width(10);
	  cout << colRowSum[n] << " ";
  }

  cout << endl;
}

int main(){
    int array[ 5 ][ 6 ] = { { 100,   567,   434,   100,   269,   324 },
                            { 100,   458,   562,   564,   305,   245 },
                            { 100,   427,   561,   591,   595,   542 },
                            { 100,   536,   491,   204,   502,   253 },
                            { 100,   482,   521,   316,   318,   495 } };

	call (array,5);

	return 0;
 
}

Fantastic! I understand what you did, but I'm not sure why when the function is called that it needed the 5 "call (array,5);"
Now from the info you gave me, I was able to add up the rows also :) but I'm not to sure how to get them to out put next to the rows, like this :
100 101 102 103 104 105 615
106 107 108 109 110 111 651
112 113 114 115 116 117 687
118 119 120 121 122 123 723
124 125 126 127 128 129 759
560 565 570 575 580 585 3435

I got to add just fine, its not in the right place...

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void call(int array[5][6], const int i){
  int colRowSum[6] = {0};
  int colSumRow[5] = {0};


  //sum to column row
  for(int i = 0; i < 5; i++){
     for( int j = 0; j < 6; j++){
        colRowSum[j] += array[i][j];
         colSumRow[i] += array[i][j];
      }
  }

  //print array
  for(int i = 0; i != 5; ++i){
	  for(int j = 0; j != 6; ++j){
		  cout.width(10);
		  cout << array[i][j] << " ";
	  }
	  cout << endl;
  }



  //print result
  for(int n = 0; n != 6; ++n){
	  cout.width(10);
	  cout << colRowSum[n] << " ";
  }
  for (int s = 0; s != 5; ++s){
      cout.width(10);
      cout <<colSumRow[s]<<"";
  }

  cout << endl;
}

int main(){
    int array[ 5 ][ 6 ] = { { 141,   567,   434,   100,   269,   324, },
                            { 578,   458,   562,   564,   305,   245, },
                            { 381,   427,   561,   591,   595,   542, },
                            { 427,   536,   491,   204,   502,   253, },
                            { 392,   482,   521,   316,   318,   495, } };

	call (array,5);


	return 0;

}

Edited 6 Years Ago by NvIs: n/a

When you print the whole array, output the row sum value. So in your code it would be like this :

for(int i = 0; i != 5; ++i){
	  for(int j = 0; j != 6; ++j){
		  cout.width(10);
		  cout << array[i][j] << " ";
	  }

          cout.width(10);
          cout << colSumRow[i];

	  cout << endl;
  }

Awwwwww! I see now Thank you very much for you help. I greatly appreciate it :)

Hmm...can i get the whole code of this?....just to study it, on how did you do it... :) plz

This question has already been answered. Start a new discussion instead.