#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define M 2
#define N 2
int main(){
int a[M][N] = { 0 }, b[M][N] = { 0 }; //定义两个矩阵a和b
int sum[M][N] = { 0 }; //矩阵sum保存矩阵a和b的和
int product[M][N] = { 0 }; //矩阵product保存矩阵a和b的积
printf("请输入第一个矩阵:\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &a[i][j]);
}
}
printf("请输入第二个矩阵:\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &b[i][j]);
}
}
//计算矩阵a与矩阵b的和
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
//计算矩阵a与矩阵b的积
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
int temp = 0;
for (int k = 0; k < N; k++) {
temp += a[i][k] * b[k][j];
}
product[i][j] = temp;
}
}
//输出矩阵a与矩阵b的和
printf("矩阵之和:\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
//输出矩阵a与矩阵b的积
printf("矩阵之积:\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}
return 0;
}
标签:product,14,int,之积,sum,矩阵,++,printf
From: https://blog.csdn.net/m0_62314267/article/details/136805367