Help to create an addition function for sums of rows and columns of a matrix. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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?

9th Sep 2017, 9:31 PM
Raad Wassey
1 Answer