forked from mr-sergi/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix Calculator in cpp
86 lines (76 loc) · 1.85 KB
/
Matrix Calculator in cpp
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
#include<stdio.h>
#include <iostream.h>
#include<conio.h>
int main()
{ clrscr();
int arr1[20][20],arr2[10][10];
int result[20][20];
int m,n;
int i,j;
// prompts the user to enter the rows and columns of the matrix
cout<<" Enter the number of rows (m) : ";
cin>>m;
cout<<" Enter the number of columns (n) : ";
cin>>n;
cout<<" \n Enter the elements of the 1st matrix \n ";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
cout<<" Element "<<i<<" : "<<j<<" --> ";
cin>>arr1[i][j];
}
}
cout<<"\n Enter the elements of the 2nd matrix : \n ";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
cout<<" Element "<<i<<" : "<<j<<" --> ";
cin>>arr2[i][j];
}
}
//------------ display matrics -----------------------
cout<<"\n 1st matrix \n ";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
cout<<" "<<arr1[i][j];
}
cout<<"\n";
}
cout<<"\n 2nd matrix \n ";
for (i=0; i<m; i++)
{
for (j=0; j<n;j++)
{
cout<<" "<<arr2[i][j];
}
cout<<"\n";
}
cout<<" \n Resultatn matrix \n";
int k=0;
// multiplies the two matrices together
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
result[i][j] = 0;
for(k=0;k< m;k++)
{
result[i][j] = result[i][j] + arr1[i][k] * arr2[k][j];
}
} // end of j sub loop
} // end of i main loop
// displays the resultant matrix
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
cout<<result[i][j]<<" ";
}
cout<<"\n \n";
}
return 0;
} // end of main