Dropping the lowest number in an array
Dec 7, 2014 at 3:54am UTC
I have a homework assignment that asks me to enter 10 grades into an array then find the average after dropping the lowest grade. I got the average part down easy but I can't figure out how to drop the lowest grade. I tried this but got an average of zero which isn't right. Help...?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include<iostream>
using namespace std;
int main(){
int total = 0;
const int size = 10;
double avg;
int grades[size] = {76,87,99,54,78,98,65,86,84,100};
for (int i = 0; i<size; i++){
if (grades[size]<grades[i-1]){
total+=grades[i];
}
}
avg= total/size;
cout<< "The average is : " << avg <<endl;
return 0;
}
Dec 7, 2014 at 4:12am UTC
What are you tring to do here?
if(grades[size]<grades[i-1])
grades[size] : out of range access, the last grade is at
grades[size-1]
grades[i-1] : out of range access, when
i == 0
This is integer division:
avg = total/size;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
int main(){
const int size = 10;
int grades[size] = {76,87,99,54,78,98,65,86,84,100};
int total = 0;
int lowest = grades[0] ;
for ( int i = 0; i<size; ++i ) {
total += grades[i];
if ( lowest > grades[i] ) lowest = grades[i] ;
}
const double avg = double (total) / size;
const int total_excluding_lowest = total - lowest ;
const double avg_excluding_lowest = double (total_excluding_lowest) / (size-1); // size > 1
// ...
}
Last edited on Dec 7, 2014 at 4:27am UTC
Dec 7, 2014 at 4:21am UTC
...I had the average part without dropping the lowest... What I posted is my attempt at dropping the lowest. Sorry I wasn't clear enough. Thanks for the help anyway.
Dec 7, 2014 at 4:27am UTC
> I had the average part without dropping the lowest... What I posted is my attempt at dropping the lowest.
Ok. I'm sorry that my post didn't take that possibility into account. (Edited now).
Topic archived. No new replies allowed.