/*
http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1354070278/618
618 名前:デフォルトの名無しさん [sage]: 2012/12/28(金) 19:37:31.49
[1] 授業単元:プログラミング演習 2
[2] 問題文(含コード&リンク)
行列の積を計算する関数multの関数プロトタイプ宣言が以下のように記述されている
void mult(double *ma, double *mb, double *mc, int n);
いま、定数Nの値がmain関数の外で#define N 3 と定義されている。N×Nの正方行列maとmb,そしてmcはmain関数の中で
double ma[N][N];
double mb[N][N];
double mc[N][N] = { 0.0 } ;
と定義されている。このとき関数multを次のように呼び出して行列の積mc=ma • mb を計算したい。
mult((double *)ma, (double *)mb, (double *)mc, N);
関数multをブラケット演算子[]なしで書き上げなさい。
[3.1] OS: unix
[3.2] コンパイラ名とバージョン: gcc
[3.3] 言語: C
[4] 期限:1月まで(出来れば早めにお願いします)
*/
#include<stdio.h>
#define N (3)
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, -1.0}, {3.0, 2.0, 1.0}, {0.0, 1.0, 2.0} };
double mb[N][N] = { {1.0, -2.0, -1.0}, {1.0, 2.0, 1.0}, {3.0, 1.0, 2.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;
}
LyoKICAgIGh0dHA6Ly90Li4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5oLm5ldC90ZXN0L3JlYWQuY2dpL3RlY2gvMTM1NDA3MDI3OC82MTgKICAgICAgICA2MTgg5ZCN5YmN77ya44OH44OV44Kp44Or44OI44Gu5ZCN54Sh44GX44GV44KTIFtzYWdlXe+8miAyMDEyLzEyLzI4KOmHkSkgMTk6Mzc6MzEuNDkgIAogICAgICAgIFsxXSDmjojmpa3ljZjlhYPvvJrjg5fjg63jgrDjg6njg5/jg7PjgrDmvJTnv5Ig77ySCiAgICAgICAgWzJdIOWVj+mhjOaWhyjlkKvjgrPjg7zjg4km44Oq44Oz44KvKQogICAgICAgIOihjOWIl+OBruepjeOCkuioiOeul+OBmeOCi+mWouaVsG11bHTjga7plqLmlbDjg5fjg63jg4jjgr/jgqTjg5flrqPoqIDjgYzku6XkuIvjga7jgojjgYbjgavoqJjov7DjgZXjgozjgabjgYTjgosKICAgICAgICB2b2lkIG11bHQoZG91YmxlIO+8im1hLCBkb3VibGUg77yKbWIsIGRvdWJsZSDvvIptYywgaW50IG4pOwogICAgICAgIOOBhOOBvuOAgeWumuaVsE7jga7lgKTjgYxtYWlu6Zai5pWw44Gu5aSW44GnI2RlZmluZSBOIDMg44Go5a6a576p44GV44KM44Gm44GE44KL44CCTsOXTuOBruato+aWueihjOWIl21h44GobWIs44Gd44GX44GmbWPjga9tYWlu6Zai5pWw44Gu5Lit44GnCiAgICAgICAgZG91YmxlIG1hW05dW05dOwogICAgICAgIGRvdWJsZSBtYltOXVtOXTsKICAgICAgICBkb3VibGUgbWNbTl1bTl0gPSB7IDAuMCB9IDsKICAgICAgICDjgajlrprnvqnjgZXjgozjgabjgYTjgovjgILjgZPjga7jgajjgY3plqLmlbBtdWx044KS5qyh44Gu44KI44GG44Gr5ZG844Gz5Ye644GX44Gm6KGM5YiX44Gu56mNbWM9bWEg4oCiIG1iIOOCkuioiOeul+OBl+OBn+OBhOOAggogICAgICAgIG11bHQoKGRvdWJsZSDvvIopbWEsIChkb3VibGUg77yKKW1iLCAoZG91YmxlIO+8iiltYywgTik7CiAgICAgICAg6Zai5pWwbXVsdOOCkuODluODqeOCseODg+ODiOa8lOeul+WtkFtd44Gq44GX44Gn5pu444GN5LiK44GS44Gq44GV44GE44CCCiAgICAgICAgWzMuMV0gT1PvvJogdW5peAogICAgICAgIFszLjJdIOOCs+ODs+ODkeOCpOODqeWQjeOBqOODkOODvOOCuOODp+ODs++8miBnY2MKICAgICAgICBbMy4zXSDoqIDoqp7vvJogQwogICAgICAgIFs0XSDmnJ/pmZDvvJox5pyI44G+44Gn77yI5Ye65p2l44KM44Gw5pep44KB44Gr44GK6aGY44GE44GX44G+44GZ77yJIAoqLwojaW5jbHVkZTxzdGRpby5oPgoKI2RlZmluZSBOICAoMykKCnZvaWQgbXVsdChkb3VibGUgKm1hLCBkb3VibGUgKm1iLCBkb3VibGUgKm1jLCBpbnQgbikKewogICAgaW50IGksIGosIGs7CiAgICBkb3VibGUgc3VtOwogICAgZm9yIChqID0gMDsgaiA8IG47IGorKykgewogICAgICAgIGZvciAoaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgc3VtID0gMC4wOwogICAgICAgICAgICBmb3IgKGsgPSAwOyBrIDwgbjsgaysrKSB7CiAgICAgICAgICAgICAgICBzdW0gKz0gKihtYSArIGogKiBuICsgaykgKiAoKihtYiArIGsgKiBuICsgaSkpOwogICAgICAgICAgICB9CiAgICAgICAgICAgICoobWMgKyBqICogbiArIGkpID0gc3VtOwogICAgICAgIH0KICAgIH0KfQoKdm9pZCBwcmludF9tYXRyaXgoZG91YmxlICp4LCBjaGFyICpzKQp7CiAgICBpbnQgaSwgajsKICAgIHByaW50ZigiJXMgPVxuIiwgcyk7CiAgICBmb3IgKGogPSAwOyBqIDwgTjsgaisrKSB7CiAgICAgICAgcHJpbnRmKCIgIHwgIik7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IE47IGkrKykgewogICAgICAgICAgICBwcmludGYoIiVmICIsICooeCArIGogKiBOICsgaSkpOwogICAgICAgIH0KICAgICAgICBwcmludGYoInxcbiIpOwogICAgfQp9CgppbnQgbWFpbigpCnsKICAgIGRvdWJsZSBtYVtOXVtOXSA9IHsgezEuMCwgMi4wLCAtMS4wfSwgezMuMCwgMi4wLCAxLjB9LCB7MC4wLCAxLjAsIDIuMH0gfTsKICAgIGRvdWJsZSBtYltOXVtOXSA9IHsgezEuMCwgLTIuMCwgLTEuMH0sIHsxLjAsIDIuMCwgMS4wfSwgezMuMCwgMS4wLCAyLjB9IH07CiAgICBkb3VibGUgbWNbTl1bTl0gPSB7IDAuMCB9OwoKICAgIG11bHQoKGRvdWJsZSAqKSBtYSwgKGRvdWJsZSAqKSBtYiwgKGRvdWJsZSAqKSBtYywgTik7CiAgICBwcmludF9tYXRyaXgoKGRvdWJsZSAqKSBtYSwgIm1hIik7CiAgICBwcmludF9tYXRyaXgoKGRvdWJsZSAqKSBtYiwgIm1iIik7CiAgICBwcmludF9tYXRyaXgoKGRvdWJsZSAqKSBtYywgIm1jID0gbWEgKiBtYiIpOwoKICAgIHJldHVybiAwOwp9