In: Computer Science
Write a C++ function that receives an integer array along with its specified length. The function will replace only one of the smallest integers and one of the largest integers by 0. Then the function will return the average for the rest of the values in the array.
Test case array: 3, 8, 2, 6, 5, 3, 4, 7, 8, 2, 10, 7
Function should return: 5.3
SOURCE CODE
#include <iostream>
using namespace std;
int main()
{
int n, i, max=0, min=999999;
float num[100], sum=0.0, average;
cout << "Enter the numbers of data: ";
cin >> n;
for(i = 0; i < n; ++i)
{
cout << i + 1 << ". Enter number: ";
cin >> num[i];
if(num[i] > max) max = num[i];
if(num[i] < min) min = num[i];
sum += num[i];
}
sum = sum - max - min;
average = sum / (n-2);
cout << "Average = " << average;
return 0;
}
SCREENSHOT
please give a upvote if u feel helpful