+ 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.

25th Oct 2016, 3:00 PM
Arshi Singh
Arshi Singh - avatar
4 Antworten
+ 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; }
25th Oct 2016, 5:56 PM
Sumita Das
Sumita Das - avatar
+ 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...
25th Oct 2016, 5:35 PM
Stefan
Stefan - avatar
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; }
28th Oct 2016, 2:19 PM
Yash Raj
Yash Raj - avatar
- 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
25th Oct 2016, 8:15 PM
Arshi Singh
Arshi Singh - avatar