samedi 28 septembre 2019

How can I filter out a value above 100 so that it doesn't puts it in the sum

The program asks for input of the speed, when the speed is above 100 it should not include that value in the calculation of the average speed. How can I do that?

I have put a i--; in the else if where it says else if(speed > 100). It repeats the question, but doesn't remove the value that is bigger than 100.

#include <stdio.h>




int main(){




int i;

double speed, sum = 0.0;

float average; 




for( i = 0; i < 10; i ++ ){ // asks 10 times the printf




printf("%d Enter speed: ", i);

scanf("%lf",&speed); // saves the input speed

    sum += speed; // sum = sum + speed; 




// decides which gear to use



 if (speed == 0){
    printf("gear N\n");

}else if (speed < 0 ){
    printf("gear R\n");

}else if(speed <= 10.0){
    printf("gear 1\n");

}else if (speed <= 30.0){
    printf("gear 2\n");

}else if (speed <= 60.0){
    printf("gear 3\n");

}else if (speed <= 80.0){
    printf("gear 4\n");

}else if (speed <= 100.0){
    printf("gear 5\n");

}elseif (speed > 100 ){ // when input higher than 100 dont save the input and ask again
    printf("max speed 100 km/h\n");

     i--;  
}else 
    printf("Error!\n");


}


average = sum/i; // average calculation

  printf("average speed = %.2lf km/h", average); // prints out the average


return(0);

}

When I put 200 it should remove the value and ask again. What I'm getting when I put 200 it asks again but uses the 200 to calculate the average.

Aucun commentaire:

Enregistrer un commentaire