有一个3×4的矩阵,要求编写程序找出每一行中最大值并与第一列交换。用C语言写
时间: 2024-06-10 14:05:47 浏览: 12
以下是使用C语言编写的程序:
```
#include <stdio.h>
int main() {
int matrix[3][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int i, j, max, maxIndex, temp;
for (i = 0; i < 3; i++) {
// 找出当前行的最大值及其所在列的下标
max = matrix[i][0];
maxIndex = 0;
for (j = 1; j < 4; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
maxIndex = j;
}
}
// 将最大值与第一列进行交换
temp = matrix[i][0];
matrix[i][0] = matrix[i][maxIndex];
matrix[i][maxIndex] = temp;
}
// 输出交换后的矩阵
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
```
程序中,我们首先定义一个3行4列的矩阵,并使用双重循环遍历矩阵,将每一行中的最大值与第一列进行交换。最后输出交换后的矩阵。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)