C语言 (2) 在main函数中输入一个4x4的矩阵,用二维数组表示,编写一函数将该矩阵转置,并将转置后的结果在main函数中输出,并将该矩阵的最大值和最小值通过指形参指针传回主函数;
时间: 2024-02-01 15:12:14 浏览: 95
以下是实现该功能的C语言代码:
```c
#include <stdio.h>
void transpose(int matrix[4][4], int *max, int *min)
{
int i, j, temp;
*max = matrix[0][0];
*min = matrix[0][0];
for (i = 0; i < 4; i++) {
for (j = i+1; j < 4; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
if (temp > *max) {
*max = temp;
} else if (temp < *min) {
*min = temp;
}
}
}
}
int main()
{
int matrix[4][4], i, j, max, min;
printf("请输入4x4的矩阵:\n");
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
scanf("%d", &matrix[i][j]);
}
}
transpose(matrix, &max, &min);
printf("转置后的矩阵为:\n");
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
printf("最大值为:%d\n", max);
printf("最小值为:%d\n", min);
return 0;
}
```
在该代码中,我们首先定义了一个`transpose`函数,用来将传入的矩阵进行转置,并通过指针形参`max`和`min`返回矩阵的最大值和最小值。
在`main`函数中,我们先输入一个4x4的矩阵,然后调用`transpose`函数将其转置。最后输出转置后的矩阵,并打印出最大值和最小值。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)