954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Grade Calculator

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?

jennystc
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

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];
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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];
invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

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);
        }
}
abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: