c++中用二维数组,循环语句求最大值

如题所述

下面是一个示例代码,演示如何使用二维数组和循环语句求二维数组中的最大值。
```c++
#include
using namespace std;
const int ROW = 2; // 行数
const int COL = 3; // 列数
int main() {
int arr[ROW][COL] = {{5, 8, 6}, {9, 2, 4}}; // 定义二维数组并初始化
int max_value = arr[0][0]; // 初始化最大值为数组第一个元素
for (int i = 0; i < ROW; i++) { // 遍历行
for (int j = 0; j < COL; j++) { // 遍历列
if (arr[i][j] > max_value) { // 如果当前元素比最大值大
max_value = arr[i][j]; // 更新最大值
}
}
}
cout << "The max value in the array is: " << max_value << endl;
return 0;
}
```
输出:
```
The max value in the array is: 9
```
温馨提示:答案为网友推荐,仅供参考