I need help creating a grade calculator in C. I am not allowed to use conditional statements. My problem is that I have written code to determine how many total points the student has, but I cannot think of a way to classify those points as an A (90-100), B (80-89), etc. without using conditional statements. My teacher gave us a hint and told us to use

#define GRADE_A 'A'

letterGrade = GRADE_A

and then a printf statement.

I understand somewhat where he's going, but not how to put it all togethor to acheive my goal. Any thoughts?

Recommended Answers

All 3 Replies

I suppose one way to do it would be to create an array of 101 characters, each array element represents the numeric grade. The use the grade to index into that array. For example

char grades[101] = {0};
grades[100] = grades[99] = grades[98] = 
    grades[97] = grades[96] = grades[95] = 'A';
grades[94] = grades[93] = grades[92] = 
    grades[91] = grades[90] = 'B';

// now get the grade
int score = 93;
char gr = grades[score];

I suppose one way to do it would be to create an array of 101 characters, each array element represents the numeric grade. The use the grade to index into that array. For example

Or create an array of 11 characters

char grades[11] = {'J' ,'I', 'H' ,'G', 'F' ,'E','D', 'C', 'B', 'A', 'A'};

// now code to get grade
int score = 93;
char gr = grades[score/10];

Are you allowed to use relational operators ?

If yes you could do something of this sort

total_marks= 85                             //For example if the total marks are 85


printGrades(total_marks);


void printGrades(total_marks)
{
      i=total_marks; 
      for(;i>=90;)
      {
            printf("A\n");
            exit(1);
      }
       for(;i>=80;)
        {
             printf("B\n");
              exit(1);
        }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.