+ 2

Help to create an addition function for sums of rows and columns of a matrix.

#include <fstream> #include <string> #include <vector> #include <iostream> #include <sstream> using namespace std; int ReadNumbers( const string & s, vector <double> & v ); void import_matrix_from_txt_file(const char* filename_X, vector <double>& v, int& rows, int& cols); int main(){ vector <double> v; int rows=0; int cols=0; import_matrix_from_txt_file("MatHW1.txt",v,rows,cols); } int ReadNumbers( const string & s, vector <double> & v ) { istringstream is( s ); double n; while( is >> n ) { v.push_back( n ); } return v.size(); } void import_matrix_from_txt_file(const char* filename_X, vector <double>& v, int& rows, int& cols){ ifstream file_X; string line; file_X.open(filename_X); if (file_X.is_open()) { int i=0; getline(file_X, line); cols =ReadNumbers( line, v ); cout << "cols:" << cols << endl; for ( i=1;i<100;i++){ if ( getline(file_X, line) == 0 ) break; ReadNumbers( line, v ); } rows=i; cout << "rows :" << rows << endl; if(rows >100) cout<< "N must be smaller than MAX_INT"; file_X.close(); } else{ cout << "file open failed"; } cout << "v:" << endl; for (int i=0;i<rows;i++){ for (int j=0;j<cols;j++){ cout << v[i*cols+j] << "\t" ; //cout << v[i]+v[j]; } cout << endl; } This is the code I have so far that reads a text file for a matrix and prints out the matrix. I am having trouble figuring how to make an addition function that creates the sum of columns and rows of the given read matrix from the text file. Also the sums of columns and rows will have to be output in a separate file. PLEASE HELP?

10th Sep 2017, 1:34 AM
Sharar Wassey
3 Antworten
+ 2
Question is not so clear plz elaborate it more. .
10th Sep 2017, 6:53 AM
Azam
Azam - avatar
+ 2
what is meant by sum of rows ad columns?
10th Sep 2017, 6:54 AM
Azam
Azam - avatar
0
So in a text file there is a matrix such as a 2×2 matrix and the code is suppose to read that matrix from a file and then add the rows and columns of the matrix and then print that in an outputfile
10th Sep 2017, 3:17 PM
Sharar Wassey