Help With Code

Code returns large unwanted numbers as the output of the calculation. It is supposed to calculate the product of two matrices.

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int c;
    int d;
    int e;
    int f;
    int g;
    int h;
    int i;
    int j;
    int k;
    int l;
    int m;
    int n;
    int o;
    int p;
    int q;
    int r;
    int final1 = (a*j) + (b*m) + (c*p);
    int final2 = (a*k) + (b*n) + (c*q);
    int final3 = (a*l) + (b*o) + (c*r);
    int final4 = (d*j) + (e*m) + (f*p);
    int final5 = (d*k) + (e*n) + (f*q);
    int final6 = (d*l) + (e*o) + (f*r);
    int final7 = (g*j) + (h*m) + (i*p);
    int final8 = (g*k) + (h*n) + (i*q);
    int final9 = (g*l) + (h*o) + (i*r);
    int matrix1[3][3] = {{a,b,c},{d,e,f},{g,h,i}};
    int matrix2[3][3] = {{j,k,l},{m,n,o},{p,q,r}};

    //a b c       j k l    f1 f2 f3
    //d e f   *   m n o  = f4 f5 f6
    //g h i       p q r    f7 f8 f9

    int endmatrix [3][3] = {{final1,final2,final3},{final4,final5,final6},{final7,final8,final9}};

    cout << "Matrix Multiplication" << endl;
    cout << "\nEnter Row 1 Column 1 of Matrix 1" << endl;
    cout << "Input: ";
    cin >> a;
    cout << "\n\nEnter Row 1 Column 2 of Matrix 1" << endl;
    cout << "Input: ";
    cin >> b;
    cout << "\n\nEnter Row 1 Column 3 of Matrix 1" << endl;
    cout << "Input: ";
    cin >> c;
    cout << "\n\nEnter Row 2 Column 1 of Matrix 1" << endl;
    cout << "Input: ";
    cin >> d;
    cout << "\n\nEnter Row 2 Column 2 of Matrix 1" << endl;
    cout << "Input: ";
    cin >> e;
    cout << "\n\nEnter Row 2 Column 3 of Matrix 1" << endl;
    cout << "Input: ";
    cin >> f;
    cout << "\n\nEnter Row 3 Column 1 of Matrix 1" << endl;
    cout << "Input: ";
    cin >> g;
    cout << "\n\nEnter Row 3 Column 2 of Matrix 1" << endl;
    cout << "Input: ";
    cin >> h;
    cout << "\n\nEnter Row 3 Column 3 of Matrix 1" << endl;
    cout << "Input: ";
    cin >> i;
    cout << "\n\nEnter Row 1 Column 1 of Matrix 2" << endl;
    cout << "Input: ";
    cin >> j;
    cout << "\n\nEnter Row 1 Column 2 of Matrix 2" << endl;
    cout << "Input: ";
    cin >> k;
    cout << "\n\nEnter Row 1 Column 3 of Matrix 2" << endl;
    cout << "Input: ";
    cin >> l;
    cout << "\n\nEnter Row 2 Column 1 of Matrix 2" << endl;
    cout << "Input: ";
    cin >> m;
    cout << "\n\nEnter Row 2 Column 2 of Matrix 2" << endl;
    cout << "Input: ";
    cin >> n;
    cout << "\n\nEnter Row 2 Column 3 of Matrix 2" << endl;
    cout << "Input: ";
    cin >> o;
    cout << "\n\nEnter Row 3 Column 1 of Matrix 2" << endl;
    cout << "Input: ";
    cin >> p;
    cout << "\n\nEnter Row 3 Column 2 of Matrix 2" << endl;
    cout << "Input: ";
    cin >> q;
    cout << "\n\nEnter Row 3 Column 3 of Matrix 2" << endl;
    cout << "Input: ";
    cin >> r;

    cout << "\n\nThe product of your two matrices is:" << endl;
    cout << endmatrix[0][0];
    cout << "     ";
    cout << endmatrix[0][1];
    cout << "     ";
    cout << endmatrix[0][2] << endl;
    cout << endmatrix[1][0];
    cout << "     ";
    cout << endmatrix[1][1];
    cout << "     ";
    cout << endmatrix[1][2] << endl;
    cout << endmatrix[2][0];
    cout << "     ";
    cout << endmatrix[2][1];
    cout << "     ";
    cout << endmatrix[2][2] << endl;
}
Your variables that correspond to the alphabet were not initialized and contain arbitrary values.
Remember that everything happens sequentially.

You are doing multiplication and assigning values to your matrix on lines 25-35.

You are not getting the actual data from the user until lines 46+.

This means the math you are doing on lines 25-35 are with random garbage and not with the data the user provided, because you didn't get the user data yet.
OK, so I need to put those lines of code after the user inputs the values?
Thank You So Much Disch, it works now!
Topic archived. No new replies allowed.