这是一道编程题。题目要求 main 函数三次调用 fun,每次输入一个数组整数,最后输出这个数组中最大值、最小值、总和以及平均值。
时间: 2024-05-13 18:19:32 浏览: 101
好的,这道题比较简单,下面是代码实现:
```c++
#include <iostream>
using namespace std;
void fun(int arr[], int size) {
int maxVal = arr[0], minVal = arr[0], sum = 0;
for (int i = 0; i < size; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
if (arr[i] < minVal) {
minVal = arr[i];
}
sum += arr[i];
}
double avg = (double)sum / size;
cout << "max value: " << maxVal << endl;
cout << "min value: " << minVal << endl;
cout << "sum: " << sum << endl;
cout << "average: " << avg << endl;
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {10, 20, 30};
int arr3[] = {-5, 0, 5, 10};
fun(arr1, 5);
fun(arr2, 3);
fun(arr3, 4);
return 0;
}
```
输出结果如下:
```
max value: 5
min value: 1
sum: 15
average: 3
max value: 30
min value: 10
sum: 60
average: 20
max value: 10
min value: -5
sum: 10
average: 2.5
```
阅读全文