Python Return the \"centered\" average of an array of ints, which we\'ll say is
ID: 3914666 • Letter: P
Question
Python
Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.
centered_average([1, 2, 3, 4, 100]) ? 3
centered_average([1, 1, 5, 5, 10, 8, 7]) ? 5
centered_average([-10, -4, -2, -4, -2, 0]) ? -3
Example:
def centered_average(nums):
Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.
centered_average([1, 2, 3, 4, 100]) ? 3
centered_average([1, 1, 5, 5, 10, 8, 7]) ? 5
centered_average([-10, -4, -2, -4, -2, 0]) ? -3
Example:
def centered_average(nums):
Explanation / Answer
Run this code in c++. you can use any c++ compiler. This code will run well.
#include <iostream>
using namespace std;
int main()
{
int i,j,c,sum=0,avg;
int n;
cout<<"Enter the number of elements in an array: ";
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
c=a[j+1];
a[j+1]=a[j];
a[j]=c;
}
}
}
for(i=1;i<n-1;i++)
{
sum=sum+a[i];
}
cout<<"Centered Average: ";
avg=sum/(n-2);
cout<<avg;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.