0

I want to read two matrices from two separate text files and multiply them. The matrices are 3x3 and 3x1 respectively, but I want to write the code that works for up to 10x10 matrices multiplication (so 10x10 matrix times 10x10 matrix). Below is my code, please help me! What I'm trying to do is to use for loops for rows and columns to read the files.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

int i, j;
int A[10][10], B[10][10], C[10][10]; 

ifstream inFile1("matrix(1).txt");
ifstream inFile2("vector(1).txt");

for (int i = 0; i < 10; i++) {

    for (int j = 0; j < 10; j++) {
        inFile1 >> A[i][j];
        inFile2 >> B[i][j];
    }

}

while (!inFile1.eof()) {
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            inFile1 >> A[i][j];
            inFile2 >> B[i][j];
        }
    }


}





}
JJL
  • 15
  • 4
  • Help you with what? Please state exactly what is your problem. Also you did not post the contents of those files, so nobody can say if the loading code is correct or not. What sort of trouble you have with implementing the multiplication? Please post what you've tried and what's wrong with it. – Quimby Mar 31 '19 at 21:35
  • The text files contain pure integers only, so I don't think that will be a problem. When I run the code, it says it cannot find the files and can't open the debugger because of it, although I placed the files in the same directory as the source code. I want to know if my method is correct and what I should do to continue with the code. – JJL Mar 31 '19 at 21:42
  • When you launch the debugger it might set different working directory. The simplest solution is to use `ofstream("testFile")`and see where it gets created and place the input files there. Please post the exact content of those files anyway. see [why is eof in the loop wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). You should probably continue by implementing the multiplication algorithm itself, or at least try to do so. – Quimby Mar 31 '19 at 21:49
  • Is there nothing wrong with the code except the eof? I want to use this code for the multiplication of any size matrices (up to 10x10), I'm wondering if I can first set the matrices to 10x10 then use eof to read only until data runs out. So if it is a 3x3 matrix times 3x3 matrix, I want the code to work anyway irrespective of the size of the matrices, given that I first set the matrices to 10x10 as the code above. – JJL Mar 31 '19 at 21:58
  • You didn't mention any of that. That's why I said earlier to state exactly your problem. In that case yes, your code isn't aware of those sizes, your first for loop iteration reads 10 numbers and stores them in the `A[0][j];` But for 2x2 matrix there aren't 10 numbers. 5x5 matrix will be instead stored as (4.5)x10 matrix with the fifth row half empty. Also for some reason you read those files twice. So my only advice is to precisely restate your problem, post those files and post the [MCVE](https://stackoverflow.com/help/mcve) along with at least an attempt for the multiplication algorithm. – Quimby Mar 31 '19 at 22:09

0 Answers0