Good day :) I want to make a program (in Visual C ++) that does several differents operations with two matrix from two different files. So far i was done this.
typedef unsigned int uint;
class matrix {
private:
uint nrows, ncol;
float **elements;
public:
matrix(const char*);
void printmatrix(const char*);
};
#include "matrix.h"
#include <iostream>
#include <fstream>
#include <iomanip>
matrix::matrix(const char*file) {
ifstream fcin(file, ios::in);
if (!fcin) {
cerr << "\nError: the file doesnt exist\n";
exit(EXIT_FAILURE);
}
fcin >> nrows;
fcin >> ncol;
elements = new float*[nrows];
for (uint i = 0; i < nrows; i++) {
elements[i] = new float[ncol];
for (uint j = 0; j < ncol; j++)
fcin >> elements[i][j];
}
fcin.close();
}
void matrix::printmatrix(const char*file) {
ofstream fcout(file, ios::out);
if (!fcout) {
cerr << "\nError: file doesnt exist\n";
exit(EXIT_FAILURE);
}
fcout << nrows;
fcout << "\n";
fcout << ncol;
fcout << "\n";
for (uint i = 0; i < nrows; i++) {
for (uint j = 0; j < ncol; j++)
fcout << setw(6) << elements[i][j];
fcout << "\n";
}
fcout.close();
};
#include "matrix.h"
#include <cstdlib>
int main()
{
matrix A("matrix1.txt");
A.imprimir("matrix2.txt");
system("PAUSE");
return 0;
}
I begun to do the program with randomly generated matrixs, and to do operations (addition, subtraction, multiplication) with overloaded operators; And also do the operations "inverse matrix" and "multiplication by a scalar". However, when working with matrixs from two different text files, I complicate: \ and I have many doubts, starting with, what is the prototype for the operator overload for matrixs from two differents text files?
When I did the sum for randomly generated matrixs, I had done this:
matrix* operator+ (const matrix&matrix2){
matrix*sum=new matriz(nrow, ncol);
for (uint i=0; i<nrows; i++){
for (uint j=0; j<ncol; j++){
sum->elements[i][j]=elements[i][j]+matrix2.elements[i][j];
}
}
return sum;
}
And similarly I had performed the operations mentioned above and they worked well. but, I have no idea how to do operations (with overhead) with matrixs obtained from two different text files. Can you help me, please? :) Sorry if my question is not clear at all, im from latinoamerica and my english is bad