c语言从键盘上输入两个2*3的矩阵求和
时间: 2023-11-26 20:03:53 浏览: 78
C语言程序设计-求5行5列矩阵的主、副对角线上元素之和;注意,两条对角线相交的元素只加一次;例如:主函数中给出的矩阵的两
5星 · 资源好评率100%
以下是从键盘上输入两个2*3的矩阵并求和的C语言代码示例:
```
#include <stdio.h>
int main() {
int A[2][3], B[2][3], C[2][3]; // 定义两个2*3矩阵和它们的和矩阵C
// 输入第一个矩阵A
printf("Enter the elements of matrix A:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &A[i][j]);
}
}
// 输入第二个矩阵B
printf("Enter the elements of matrix B:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &B[i][j]);
}
}
// 计算和矩阵C
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// 输出和矩阵C
printf("The sum of the two matrices is:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
```
运行示例:
```
Enter the elements of matrix A:
1 2 3
4 5 6
Enter the elements of matrix B:
6 5 4
3 2 1
The sum of the two matrices is:
7 7 7
7 7 7
```
阅读全文