-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmatrixMultiplication.cpp
More file actions
66 lines (59 loc) · 2.04 KB
/
Copy pathmatrixMultiplication.cpp
File metadata and controls
66 lines (59 loc) · 2.04 KB
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
#include <iostream>
using namespace std;
int main()
{
// Initialize variables and arrays
int l, m, z, n;
int matrixA[10][10];
int matrixB[10][10];
int matrixC[10][10];
// Get dimensions of matrices to be multiplied
cout << "Enter the dimensions of the first matrix, rows first, then columns: ";
cin >> l >> m;
cout << "\nEnter the dimensions of the second matrix, rows first, then columns: ";
cin >> z >> n;
// Validation of size of matrices for multiplication
while (m!=z)
{
cout <<"Error. The columns of the first matrix must equal the rows of the second matrix.";
cout <<"Please re-enter the dimensions of the first matrix, rows first, then columns: ";
cin >> l >> m;
cout <<"Please re-enter the dimensions of the first matrix, rows first, then columns: ";
cin >> z >> n;
}
// Obtain data for first matrix
cout<<"\nEnter each number in the first matrix, starting with a11, a12, ..., a1n, \n"
"then a21, a22, ... a2n, etc... :" << endl;
for (int i = 0; i < l; i++)
{ for (int j = 0; j < m; j++)
{ cin >> matrixA[i][j];
}
}
// Obtain data for second matrix
cout<<"Enter each number in the second matrix, starting with b11, b12, ..., b1n, \n"
"then b21, b22, ... b2n, etc... :" << endl;
for (int i = 0; i < z; i++)
{ for (int j = 0; j < n; j++)
{ cin >> matrixB[i][j];
}
}
// Multiply matrices using iteration
for (int i = 0; i < l; i++)
{ for (int j = 0; j < n; j++)
{ matrixC[i][j]=0;
for (int k = 0; k < m; k++)
{
matrixC[i][j] = matrixC[i][j]+(matrixA[i][k] * matrixB[k][j]);
}
}
}
// Display resulting matrix
cout << "\nThe answer is: " << endl;
for (int i = 0; i < l; i++)
{ for (int j = 0; j < n; j++)
{ cout << matrixC[i][j] << " ";
}
cout <<endl;
}
return 0;
}