J.J. Hays
J.J. Hays

Reputation: 27

C Grading Program: Not Returning 'C' for Grading Program

I am unsure what's wrong with my program at the moment. I can get correct values for A, B, D, F; but it does not return value C when the grade is between 70 and 80.

I understand that I went around this program a weird way, that is due to how the professor has set up the assignment. I was able to get C with simple if statements. I believe the problem has to do with how my else (userGrade B) is set up.

/* Jon Hays
   Assignment 3B Due 9/25/19
   "Grade Calculator"
   This program calculates the average grade (%)
   out of three test scores and converts it to a character
   (A, B, C, D, F)*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main()
{
    double test1;
    double test2;
    double test3;
    char userGrade;

    printf("Please input three test scores:");
    scanf("%lf %lf %lf", &test1, &test2, &test3);

    double testAverage = (test1 + test2 + test3) / 3;
    double second3rdAverage = (test2 + test3) / 2;

    if (testAverage >= 90)
        userGrade = 'A';
    else if (testAverage >= 70 && testAverage < 90)
    {
        if (test3 > 90) 
            userGrade = 'A';
        else 
            userGrade = 'B';
    }
    else if (testAverage >= 50 && testAverage < 70)
    {
        if (second3rdAverage >= 70)
            userGrade = 'C';
        else
            userGrade = 'D';
    }
    if (testAverage <= 50)
        userGrade = 'F';

    printf("%c", userGrade);
}

Upvotes: 0

Views: 75

Answers (1)

cnh003
cnh003

Reputation: 144

Your algorithm is correct. The task you indicated in the comments

  • If the average score is 90% or more, the grade is A.
  • If the average score is 70% or more and less than 90%, check the third score.
    • If the third score is more than 90%, the grade is A; otherwise the grade is B.
  • If the average score is 50% or more and less than 70%, check the average of the second and third scores.
    • If the average of the two is greater than 70%, the grade is C; otherwise the grade is D.
  • If the average score is less than 50% then the grade is F.

means, that a C is only awarded if your overall score is below 70%. I guess, your assumption was, that the task at hand has something to do with real grading - which is not the case.

Upvotes: 2

Related Questions