-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmatmul.c
65 lines (60 loc) · 1.58 KB
/
matmul.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
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
double tmp;
int i,j,k;
int arows,acols;
double *amatrix;
int brows,bcols;
double *bmatrix;
int crows,ccols;
double *cmatrix;
/* read matrix A */
fscanf(stdin,"%d%d",&arows,&acols);
amatrix = (double *)malloc(arows*acols*sizeof(double));
for (i=0; i<arows; ++i) {
for (j=0; j<acols; ++j) {
fscanf(stdin,"%lg",&(amatrix[i*arows+j]));
}
}
/* read matrix B */
fscanf(stdin,"%d%d",&brows,&bcols);
bmatrix = (double *)malloc(brows*bcols*sizeof(double));
for (i=0; i<brows; ++i) {
for (j=0; j<bcols; ++j) {
fscanf(stdin,"%lg",&(bmatrix[i*brows+j]));
}
}
/* allocate matrix C */
if ((arows != brows) || (acols != bcols)) {
printf("Matrices not compatible: %d vs. %d, %d vs. %d\n",
arows, brows, acols, bcols);
free(amatrix);
free(bmatrix);
return 1;
}
crows = arows;
ccols = acols;
cmatrix = (double *)malloc(crows*ccols*sizeof(double));
/* multiplication */
for (i=0; i<crows; ++i) {
for (j=0; j<ccols; ++j) {
tmp = 0.0;
for (k=0; k < brows; ++k)
tmp += amatrix[i*arows+k]*bmatrix[j*brows+k];
cmatrix[i*crows+j] = tmp;
}
}
/* output result */
for (i=0; i<crows; ++i) {
for (j=0; j<ccols; ++j) {
printf("%.15lg ",cmatrix[i*crows+j]);
}
printf("\n");
}
free(amatrix);
free(bmatrix);
free(cmatrix);
return 0;
}