按如下函数原型编程从键盘输入一个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); 输入提示信息: “Input m, n:” "Input %d%d array:\n" 输入格式:"%d,%d" 输出提示信息和格式:“max = %d, row = %d, col = %d\n”
时间: 2024-02-06 12:12:30 浏览: 24
下面是按照给定函数原型编写的程序:
```
#include <iostream>
using namespace std;
void InputArray(int *p, int m, int n)
{
cout << "Input " << m << "x" << n << " array:" << endl;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin >> *(p + i * n + j);
}
}
}
int FindMax(int *p, int m, int n, int *pRow, int *pCol)
{
int maxVal = *p;
*pRow = 0;
*pCol = 0;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(*(p + i * n + j) > maxVal)
{
maxVal = *(p + i * n + j);
*pRow = i;
*pCol = j;
}
}
}
return maxVal;
}
int main()
{
int m, n;
int arr[10][10];
int *p = &arr[0][0];
int maxVal, maxRow, maxCol;
cout << "Input m, n:" << endl;
cin >> m >> n;
InputArray(p, m, n);
maxVal = FindMax(p, m, n, &maxRow, &maxCol);
cout << "max = " << maxVal << ", row = " << maxRow << ", col = " << maxCol << endl;
return 0;
}
```
在上面的代码中,函数InputArray通过指针p遍历二维数组,并利用cin从键盘输入数组元素的值。函数FindMax首先将数组中第一个元素的值设为最大值,然后遍历整个数组,如果发现比当前最大值更大的元素,就将最大值更新为该元素的值,并记录该元素的行列下标。最后,主函数输出最大值及其所在的行列下标。需要注意的是,二维数组在内存中是按行存储的,因此要通过指针p来访问二维数组的元素。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)