/*
C/C++の宿題片付けます 161代目
http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1354070278/
386 名前:デフォルトの名無しさん [aa]: 2012/12/17(月) 18:24:30.48
二つの2×3型行列を
A=1.1 -2.2 0.9
-0.2 2.7 0.3
B= 1.8 0.5 1.3
-0.4 0.6 -0.3
と定義する。
387 名前:デフォルトの名無しさん [sd]: 2012/12/17(月) 18:26:36.33
これらの二つの行列の宣言、初期化し、C≡A+B C≡A-Bを算出して
2×3型の表形式で出力するプログラムを作成せよ。ただし、行列CとDは固定長
配列とし、関数内の演算を行うこと
上記の問題と同じように行列A、Bを用いて、積C≡ABt を算出し、2×2型の表
形式で出力せよ。ただし、Bの転置行列、Btはプログラム中に直接記述するのでは
なく、Bより作成すること。また、Cはnew演算子によりメモリを確保し、関数内で
演算を行うこと。
*/
#include <stdio.h>
#define MM (2)
#define NN (3)
/* M×N列 表示 */
void m_print_MN(double mx[MM][NN])
{
int i, j;
for (i = 0; i < MM; i++) {
printf(" | ");
for (j = 0; j < NN; j++) {
printf("%f ", mx[i][j]);
}
printf("|\n");
}
}
/* pointer行列 表示 */
void m_print_ptr(double **mx, int m, int n)
{
int i, j;
for (i = 0; i < m; i++) {
printf(" | ");
for (j = 0; j < n; j++) {
printf("%f ", mx[i][j]);
}
printf("|\n");
}
}
/* 行列確保(new) */
double **m_alloc(int m, int n)
{
int i;
double **pma;
pma = new double *[m];
pma[0] = new double[m * n];
for (i = 1; i < m; i++) {
pma[i] = pma[0] + n * i;
}
return pma;
}
/* 行列解放(delete) */
void m_free(double **pma)
{
delete[]pma[0];
delete[]pma;
}
/* 加算 */
double **m_add(double s1[MM][NN], double s2[MM][NN])
{
int i, j;
double **dst;
dst = m_alloc(MM, NN);
for (i = 0; i < MM; i++) {
for (j = 0; j < NN; j++) {
dst[i][j] = s1[i][j] + s2[i][j];
}
}
return dst;
}
/* 減算 */
double **m_sub(double s1[MM][NN], double s2[MM][NN])
{
int i, j;
double **dst;
dst = m_alloc(MM, NN);
for (i = 0; i < MM; i++) {
for (j = 0; j < NN; j++) {
dst[i][j] = s1[i][j] - s2[i][j];
}
}
return dst;
}
/* 転置乗算 */
double **m_mul_Bt(double ma[MM][NN], double mb[MM][NN])
{
int i, j, k;
double **bt = m_alloc(NN, MM);
double **dst = m_alloc(MM, MM);
// Bt
printf("Bt =\n");
for (i = 0; i < MM; i++) {
for (j = 0; j < NN; j++) {
bt[j][i] = mb[i][j];
}
}
m_print_ptr(bt, NN, MM);
// mul
for (i = 0; i < MM; i++) {
for (j = 0; j < MM; j++) {
dst[i][j] = 0.0;
for (k = 0; k < NN; k++) {
dst[i][j] += ma[i][k] * bt[k][j];
}
}
}
// end
m_free(bt);
return dst;
}
/* */
int main()
{
int i;
double mA[MM][NN] = {
{1.1, -2.2, 0.9},
{-0.2, 2.7, 0.3}
};
double mB[MM][NN] = {
{1.8, 0.5, 1.3},
{-0.4, 0.6, -0.3}
};
double **mAB_add, **mAB_sub;
double **mABt_mul;
// A, B
printf("A =\n");
m_print_MN(mA);
printf("B =\n");
m_print_MN(mB);
// A + B
mAB_add = m_add(mA, mB);
printf("A + B =\n");
m_print_ptr(mAB_add, MM, NN);
// A - B
mAB_sub = m_sub(mA, mB);
printf("A - B =\n");
m_print_ptr(mAB_sub, MM, NN);
// A × Bt
mABt_mul = m_mul_Bt(mA, mB);
printf("A × Bt =\n");
m_print_ptr(mABt_mul, MM, MM);
// end
m_free(mABt_mul);
m_free(mAB_sub);
m_free(mAB_add);
return 0;
}
LyoKICAgIEMvQysr44Gu5a6/6aGM54mH5LuY44GR44G+44GZIDE2MeS7o+ebrgogICAgaHR0cDovL3QuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmgubmV0L3Rlc3QvcmVhZC5jZ2kvdGVjaC8xMzU0MDcwMjc4LwogICAgICAgIDM4NiDlkI3liY3vvJrjg4fjg5Xjgqnjg6vjg4jjga7lkI3nhKHjgZfjgZXjgpMgW2FhXe+8miAyMDEyLzEyLzE3KOaciCkgMTg6MjQ6MzAuNDgKICAgICAgICDkuozjgaTjga7vvJLDl++8k+Wei+ihjOWIl+OCkgogICAgICAgIEHvvJ0xLjHjgIAtMi4y44CAMC45CiAgICAgICAgICAtMC4y44CAIDIuNyAwLjMKICAgICAgICBCPSAxLjggMC41IDEuMwogICAgICAgIC0wLjQgMC42IC0wLjMKICAgICAgICDjgajlrprnvqnjgZnjgovjgIIKCiAgICAgICAgMzg3IOWQjeWJje+8muODh+ODleOCqeODq+ODiOOBruWQjeeEoeOBl+OBleOCkyBb772T772EXe+8miAyMDEyLzEyLzE3KOaciCkgMTg6MjY6MzYuMzMKICAgICAgICDjgZPjgozjgonjga7kuozjgaTjga7ooYzliJfjga7lrqPoqIDjgIHliJ3mnJ/ljJbjgZfjgIFD4omhQStC44CAQ+KJoUEtQuOCkueul+WHuuOBl+OBpgogICAgICAgIO+8ksOX77yT5Z6L44Gu6KGo5b2i5byP44Gn5Ye65Yqb44GZ44KL44OX44Ot44Kw44Op44Og44KS5L2c5oiQ44Gb44KI44CC44Gf44Gg44GX44CB6KGM5YiXQ+OBqETjga/lm7rlrprplbcKICAgICAgICDphY3liJfjgajjgZfjgIHplqLmlbDlhoXjga7mvJTnrpfjgpLooYzjgYbjgZPjgagKCiAgICAgICAg5LiK6KiY44Gu5ZWP6aGM44Go5ZCM44GY44KI44GG44Gr6KGM5YiXQeOAgULjgpLnlKjjgYTjgabjgIHnqY1D4omhQULvvZTjgIDjgpLnrpflh7rjgZfjgIHvvJLDl++8kuWei+OBruihqAogICAgICAgIOW9ouW8j+OBp+WHuuWKm+OBm+OCiOOAguOBn+OBoOOBl+OAgULjga7ou6Lnva7ooYzliJfjgIFCdOOBr+ODl+ODreOCsOODqeODoOS4reOBq+ebtOaOpeiomOi/sOOBmeOCi+OBruOBp+OBrwogICAgICAgIOOBquOBj+OAgULjgojjgorkvZzmiJDjgZnjgovjgZPjgajjgILjgb7jgZ/jgIFD44GvbmV35ryU566X5a2Q44Gr44KI44KK44Oh44Oi44Oq44KS56K65L+d44GX44CB6Zai5pWw5YaF44GnCiAgICAgICAg5ryU566X44KS6KGM44GG44GT44Go44CCCiAqLwojaW5jbHVkZSA8c3RkaW8uaD4KCiNkZWZpbmUgTU0gKDIpCiNkZWZpbmUgTk4gKDMpCgovKiBNw5dO5YiXIOihqOekuiAqLwp2b2lkIG1fcHJpbnRfTU4oZG91YmxlIG14W01NXVtOTl0pCnsKICAgIGludCBpLCBqOwogICAgZm9yIChpID0gMDsgaSA8IE1NOyBpKyspIHsKICAgICAgICBwcmludGYoIiAgfCAiKTsKICAgICAgICBmb3IgKGogPSAwOyBqIDwgTk47IGorKykgewogICAgICAgICAgICBwcmludGYoIiVmICIsIG14W2ldW2pdKTsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCJ8XG4iKTsKICAgIH0KfQoKLyogcG9pbnRlcuihjOWIlyDooajnpLogKi8Kdm9pZCBtX3ByaW50X3B0cihkb3VibGUgKipteCwgaW50IG0sIGludCBuKQp7CiAgICBpbnQgaSwgajsKICAgIGZvciAoaSA9IDA7IGkgPCBtOyBpKyspIHsKICAgICAgICBwcmludGYoIiAgfCAiKTsKICAgICAgICBmb3IgKGogPSAwOyBqIDwgbjsgaisrKSB7CiAgICAgICAgICAgIHByaW50ZigiJWYgIiwgbXhbaV1bal0pOwogICAgICAgIH0KICAgICAgICBwcmludGYoInxcbiIpOwogICAgfQp9CgovKiDooYzliJfnorrkv50obmV3KSAqLwpkb3VibGUgKiptX2FsbG9jKGludCBtLCBpbnQgbikKewogICAgaW50IGk7CiAgICBkb3VibGUgKipwbWE7CiAgICBwbWEgPSBuZXcgZG91YmxlICpbbV07CiAgICBwbWFbMF0gPSBuZXcgZG91YmxlW20gKiBuXTsKICAgIGZvciAoaSA9IDE7IGkgPCBtOyBpKyspIHsKICAgICAgICBwbWFbaV0gPSBwbWFbMF0gKyBuICogaTsKICAgIH0KICAgIHJldHVybiBwbWE7Cn0KCi8qIOihjOWIl+ino+aUvihkZWxldGUpICovCnZvaWQgbV9mcmVlKGRvdWJsZSAqKnBtYSkKewogICAgZGVsZXRlW11wbWFbMF07CiAgICBkZWxldGVbXXBtYTsKfQoKLyog5Yqg566XICovCmRvdWJsZSAqKm1fYWRkKGRvdWJsZSBzMVtNTV1bTk5dLCBkb3VibGUgczJbTU1dW05OXSkKewogICAgaW50IGksIGo7CiAgICBkb3VibGUgKipkc3Q7CiAgICBkc3QgPSBtX2FsbG9jKE1NLCBOTik7CiAgICBmb3IgKGkgPSAwOyBpIDwgTU07IGkrKykgewogICAgICAgIGZvciAoaiA9IDA7IGogPCBOTjsgaisrKSB7CiAgICAgICAgICAgIGRzdFtpXVtqXSA9IHMxW2ldW2pdICsgczJbaV1bal07CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIGRzdDsKfQoKLyog5rib566XICovCmRvdWJsZSAqKm1fc3ViKGRvdWJsZSBzMVtNTV1bTk5dLCBkb3VibGUgczJbTU1dW05OXSkKewogICAgaW50IGksIGo7CiAgICBkb3VibGUgKipkc3Q7CiAgICBkc3QgPSBtX2FsbG9jKE1NLCBOTik7CiAgICBmb3IgKGkgPSAwOyBpIDwgTU07IGkrKykgewogICAgICAgIGZvciAoaiA9IDA7IGogPCBOTjsgaisrKSB7CiAgICAgICAgICAgIGRzdFtpXVtqXSA9IHMxW2ldW2pdIC0gczJbaV1bal07CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIGRzdDsKfQoKLyog6Lui572u5LmX566XICovCmRvdWJsZSAqKm1fbXVsX0J0KGRvdWJsZSBtYVtNTV1bTk5dLCBkb3VibGUgbWJbTU1dW05OXSkKewogICAgaW50IGksIGosIGs7CiAgICBkb3VibGUgKipidCA9IG1fYWxsb2MoTk4sIE1NKTsKICAgIGRvdWJsZSAqKmRzdCA9IG1fYWxsb2MoTU0sIE1NKTsKICAgIC8vIEJ0CiAgICBwcmludGYoIkJ0ID1cbiIpOwogICAgZm9yIChpID0gMDsgaSA8IE1NOyBpKyspIHsKICAgICAgICBmb3IgKGogPSAwOyBqIDwgTk47IGorKykgewogICAgICAgICAgICBidFtqXVtpXSA9IG1iW2ldW2pdOwogICAgICAgIH0KICAgIH0KICAgIG1fcHJpbnRfcHRyKGJ0LCBOTiwgTU0pOwogICAgLy8gbXVsCiAgICBmb3IgKGkgPSAwOyBpIDwgTU07IGkrKykgewogICAgICAgIGZvciAoaiA9IDA7IGogPCBNTTsgaisrKSB7CiAgICAgICAgICAgIGRzdFtpXVtqXSA9IDAuMDsKICAgICAgICAgICAgZm9yIChrID0gMDsgayA8IE5OOyBrKyspIHsKICAgICAgICAgICAgICAgIGRzdFtpXVtqXSArPSBtYVtpXVtrXSAqIGJ0W2tdW2pdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgLy8gZW5kCiAgICBtX2ZyZWUoYnQpOwogICAgcmV0dXJuIGRzdDsKfQoKLyogKi8KaW50IG1haW4oKQp7CiAgICBpbnQgaTsKCiAgICBkb3VibGUgbUFbTU1dW05OXSA9IHsKICAgICAgICB7MS4xLCAtMi4yLCAwLjl9LAogICAgICAgIHstMC4yLCAyLjcsIDAuM30KICAgIH07CiAgICBkb3VibGUgbUJbTU1dW05OXSA9IHsKICAgICAgICB7MS44LCAwLjUsIDEuM30sCiAgICAgICAgey0wLjQsIDAuNiwgLTAuM30KICAgIH07CiAgICBkb3VibGUgKiptQUJfYWRkLCAqKm1BQl9zdWI7CiAgICBkb3VibGUgKiptQUJ0X211bDsKCiAgICAvLyBBLCBCCiAgICBwcmludGYoIkEgPVxuIik7CiAgICBtX3ByaW50X01OKG1BKTsKICAgIHByaW50ZigiQiA9XG4iKTsKICAgIG1fcHJpbnRfTU4obUIpOwogICAgLy8gQSArIEIKICAgIG1BQl9hZGQgPSBtX2FkZChtQSwgbUIpOwogICAgcHJpbnRmKCJBICsgQiA9XG4iKTsKICAgIG1fcHJpbnRfcHRyKG1BQl9hZGQsIE1NLCBOTik7CiAgICAvLyBBIC0gQgogICAgbUFCX3N1YiA9IG1fc3ViKG1BLCBtQik7CiAgICBwcmludGYoIkEgLSBCID1cbiIpOwogICAgbV9wcmludF9wdHIobUFCX3N1YiwgTU0sIE5OKTsKICAgIC8vIEEgw5cgQnQKICAgIG1BQnRfbXVsID0gbV9tdWxfQnQobUEsIG1CKTsKICAgIHByaW50ZigiQSDDlyBCdCA9XG4iKTsKICAgIG1fcHJpbnRfcHRyKG1BQnRfbXVsLCBNTSwgTU0pOwoKICAgIC8vIGVuZAogICAgbV9mcmVlKG1BQnRfbXVsKTsKICAgIG1fZnJlZShtQUJfc3ViKTsKICAgIG1fZnJlZShtQUJfYWRkKTsKICAgIHJldHVybiAwOwp9Cg==