forked from sahilbansal17/Cplusplus
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMultiplyTwoMatrices
More file actions
63 lines (55 loc) · 1.65 KB
/
MultiplyTwoMatrices
File metadata and controls
63 lines (55 loc) · 1.65 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
#include<iostream>
using namespace std;
int main()
{
int a[10][10], b[10][10], c[10][10], r1, c1, r2, c2, i, j, k;
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
while (c1!=r2)
{
cout << "Error! column of first matrix not equal to row of second.";
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < r2; ++i)
for(j = 0; j < c2; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
cout<<"\n\n Multiplication of Two Matrices : \n";
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
sum=0;
for(k=0; k<c1; k++)
{
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
cout<<" ";
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
return 0;
}