Input a Score and Determine Its Grade

Preface
It’s been a while since I played with C. Last night I saw a problem:
Input a score and automatically determine its grade.
Requirement: Use
switch(){}.
Process
First, I need to get the score and assign it to a variable. scanf(); can do that:
#include<stdio.h>
int main()
{
float score;
printf("Please type a score:\n");
scanf("%f", &score);
}
Second, I need to make sure the number is less than 100 and greater than 0:
Full score is 100. Minimum score is 0.
An article I wrote before mentioned the && operator.
So just use if(score <= 100 && score >= 0){}:
#include <stdio.h>
int main()
{
float score;
printf("Please type a score:\n");
scanf("%f", &score);
if(score <= 100 && score >= 0)
{
}
}
Now the score source problem is solved. If the input score is greater than 100 or less than 0, just report an error:
printf("The score %.2f is an Error Value!", score);
Now I need to assign different grades to different scores. Here are the score-to-grade mappings:
- 90 to 100 is A
- 80 to 89 is B
- 70 to 79 is C
- 60 to 69 is D
- Below 60 is E
These letters will be stored in a single character variable, which I’ll name grade:
#include <stdio.h>
int main()
{
float score;
char grade;
printf("Please type a score:\n");
scanf("%f", &score);
if(score <= 100 && score >= 0)
{
}
else
{
printf("The score %.2f is an Error Value!", score);
}
}
How do we determine the grade corresponding to a score?
We could use if(){} to write it, but the requirement is to use switch();.
The idea is to take the score, divide it by 10, convert it to an integer, and then compare it case by case:
switch ((int)(score / 10))
{
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
default:
grade = 'E';
break;
}
Then just output the score and letter:
printf("The score %.2f is grade %c .\n",score,grade);
Complete Code
See on GitHub: drafts/a.c at main · WeepingDogel/drafts · GitHub
Or here:
/*
Score to Grade ABCDEF
By WeepingDogel
*/
#include <stdio.h>
int main()
{
float score;
char grade;
printf("please input your score:\n");
scanf("%f",&score);
if (score <= 100 && score >= 0)
{
switch ((int)(score / 10))
{
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
default:
grade = 'E';
break;
}
printf("The score %.2f is grade %c .\n",score,grade);
}
else
{
printf("The score %.2f is an Error Value!", score);
}
return 0;
}