How would I add just the columns in this 2-dimensional array?

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int rows = 5;
const int cols = 5;

int main ()
{
	ifstream inData;
	int numbers[rows][cols];
	int i,j;
	

	inData.open("data.txt");

	while (!inData.eof()){
		
		for (i=0; i<rows; i++)
		{	
            for (j=0; j<cols; j++)
            { 
	         inData >> numbers[i][j];
	         cout<<setw(5)<<numbers[i][j]<<" ";
            }
			cout<<endl;
		}

	}
	
return 0;
}

Simply run your column loop as outisde loop, the row loop on the inside, like:

for( j = 0; j < cols; j++ )
{
    sum = 0;
    for( i = 0; i < rows; i++ )
        sum += numbers[i][j];

    //do something with sum
}

Note that even though the row/column loops are switched, don't change the index order where you access the array

I knew it was going to be something easy! Thanks for the help!

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.