#include<stdio.h>
#define N (2)
void mult(double *ma, double *mb, double *mc, int n)
{
int i, j, k;
double sum;
for (j = 0; j < n; j++) {
for (i = 0; i < n; i++) {
sum = 0.0;
for (k = 0; k < n; k++) {
sum += *(ma + j * n + k) * (*(mb + k * n + i));
}
*(mc + j * n + i) = sum;
}
}
}
void print_matrix(double *x, char *s)
{
int i, j;
for (j = 0; j < N; j++) {
for (i = 0; i < N; i++) {
printf("%f ", *(x
+ j
* N
+ i
)); }
}
}
int main()
{
double ma[N][N] = { {1.0, 2.0}, {3.0, 4.0} };
double mb[N][N] = { {1.0, 2.0}, {-2.0, -1.0} };
double mc[N][N] = { 0.0 };
mult((double *) ma, (double *) mb, (double *) mc, N);
print_matrix((double *) ma, "ma");
print_matrix((double *) mb, "mb");
print_matrix((double *) mc, "mc = ma * mb");
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KCiNkZWZpbmUgTiAgKDIpCgp2b2lkIG11bHQoZG91YmxlICptYSwgZG91YmxlICptYiwgZG91YmxlICptYywgaW50IG4pCnsKICAgIGludCBpLCBqLCBrOwogICAgZG91YmxlIHN1bTsKICAgIGZvciAoaiA9IDA7IGogPCBuOyBqKyspIHsKICAgICAgICBmb3IgKGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIHN1bSA9IDAuMDsKICAgICAgICAgICAgZm9yIChrID0gMDsgayA8IG47IGsrKykgewogICAgICAgICAgICAgICAgc3VtICs9ICoobWEgKyBqICogbiArIGspICogKCoobWIgKyBrICogbiArIGkpKTsKICAgICAgICAgICAgfQogICAgICAgICAgICAqKG1jICsgaiAqIG4gKyBpKSA9IHN1bTsKICAgICAgICB9CiAgICB9Cn0KCnZvaWQgcHJpbnRfbWF0cml4KGRvdWJsZSAqeCwgY2hhciAqcykKewogICAgaW50IGksIGo7CiAgICBwcmludGYoIiVzID1cbiIsIHMpOwogICAgZm9yIChqID0gMDsgaiA8IE47IGorKykgewogICAgICAgIHByaW50ZigiICB8ICIpOwogICAgICAgIGZvciAoaSA9IDA7IGkgPCBOOyBpKyspIHsKICAgICAgICAgICAgcHJpbnRmKCIlZiAiLCAqKHggKyBqICogTiArIGkpKTsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCJ8XG4iKTsKICAgIH0KfQoKaW50IG1haW4oKQp7CiAgICBkb3VibGUgbWFbTl1bTl0gPSB7IHsxLjAsIDIuMH0sIHszLjAsIDQuMH0gfTsKICAgIGRvdWJsZSBtYltOXVtOXSA9IHsgezEuMCwgMi4wfSwgey0yLjAsIC0xLjB9IH07CiAgICBkb3VibGUgbWNbTl1bTl0gPSB7IDAuMCB9OwoKICAgIG11bHQoKGRvdWJsZSAqKSBtYSwgKGRvdWJsZSAqKSBtYiwgKGRvdWJsZSAqKSBtYywgTik7CiAgICBwcmludF9tYXRyaXgoKGRvdWJsZSAqKSBtYSwgIm1hIik7CiAgICBwcmludF9tYXRyaXgoKGRvdWJsZSAqKSBtYiwgIm1iIik7CiAgICBwcmludF9tYXRyaXgoKGRvdWJsZSAqKSBtYywgIm1jID0gbWEgKiBtYiIpOwoKICAgIHJldHVybiAwOwp9