L
LL
#include <stdio.h>
#include <stdlib.h>
typedef double matrix[50][50];
// Matrix multiplication
matrix* mm(matrix a, matrix b, int m, int r, int n) {
matrix *c=(matrix*)malloc(sizeof(matrix));
// This part: why doesn't it work properly?
int i,j,k;
for (i=0; i<m; i++) {
for (k=0; k<n; k++) {
for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];
}
}
}
return c;
}
int main() {
// Sample matrices a,b
matrix a_={{1,0},{0,1}}; // Identity matrix
matrix b_={{1,2},{3,4}};
matrix *c_=(matrix*)malloc(sizeof(matrix));
// a_*b_=c_
c_=mm(a_,b_,2,2,2);
int i,j;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
printf("%lf ", (*c_)[j]);
}
printf("\n");
}
}
/* Expected output:
* 1 2
* 3 4
*
* Output:
* 0 0
* 3 4
* Why!!??
*/
#include <stdlib.h>
typedef double matrix[50][50];
// Matrix multiplication
matrix* mm(matrix a, matrix b, int m, int r, int n) {
matrix *c=(matrix*)malloc(sizeof(matrix));
// This part: why doesn't it work properly?
int i,j,k;
for (i=0; i<m; i++) {
for (k=0; k<n; k++) {
for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];
}
}
}
return c;
}
int main() {
// Sample matrices a,b
matrix a_={{1,0},{0,1}}; // Identity matrix
matrix b_={{1,2},{3,4}};
matrix *c_=(matrix*)malloc(sizeof(matrix));
// a_*b_=c_
c_=mm(a_,b_,2,2,2);
int i,j;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
printf("%lf ", (*c_)[j]);
}
printf("\n");
}
}
/* Expected output:
* 1 2
* 3 4
*
* Output:
* 0 0
* 3 4
* Why!!??
*/