输入一个3×6的二维整数数组,输出其中最大值、最小值及其所在的行列下标。(用指针完成)
时间: 2024-02-03 18:13:07 浏览: 15
以下是代码实现:
```c++
#include <iostream>
using namespace std;
void findMaxMin(int arr[][6], int row, int col, int* maxVal, int* minVal, int* maxRow, int* maxCol, int* minRow, int* minCol) {
*maxVal = arr[0][0];
*minVal = arr[0][0];
*maxRow = 0;
*maxCol = 0;
*minRow = 0;
*minCol = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (arr[i][j] > *maxVal) {
*maxVal = arr[i][j];
*maxRow = i;
*maxCol = j;
}
if (arr[i][j] < *minVal) {
*minVal = arr[i][j];
*minRow = i;
*minCol = j;
}
}
}
}
int main() {
int arr[3][6] = {{1,2,3,4,5,6},
{7,8,9,10,11,12},
{13,14,15,16,17,18}};
int maxVal, minVal, maxRow, maxCol, minRow, minCol;
findMaxMin(arr, 3, 6, &maxVal, &minVal, &maxRow, &maxCol, &minRow, &minCol);
cout << "Max value: " << maxVal << " at (" << maxRow << ", " << maxCol << ")" << endl;
cout << "Min value: " << minVal << " at (" << minRow << ", " << minCol << ")" << endl;
return 0;
}
```
输出结果为:
```
Max value: 18 at (2, 5)
Min value: 1 at (0, 0)
```
阅读全文