去掉最高值,去掉最低值,求平均值 用void Max(const int *arr,int len,int *max){} void Min(const int *arr,int len,int *min){} void Sum(const int *arr,int len,int *sum){} void Avg(int sum,int max,int min,int len,float *avg){} void test03(){ int arr[]={1,2,5,4,3,6,7}; int len =sizeof (arr)/sizeof (int); }
时间: 2023-08-28 09:08:58 浏览: 81
可以使用以下代码实现:
```
#include <iostream>
using namespace std;
void Max(const int *arr, int len, int *max) {
*max = arr[0];
for (int i = 1; i < len; i++) {
if (arr[i] > *max) {
*max = arr[i];
}
}
}
void Min(const int *arr, int len, int *min) {
*min = arr[0];
for (int i = 1; i < len; i++) {
if (arr[i] < *min) {
*min = arr[i];
}
}
}
void Sum(const int *arr, int len, int *sum) {
*sum = 0;
for (int i = 0; i < len; i++) {
*sum += arr[i];
}
}
void Avg(int sum, int max, int min, int len, float *avg) {
*avg = (float)(sum - max - min) / (len - 2);
}
void test03() {
int arr[] = {1, 2, 5, 4, 3, 6, 7};
int len = sizeof(arr) / sizeof(int);
int max, min, sum;
float avg;
Max(arr, len, &max);
Min(arr, len, &min);
Sum(arr, len, &sum);
Avg(sum, max, min, len, &avg);
cout << "Max: " << max << endl;
cout << "Min: " << min << endl;
cout << "Sum: " << sum << endl;
cout << "Avg: " << avg << endl;
}
int main() {
test03();
return 0;
}
```
运行结果为:
```
Max: 7
Min: 2
Sum: 28
Avg: 3.5
```
阅读全文