@
anup32
That is the same as the OP’s code, just with less whitespace.
Sorry, my comment was not nice, nor correct. (See a few posts down for more.)
@
Alien000
You should not worry too much about
efficiency unless you can prove that it is an issue.
For
shorter, you want to use a simple array lookup: see line 12:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <cmath>
#include <iostream>
int main()
{
int score;
{
std::cout << "score? ";
std::cin >> score;
}
std::cout << "FDCBA"[ std::max( 5, std::min( score / 10, 9 ) ) - 5 ] << "\n";
}
|
The score divided by 10 is one of 0, 1, 2, …, 9, 10, …
Next we use min and max to make sure it is clamped to the range 5..9.
Next we subtract 5 to index the beginning of our grades array (
"FDCBA").
This is
also “more efficient”, but unless you are processing several hundred thousand student grades, you won’t notice the difference.
Your current strategy is much more readable, and therefore a better answer.
Hope this helps.
[edit]
@
lastchance
Timejumper!
You and I had the same brain wave.