找数组最值 按如下函数原型编程从键盘输入一个m行n列的二维数组,然后计算数组中元素的最大值及其所在的行列下标值。其中,m和n的值由用户键盘输入。已知m和n的值都不超过10。 void InputArray(int *p, int m, int n); int FindMax(int *p, int m, int n, int *pRow, int *pCol);//函数返回最大值,pRow和pCol分别返回最大值所在的行列下标 例如,程序的1次运行结果如下: Input m,n: 3,4↙ Input
时间: 2024-02-27 22:55:16 浏览: 87
好的,下面是实现找数组最值的示例代码:
```c
#include <stdio.h>
void InputArray(int *p, int m, int n);
int FindMax(int *p, int m, int n, int *pRow, int *pCol);
int main() {
int m, n;
printf("Input m,n: ");
scanf("%d,%d", &m, &n);
int arr[m][n];
InputArray(&arr[0][0], m, n);
int max, row, col;
max = FindMax(&arr[0][0], m, n, &row, &col);
printf("The max number is %d, located in row %d, column %d.\n", max, row+1, col+1);
return 0;
}
void InputArray(int *p, int m, int n) {
printf("Please enter %d numbers:\n", m * n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", p + i * n + j);
}
}
}
int FindMax(int *p, int m, int n, int *pRow, int *pCol) {
int max = *(p + 0);
*pRow = 0;
*pCol = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (*(p + i * n + j) > max) {
max = *(p + i * n + j);
*pRow = i;
*pCol = j;
}
}
}
return max;
}
```
运行结果如下:
```
Input m,n: 3,4
Please enter 12 numbers:
1 2 3 4 5 6 7 8 9 10 11 12
The max number is 12, located in row 3, column 4.
```
阅读全文