forked from wafarifki/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix multiplication.c
106 lines (90 loc) · 2.65 KB
/
matrix multiplication.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
104
105
106
/*
Author: Chamod Dananjaya
*/
#include <stdio.h>
#include <stdlib.h>
void main()
{
int **matrix1;
int **matrix2;
int **output;
int r1,r2,c1,c2;
printf("\tEnter your matrix size,\n\nfirst matrix,\n");
printf("\trows: ");
scanf("%d",&r1);
printf("\tColumns: ");
scanf("%d",&c1);
matrix1 = malloc(sizeof(int *)*r1);
printf("\nEnter data here!\n");
for(int i=0; i<r1; i++){
matrix1[i] = malloc(sizeof(int *)*c1);
for(int j=0; j<c1; j++){
printf("\t-> ");
scanf("%d",&matrix1[i][j]);
}
}
printf("\nSecond matrix\n");
printf("\trows: ");
scanf("%d",&r2);
printf("\tColumns: ");
scanf("%d",&c2);
matrix2 = malloc(sizeof(int *)*r2);
printf("\nEnter data here!\n");
for(int i=0; i<r2; i++){
matrix2[i] = malloc(sizeof(int *)*c2);
for(int j=0; j<c2; j++){
printf("\t-> ");
scanf("%d",&matrix2[i][j]);
}
}
if(c1==r2){
printf("\n-----------------------------------------------------");
printf("\n\tMatrxi1\n");
printf("-----------------------------------------------------\n");
for(int i=0; i<r1; i++){
printf("\t");
for(int j=0; j<c1; j++){
printf("%d ",matrix1[i][j]);
}
printf("\n");
}
printf("\n-----------------------------------------------------");
printf("\n\tMatrxi2\n");
printf("-----------------------------------------------------\n");
for(int i=0; i<r2; i++){
printf("\t");
for(int j=0; j<c2; j++){
printf("%d ",matrix2[i][j]);
}
printf("\n");
}
output = malloc(sizeof(int *)*r1);
for(int i=0; i<r1; i++){
output[i] = malloc(sizeof(int *)*c2);
}
int sum=0;
for(int i=0; i<r1; i++){
for(int j=0; j<c2; j++){
for(int k=0; k<r2; k++){
sum += matrix1[i][k] * matrix2[k][j];
}
output[i][j] = sum;
sum=0;
}
}
printf("\n-----------------------------------------------------");
printf("\n\tResultant matrix");
printf("\n-----------------------------------------------------\n");
for(int i=0; i<r1; i++){
printf("\t");
for(int j=0; j<c2; j++){
printf("%d ",output[i][j]);
}
printf("\n");
}
printf("-----------------------------------------------------\n");
}
else{
printf("\nNon compatible matrices!\n");
}
}