C++数组实验——输出矩阵最大值、最小值及其对应的位置
时间: 2023-09-27 21:04:59 浏览: 242
好的,这是一个关于C语言的数组实验问题,我可以给您提供以下的代码实现:
```c
#include <stdio.h>
int main()
{
int matrix[3][4] = {
{4, 6, 7, 2},
{9, 3, 1, 5},
{8, 2, 4, 6}
};
int max_val = matrix[0][0]; // 初始化为矩阵中的第一个元素
int min_val = matrix[0][0];
int max_row = 0, max_col = 0;
int min_row = 0, min_col = 0;
// 遍历矩阵,找到最大值和最小值以及对应位置
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
if (matrix[i][j] > max_val) {
max_val = matrix[i][j];
max_row = i;
max_col = j;
}
if (matrix[i][j] < min_val) {
min_val = matrix[i][j];
min_row = i;
min_col = j;
}
}
}
// 输出结果
printf("max_val = %d, at (%d, %d)\n", max_val, max_row, max_col);
printf("min_val = %d, at (%d, %d)\n", min_val, min_row, min_col);
return 0;
}
```
这段代码会输出矩阵中的最大值、最小值以及对应的位置。如果您对此有任何疑问,请随时向我提出。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)