-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix assignment.cpp
96 lines (82 loc) · 2.45 KB
/
matrix assignment.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
87
88
89
90
91
92
93
94
#include <stdio.h>
int main()
{
int rows1, columns1, rows2, columns2, i, j, k, sum = 0,first[100][100], second[100][100], multiply[100][100];
printf("Please enter '0' to exit program\n");
printf("Enter number of rows of first matrix: ");
scanf("%d", &rows1);
if (rows1==0)
{
printf("Program exist... Bye... \n");
goto a;
}
printf("Enter number of columns of first matrix: ");
scanf("%d", &columns1);
if (columns1==0)
{
printf("Program exist... Bye... \n");
goto a;
}
printf("Enter number of rows of second matrix: ");
scanf("%d", &rows2);
if (rows2==0)
{
printf("Program exist... Bye... \n");
goto a;
}
printf("Enter number of columns of first matrix: ");
scanf("%d", &columns2);
if (columns2==0)
{
printf("Program exist... Bye... \n");
goto a;
}
if (columns1 != rows2)
{
printf("Can't multiply! Number of first column should be equal to second row. \n");
}
else
{
printf("Enter elements of first matrix\n");
for (i = 0; i < rows1; i++)
{
for (j = 0; j < columns1; j++)
{
printf("Enter element [%d] [%d] :",i,j);
scanf("%d", &first[i][j]);
}
}
printf("Enter elements of second matrix\n");
for (i = 0; i < rows2; ++i)
{
for (j = 0; j < columns2; ++j)
{
printf("Enter element [%d] [%d] :",i,j);
scanf("%d", &second[i][j]);
}
}
for (i = 0; i < rows1; ++i)
{
for (j = 0; j < columns2; ++j)
{
for (k = 0; k < rows2; ++k)
{
sum = sum + first[i][k]*second[k][j];
}
multiply[i][j] = sum;
sum = 0;
}
}
printf("Result matrix is :\n");
for (i = 0; i < rows1; ++i)
{
for (j = 0; j < columns2; ++j)
{
printf("%d\t", multiply[i][j]);
}
printf("\n");
}
}
a:;
return 0;
}