按如下函数原型编程计算并输出n×n阶矩阵的转置矩阵。其中,n由用户从键盘输入。已知n值不超过10。 void Transpose(int (*a)[N], int n); void Swap(int *x, int *y); void InputMatrix(int (*a)[N], int n); void PrintMatrix(int (*a)[N], int n); 输入提示信息:"Input n:" 输入格式:"%d" 输入提示信息:"Input %d*%d matrix:\n" 输出提示信息:"The transposed matrix is:\n" 输出格式:"%d\t"
时间: 2023-11-27 22:50:51 浏览: 53
矩阵的转置
以下是代码实现:
```c
#include <stdio.h>
#define N 10
void Swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
void Transpose(int (*a)[N], int n) {
int i, j;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
Swap(&a[i][j], &a[j][i]);
}
}
}
void InputMatrix(int (*a)[N], int n) {
int i, j;
printf("Input %d*%d matrix:\n", n, n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
}
void PrintMatrix(int (*a)[N], int n) {
int i, j;
printf("The transposed matrix is:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
int main() {
int matrix[N][N], n;
printf("Input n:");
scanf("%d", &n);
InputMatrix(matrix, n);
Transpose(matrix, n);
PrintMatrix(matrix, n);
return 0;
}
```
输入输出示例:
```
Input n:3
Input 3*3 matrix:
1 2 3
4 5 6
7 8 9
The transposed matrix is:
1 4 7
2 5 8
3 6 9
```
阅读全文