Can you use inline conditional assignments? IE:
int Grade = (pct >= 85) ? 'A' : ((pct >= 75) ? 'B' : ((pct >= 65) ? 'C' : ((pct >= 55) ? 'D' : 'F')));
No if statements there! :-)
rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
I need to write a code that takes a percentage and converts it into a letter grade. The problem is that I'm not allowed to use if statements.
85-100 -> A
75-84 -> B
65-74 -> C
55-64 -> D
0-54 -> F
One attempt I made was changing the numbers to their ASCII code but the 15 percent range for A and the D to F makes the method not work.
A = 65; B = 66; C = 67; D = 68; F = 70
letterGrade = 69 - (int)(percentage / 10 - 4.5);
Works from 94 to 55.
Any ideas?
Looks to me like you're on the right track, just need to zero in on the correct equation and technique.
One thing to think about is an array to hold the letter grades. How can that idea be used?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
You don't have to mess with ASCII codes.
So, since you completely confused us, how about restating your problem so we understand exactly what you can and can't do. Can you use the % operator?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
That's what is called "thinking around corners". :-)
rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
It's not a perfect solution like you think though it works with your requirement. Imagine a case where F grade is for 0-35 score. Then you need some more changes :)
But that was not the question! The answer generated was correct under the conditions set by the teacher.
rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179