I need help adding the data in columns and rows of 10 in a 2d array. I got the rows added, but could someone please help out with how to sum the data in the columns. Please look at my code, and thanks in advance

#include <iostream>		// I/O TO DOS SCREEN
#include <fstream>		// FILE I/O
#include <stdlib.h>			// STANDARD LIBRARY
#include <iomanip>	// I/O FORMATTING

using namespace std;

//
// PROTOTYPE STATEMENTS
//
void function_name();

//
// GLOBAL VARIABLE DECLARATIONS
//
const int SIZE = 10;
float sum = 0;

int main() 
{
    int two_di_arr[ SIZE ][ SIZE ];
    ifstream infile("Two_Dimensional_Data_In.txt");
    
    for( int row = 0; row < SIZE; row++ )
    {
       for( int col = 0; col < SIZE; col++ )
    {
	infile >> two_di_arr[ row ][ col ];
              sum += two_di_arr[ row ][ col ];	//This is where I sum the rows
             cout << setw( 6 ) << two_di_arr[ row ][ col ] << " ";	
           }
           cout << sum / SIZE;
           cout << endl;
			
    }


    system("pause");
    return 0;
}
// END MAIN() //////////////////////////////

// FUNCTION DEFINITIONS

void function_name()
{

}
////////////////////////////////////////////

Code tags added. -Narue

Recommended Answers

All 2 Replies

#include <iostream>

using namespace std;

int main()
{
  int a[5][5] = {
    {1, 2,  3, 4, 5},
    {6, 7,  8, 9,10},
    {11,12,13,14,15},
    {16,17,18,19,20},
  };

  // Row major order
  for ( int i = 0; i < 5; i++ ) {
    int sum = 0;

    for ( int j = 0; j < 5; j++ )
      sum += a[i][j];

    cout<< sum <<'\t';
  }

  cout<<endl;

  // Column major order
  for ( int i = 0; i < 5; i++ ) {
    int sum = 0;

    for ( int j = 0; j < 5; j++ )
      sum += a[j][i];

    cout<< sum <<'\t';
  }

  cout<<endl;
}

Thanks alot, that helped....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.