Home > front end >  Grade classification program using float and if statements using relational operator creates a logic
Grade classification program using float and if statements using relational operator creates a logic

Time:10-05

When using the variable from float grade that is equal to sometimes the start of the range or the end of the range, skips to else statement. Also when scanning a negative number, discards the unary sign and acts as a positive.

Sample input 1: 3.8 expected result: Excellent outcome: Average

Sample input 2: 1.3 expected result: Passing outcome: Invalid input...

Grade Description
3.8 - 4.0 Excellent
3.3 - 3.7 Superior
2.8 - 3.2 Good
1.8 - 2.7 Average
1.3 - 1.7 Below Average
1.0 - 1.2 Passing
below 1.0 Failure
not within 0.0 - 4.0 Invalid
#include<stdio.h>

int main (void){
    
    float grade;
    
    printf("\t\tWhat is your grade?\t\t");
    scanf("%f",&grade);
    
    if ((grade<=4.0) && (grade>=3.8)){
        printf("\n\t\tInput grade: %.1f",grade);
        printf("\n\t\tExcellent");
    }
    else if ((grade<=3.7) && (grade>=3.3)){
        printf("\n\t\tInput grade: %.1f",grade);
        printf("\n\t\tSuperior");
    }
    else if ((grade<=3.2) && (grade>=2.8)){
        printf("\n\t\tInput grade: %.1f",grade);
        printf("\n\t\tGood");
    }
    else if ((grade<=3.7) && (grade>=1.8)){
        printf("\n\t\tInput grade: %.1f",grade);
        printf("\n\t\tAverage");
    }
    else if ((grade<=1.7) && (grade>=1.3)){
        printf("\n\t\tInput grade: %.1f",grade);
        printf("\n\t\tBelow Average");
    }
    else if ((grade<=1.2) && (grade>=1.0)){
        printf("\n\t\tInput grade: %.1f",grade);
        printf("\n\t\tPassing");
    }
    else if ((grade<=1.0) && (grade>=0)){
        printf("\n\t\tInput grade: %.1f",grade);
        printf("\n\t\tFailure");
    }
    else{
        printf("\n\t\tInvalid input...");
    }
    return 0;
}

CodePudding user response:

equal to sometimes the start of the range or the end of the range, skips...

Floating point numbers are rarely exactly the value you see. Try to imagine adding the fractions 1/2 1/4 1/8 1/16... to see that floating point values can only be approximately equal to the rounded base-10 values they represent; sometimes a tiny, tiny fraction more, sometimes a tiny fraction less. To test for equality is likely to disappoint.

Below, the user can comfortably enter a familiar 2 digit grade point. The value is immediately scaled up by 10 (with a smidgen more to 'push' reluctant values above the cut-off thresholds), then converted to a reliable binary value, albeit 10x larger.

This makes "integer comparison" a breeze.

Rather than clutter the code with if/else ladders, this code is a tiny bit less efficient, but is easy to visually scan to confirm values. (These values come from your code.) A "text evaluation" is decided, and a single printf serves. (Don't waste time mucking with tabs and "layout" until the code is working.) And, negatives are handled as if they were positive values.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    float grade;
    
    printf( "What is your grade? " );
    scanf( "%f",&grade );
    /* omitting test for success */
    
    int x = abs(grade * 10.0001); // to ensure not "slipping under"
    char *eval = NULL;

    if( eval == NULL && x >  40 ) eval = "Invalid input";
    if( eval == NULL && x >= 38 ) eval = "Excellent";
    if( eval == NULL && x >= 33 ) eval = "Superior";
    if( eval == NULL && x >= 28 ) eval = "Good";
    if( eval == NULL && x >= 18 ) eval = "Average";
    if( eval == NULL && x >= 13 ) eval = "Below Average";
    if( eval == NULL && x >= 10 ) eval = "Passing";
    if( eval == NULL            ) eval = "Failure";

    printf( "Input grade: %.1f - %s\n", grade, eval );

    return 0;
}
  • Related