-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatrixMult.c
103 lines (88 loc) · 1.82 KB
/
matrixMult.c
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
#include <stdio.h>
#include <conio.h>
#include <math.h>
//Global vars
int matrix1[25][25], matrix2[25][25], result[25][25];
int r1, c1, r2, c2, i, j, k; //throughout the program i and j are variables reserved for use in for loops
int initSetup()
{
printf("Enter the no. of rows in the first matrix and press the return key then proceed to enter the no. of coloumns \n");
scanf("%d", &r1);
printf("x\n");
scanf("%d", &c1);
printf("Enter the no. of rows in the second matrix and press the return key then proceed to enter the no. of coloumns \n");
scanf("%d", &r2);
printf("x\n");
scanf("%d", &c2);
}
int handleErr()
{
printf("Error! the matrices cannot be multiplied\n\nEnter new values\n");
initSetup();
}
int checkForErr()
{
if (c1 != r2)
{
return true;
}
}
getValues()
{
printf("\nEnter elements a[i][j] of the first matrix(%dx%d)\n", r1, c1);
for(i = 1; i <= r1; i++)
{
for(j = 1; j <= c1; j++)
{
// taking a[i][j] as general element of Ist matrix
printf("Element a[%d][%d]= ", i, j);
scanf("%d",&matrix1[i][j]);
}
}
printf("\nEnter elements b[i][j] of the second matrix(%dx%d)\n", r2, c2);
for(i = 1; i <= r2; i++)
{
for(j = 1; j <= c2; j++)
{
// taking b[i][j] as general element of IInd matrix
printf("Element b[%d][%d]= ", i, j);
scanf("%d",&matrix2[i][j]);
}
}
}
multiplyMatrices()
{
int sum=0;
for(i=1;i<=r1;i++){
for(j=1;j<=c2;j++){
for(k=1;k<=r2;k++){
sum+=matrix1[i][k]*matrix2[k][j];
}
result[i][j]=sum;
sum=0;
}
}
}
printResult()
{
for(i = 1; i <= c1; i++)
{
for(j = 1; j <= c2; j++)
{
printf("%d\t", result[i][j]);
}
printf("\n");
}
}
int main()
{
initSetup();
if (checkForErr() == true)
{
handleErr();
};
getValues();
multiplyMatrices();
printResult();
return 0;
}