+ 1
How do i exclude min and max values from an array considering there are 5 values. I want to ignore the max n min value and take the average of the rest 3.
4 ответов
+ 5
This is how you can do it:
#include <iostream>
using namespace std;
int main() {
int arr[5];
int max,min,sum=0,avg;
for(int i=0;i<5;i++)
cin>>arr[i];
max=arr[0];
min=arr[0];
for(int i=0;i<5;i++)
{
if(arr[i]>max)
{
max=arr[i];
}
if(arr[i]<min)
{
min=arr[i];
}
sum+=arr[i];
}
sum=sum-(max+min);
avg=sum/3;
cout<<"avg is"<<avg;
return 0;
}
+ 1
How does your program behave, if you have multiple maximum or minimum values? In a valid case all values are the same, so all values are minimum and maximum. Therefore no value would be used for the calculation...
0
#include<iostream>
using namespace std;
int main()
{ int arr[5],max=0,min=0,i=0,sum=0;
float avg;
while (i!=5)
{
cin>>arr[i];
if (i==0)min = arr[i];
if (arr[i]>max) max = arr[i];
else if (arr[i]<min) min = arr[i];
i++;
}
i-- ;
while(i>=0)
{
sum = sum + arr [i];
i-- ;
}
sum=sum - max- min;
avg = sum /3.00;
return 0;
}
- 1
Hmm i was going to do bubble sort and then start my for loop with i=1 and finish with size-1 that will exclude the min n max I think