-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcocktail.c
77 lines (71 loc) · 1.4 KB
/
cocktail.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
#include <stdio.h>
#include <stdlib.h>
void multiply(int m,int n,int (*arr1)[n],int a,int b,int (*arr2)[b],int (*ans)[b])
{
int i=0,j=0,k=0;
while(i<m)
{
j=0;
while(j<b)
{
k=0;(*(*(ans+i)+j))=0;
while(k<n)
{
(*(*(ans+i)+j))=(*(*(ans+i)+j))+(*(*(arr1+i)+k))*(*(*(arr2+k)+j));k++;
}
j++;
}
i++;
}
return;
}
int main()
{
FILE *f1=fopen("input.txt","r");
FILE *f2=fopen("input2.txt","r");
FILE *f3=fopen("answer.txt","w");
if(f1==NULL||f2==NULL||f3==NULL)
{
printf("FILE not opened properly");
return 0;
}
int m,n,a,b;
fscanf(f1,"%d %d",&m,&n);
fscanf(f2,"%d %d",&a,&b);
if(n!=a)
{
printf("Matrix multiplication not possible");
return 0;
}
int arr1[m][n];
int arr2[a][b];
int ans[m][b];
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
fscanf(f1,"%d",(*(arr1+i)+j));
}
}
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
fscanf(f2,"%d",(*(arr2+i)+j));
}
}
multiply(m,n,arr1,a,b,arr2,ans);
for(i=0;i<m;i++)
{
for(j=0;j<b;j++)
{
fprintf(f3,"%d ",*(*(ans+i)+j));
}
fprintf(f3,"\n");
}
fclose(f1);
fclose(f2);
fclose(f3);
return 0;
}