指针法输出十个整形数组中最大值最小值
时间: 2023-11-03 15:10:27 浏览: 93
可以通过指针遍历数组,同时记录最大值和最小值,来输出十个整形数组中的最大值和最小值。具体实现如下:
```c++
#include <iostream>
using namespace std;
int main() {
int arr[10][10] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36, 37, 38, 39, 40},
{41, 42, 43, 44, 45, 46, 47, 48, 49, 50},
{51, 52, 53, 54, 55, 56, 57, 58, 59, 60},
{61, 62, 63, 64, 65, 66, 67, 68, 69, 70},
{71, 72, 73, 74, 75, 76, 77, 78, 79, 80},
{81, 82, 83, 84, 85, 86, 87, 88, 89, 90},
{91, 92, 93, 94, 95, 96, 97, 98, 99, 100} };
int *p = arr[0];
int max_val = *p;
int min_val = *p;
for(int i = 0; i < 10 * 10; i++) {
if(*(p+i) > max_val) {
max_val = *(p+i);
}
if(*(p+i) < min_val) {
min_val = *(p+i);
}
}
cout << "max value: " << max_val << endl;
cout << "min value: " << min_val << endl;
return 0;
}
```
这里定义了一个大小为10x10的二维数组arr,并将其首地址赋值给指针p,然后通过指针p遍历整个数组,同时记录最大值和最小值。最后输出最大值和最小值即可。
阅读全文