输入一个四阶方阵,求原矩阵与其转置之和,输出结果。c语言编程
时间: 2023-06-13 21:08:47 浏览: 149
以下是求解原矩阵与其转置之和的C语言代码实现:
```c
#include <stdio.h>
#define N 4
int main() {
int mat[N][N], sum[N][N];
int i, j;
// Input matrix
printf("Enter a %dx%d matrix:\n", N, N);
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
scanf("%d", &mat[i][j]);
}
}
// Calculate sum of matrix and its transpose
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
sum[i][j] = mat[i][j] + mat[j][i];
}
}
// Print result
printf("The sum of the matrix and its transpose is:\n");
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
```
输入格式示例:
```
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
```
输出结果示例:
```
The sum of the matrix and its transpose is:
2 7 12 17
7 12 17 22
12 17 22 27
17 22 27 32
```
阅读全文