0

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

  • 2
    Off topic: Before you go too far, I strongly recommend familiarizing yourself with [the Rules of Three, Five, and Zero](http://en.cppreference.com/w/cpp/language/rule_of_three) and spending some time [standing on the shoulders of giants](https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op) – user4581301 Apr 16 '17 at 22:56
  • A read through [sbi's piece on Operator Overloading](http://stackoverflow.com/questions/4421706/operator-overloading) is a good idea, too. A combination of the three should answer your question or put you in a position where you can better target your problem. – user4581301 Apr 16 '17 at 23:00
  • @user4581301 sorry:(, i think i cant explain good my problem because my english is bad. i'm from latinoamerica:/ thanks anyway. :) – somebodylikeyou Apr 17 '17 at 00:35
  • 1
    Off topic: If you need a matrix library, do not write it yourself, use e.g. [eigen](http://eigen.tuxfamily.org/index.php?title=Main_Page) – yar Apr 17 '17 at 01:35
  • @yar i can't use that:( – somebodylikeyou Apr 17 '17 at 01:43
  • for what reason can't you? – yar Apr 17 '17 at 02:00
  • @yar I didnt know about that until now. they havent taught us that. D: – somebodylikeyou Apr 17 '17 at 02:16
  • So now you can =). If it's just a lab, it's a different story but if you need to do something real, don't try to reinvent the wheel. – yar Apr 17 '17 at 02:17
  • Your English is not a problem. What you want is a `main` that looks something like `int main() { matrix A("matrix1.txt"); matrix B("matrix2.txt"); matrix C = A + B; }`, but your `matrix` class is too simple to make this possible. Complying with the Rule of Three and implementing an `operator+` with the correct signature will fix your problems. I do not want to write up a formal answer because it would require me to effectively write the whole program for you and then you learn too little from the assignment. – user4581301 Apr 17 '17 at 04:33
  • thankyou! :) @user4581301 – somebodylikeyou Apr 17 '17 at 05:48

0 Answers0