### Problem regarding the output of the sum of rows in a matrix

Hey so my code reads a text file and then prints the sum of the columns. I am just trying it to get to print the sum of the rows.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138`` ``````#include #include #include #include #include using std::cout; using std::vector; void readFile( const char* fname );// just looking bool readFile( const char* fname, vector& A, int& rows, int& cols );// copies file data to array void print( const vector& A, int cols ); void showColSums( const vector& A, int cols ); void showRowSums( const vector& A, int rows ); int main() { int Arows=0, Acols=0; vector A; readFile("MatHW1.txt"); if( readFile( "MatHW1.txt", A, Arows, Acols ) ) { print( A, Acols ); print( A, Arows ); showColSums( A, Acols ); showRowSums( A, Arows ); } else cout << "Could not get array data\n"; return 0; } void readFile( const char* fname )// just looking { std::ifstream fin( fname ); int val=0, rows=0, cols=0, numItems=0; std::stringstream ss; std::string temp; getline( fin, temp ); ss << temp; while( ss >> val ) { cout << val << ' '; ++numItems; } cols = numItems;// # of columns found cout << '\n'; while( fin >> val ) { ++numItems; cout << val << ' '; if( numItems%cols == 0 ) cout << '\n'; } rows = numItems/cols; cout << "rows = " << rows << ", cols = " << cols << '\n'; } // copies file data to vector bool readFile( const char* fname, vector& A, int& rows, int& cols )// copies file data to array { std::ifstream fin( fname ); int val=0, numItems=0; A.clear(); std::stringstream ss; std::string temp; getline( fin, temp ); ss << temp; while( ss >> val ){ ++numItems; A.push_back(val); } cols = numItems;// # of columns found while( fin >> val ){ ++numItems; A.push_back(val); }// keep counting // got data! if( A.size() > 0 ) { rows = numItems/cols; return true; } else// didn't get any data cout << "data reading failed\n"; return false; } void print( const vector& A, int cols) { if( A.size() < (unsigned int)cols ) return; cout << '\n'; for( size_t i=0; i& A, int cols ) { if( A.size() < (unsigned int)cols ) return; cout << "column sums\n"; int rows = A.size()/cols; for( int c=0; c& A, int rows ) { if( A.size() < (unsigned int)rows ) return; cout << "row sums\n"; int cols = A.size()/rows; for (int c=0; c

This is the code I have, I created the sum row function but still outputs the sum of columns. The text file looks like this
5 9 6
7 2 32
5 8 6

Any help
Hello @ynwa,

I think you would guess that, since it is the row sums that are wrong, then you need to look at function showRowSums().

Have a look at the nesting of loops there. You want the sums of the three rows only and to compute the sum of EACH row you will need to scan through the columns. So the inner loop should scan through the columns to find the sum of that row and the outer loop will scan through the rows. But this is the reverse of what you have. So have a think about which way round you should nest loops.

The other problem is your conversion between row/column and 1-d indices. It was obviously OK for columns on line 119. This conversion isn't going to change simply because you want a different sum. So have a look at line 134 and compare with earlier.

Topic archived. No new replies allowed.