💻

行列の計算

📌
n×3 型の行列Aと3×m型の行列B の積を計算するプログラムを作成せよ。更に次の行列の積を求めよ.

Code:行列を固定する場合

#include <stdio.h> #include <math.h> typedef struct { int row; int col; } matSize; int main() { int matA[5][3] = { {12, 0, -2}, {14, 2, 7}, {1, 2, 5}, {-4, 6, -8}, {3, 2, 1}}; int matB[3][4] = { {1, 1, 2, 1}, {1, 4, 5, -2}, {-3, 13, 19, 0}}; int matSum[5][4] = {0}; matSize matASize = {sizeof(matA) / sizeof(*matA), sizeof(matA[0]) / sizeof(matA[0][0])}; matSize matBSize = {sizeof(matB) / sizeof(*matB), sizeof(matB[0]) / sizeof(matB[0][0])}; printf("------------------\n"); printf("matASize: %d x %d\n", matASize.row, matASize.col); for (int i = 0; i < matASize.row; i++) { for (int j = 0; j < matASize.col; j++) { printf("%d \t", matA[i][j]); } printf("\n"); } printf("------------------\n"); printf("matBSize: %d x %d\n", matBSize.row, matBSize.col); for (int i = 0; i < matBSize.row; i++) { for (int j = 0; j < matBSize.col; j++) { printf("%d \t", matB[i][j]); } printf("\n"); } for (int i = 0; i < matASize.row; i++) { for (int j = 0; j < matBSize.col; j++) { for (int k = 0; k < matASize.col; k++) { matSum[i][j] += matA[i][k] * matB[k][j]; } } } // print matSum printf("------------------\nmatSum:\n"); for (int i = 0; i < 5; i++) { for (int j = 0; j < 4; j++) { printf("%d \t", matSum[i][j]); } printf("\n"); } return 0; }

output

------------------ matASize: 5 x 3 12 0 -2 14 2 7 1 2 5 -4 6 -8 3 2 1 ------------------ matBSize: 3 x 4 1 1 2 1 1 4 5 -2 -3 13 19 0 ------------------ matSum: 18 -14 -14 12 -5 113 171 10 -12 74 107 -3 26 -84 -130 -16 2 24 35 -1

Code:行列を入力する場合

#include <stdio.h> #include <math.h> typedef struct { int row; int col; } matSize; // int matA[5][3] = { // {12, 0, -2}, // {14, 2, 7}, // {1, 2, 5}, // {-4, 6, -8}, // {3, 2, 1}}; // int matB[3][4] = { // {1, 1, 2, 1}, // {1, 4, 5, -2}, // {-3, 13, 19, 0}}; #define MAX_SIZE 16 int matA[MAX_SIZE][MAX_SIZE] = {1}; int matB[MAX_SIZE][MAX_SIZE] = {1}; int matSum[MAX_SIZE][MAX_SIZE] = {1}; int main() { //初期値 matSize matASize = {sizeof(matA) / sizeof(*matA), sizeof(matA[0]) / sizeof(matA[0][0])}; matSize matBSize = {sizeof(matB) / sizeof(*matB), sizeof(matB[0]) / sizeof(matB[0][0])}; printf("type your matix A size (row col):\n"); scanf("%d %d", &matASize.row, &matASize.col); printf("matrix A: %d x %d\n", matASize.row, matASize.col); printf("type your matix B size (row col):\n"); scanf("%d %d", &matBSize.row, &matBSize.col); printf("matrix B: %d x %d\n", matBSize.row, matBSize.col); for (int i = 0; i < matASize.row; i++) { for (int j = 0; j < matASize.col; j++) { printf("A[%d][%D] : ", i + 1, j + 1); scanf("%d", &matA[i][j]); } } for (int i = 0; i < matBSize.row; i++) { for (int j = 0; j < matBSize.col; j++) { printf("B[%d][%D] : ", i + 1, j + 1); scanf("%d", &matB[i][j]); } } printf("------------------\n"); printf("matASize: %d x %d\n", matASize.row, matASize.col); for (int i = 0; i < matASize.row; i++) { for (int j = 0; j < matASize.col; j++) { printf("%d \t", matA[i][j]); } printf("\n"); } printf("------------------\n"); printf("matBSize: %d x %d\n", matBSize.row, matBSize.col); for (int i = 0; i < matBSize.row; i++) { for (int j = 0; j < matBSize.col; j++) { printf("%d \t", matB[i][j]); } printf("\n"); } for (int i = 0; i < matASize.row; i++) { for (int j = 0; j < matBSize.col; j++) { for (int k = 0; k < matASize.col; k++) { matSum[i][j] += matA[i][k] * matB[k][j]; } } } // print matSum printf("------------------\nmatSum:\n"); for (int i = 0; i < 5; i++) { for (int j = 0; j < 4; j++) { printf("%d \t", matSum[i][j]); } printf("\n"); } return 0; }

output

type your matix A size (row col): 5 3 matrix A: 5 x 3 type your matix B size (row col): 3 4 matrix B: 3 x 4 A[1][1] : 12 A[1][2] : 0 A[1][3] : -2 A[2][1] : 14 A[2][2] : 2 A[2][3] : 7 A[3][1] : 1 A[3][2] : 2 A[3][3] : 5 A[4][1] : -3 A[4][2] : 6 A[4][3] : -8 A[5][1] : 3 A[5][2] : 2 A[5][3] : 1 B[1][1] : 1 B[1][2] : 1 B[1][3] : 2 B[1][4] : 1 B[2][1] : 1 B[2][2] : 4 B[2][3] : 5 B[2][4] : -2 B[3][1] : -3 B[3][2] : 13 B[3][3] : 19 B[3][4] : 0 ------------------ matASize: 5 x 3 12 0 -2 14 2 7 1 2 5 -3 6 -8 3 2 1 ------------------ matBSize: 3 x 4 1 1 2 1 1 4 5 -2 -3 13 19 0 ------------------ matSum: 19 -14 -14 12 -5 113 171 10 -12 74 107 -3 27 -83 -128 -15 2 24 35 -1