Can't multiply two 5x5 matrices

I'm very new to c++ and I'm confused as to why my code isn't working. I feel it's an issue with the final array (array3). I don't know what though. If anyone can help out it'd be appreciated :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  #include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main(){

    int array1[5][5];
    int array2[5][5];
    int array3[5][5];
    string infilename1, infilename2, mystring1, mystring2;
    int i,j,k;

        // Ask for file path and name of file
    cout << "What is the file path and name of the first file?" << endl;
    cin >> infilename1;

        // Ask for file path and name of file
    cout << "What is the file path and name of the second file?" << endl;
    cin >> infilename2;

    // After user gives program path file, it will find the files and opens them.
    ifstream inFile1;
    inFile1.open(infilename1.c_str());

    ifstream inFile2;
    inFile2.open(infilename2.c_str());


    for(int i=0; i<5; i++){
        for(int j =0; j<5; j++){

            getline(inFile1, mystring1, ',');


            array1[i][j] = stoi(mystring1);

            cout << array1[i][j] << endl;
        }
    }

        for( i=0; i<5; i++){
        for( j =0; j<5; j++){

            getline(inFile2, mystring2, ',');


            array2[i][j] = stoi(mystring2);

            cout << array2[i][j] << endl;
        }
    }

        // Multiplying matrix a and b and storing in array3.
    for(i = 0; i < 5; i++){
        for(j = 0; j < 5; j++){
            for(k = 0; k < 5; k++)
            {
                array3[i][j] == array1[i][k] * array2[k][j];
            }
    }
}
    // Displaying the multiplication of two matrix.
    cout << endl << "Output Matrix: " << endl;
    for(i = 0; i < 5; ++i)
    for(j = 0; j < 5; ++j)
    {
        cout << " " << array3[i][j];
    }


    return 0;
}
Last edited on
You are MULTIPLYING ... STRINGS !!!

The rest of us multiply numbers.
Yeah sorry, silly mistake, but I've tried amending it and it still doesn't work.
You have a == instead of = for your assignment to array3.
And you probably want some newlines between the rows of the output matrix.
Some functions wouldn't hurt, either!
Last edited on
Topic archived. No new replies allowed.