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?

Edited 4 Years Ago by Mxous: typo

Are you allowed to use for loops? Something like:

for(; (int)(percentage) > 90;) {
	//set letter grade to A
	break;
}
for(; \\...

switch statements and while loops could also work, but none of these are great solutions.

Edited 4 Years Ago by dmanw100: n/a

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! :-)

Edited 4 Years Ago by rubberman: n/a

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?

Edited 4 Years Ago by WaltP: n/a

I should have mentioned that the directions specifically say no "logical operators" rather than no if statements. So no switch or inlines. That array idea is genius... But I really don't think it would be allowed since we haven't talked about them yet.

This is the second assignment of a basic computer science course. While I've had some programming experience, many in the class are beginners. I imagine there is some sort of miscommunication that makes the problem overly difficult.

Maybe I'll come up with the answer by messing around with the ASCII codes more as it seems the likeliest of solutions.

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?

How about this??? :cool:

unsigned int grade_index;
    unsigned int score = 35; //this is your score. use scanf to get it.

    grade_index = score/55 + score/65 + score/75 + score/85;

    char grade = 69;
    grade = grade - grade_index;
    printf("%c", grade);

Very interesting set up subith. Adds one every time it is over a value... To make it display F instead of D though:

grade_index = 2*(score/55) + score/65 + score/75 + score/85;
     
    char grade = 70;
    grade = grade - grade_index;
    printf("%c", grade);

It Works!
Such a simple solution that I probably would never have come up with...

Thanks for everyone's help.

Very interesting set up subith. Adds one every time it is over a value... To make it display F instead of D though:

grade_index = 2*(score/55) + score/65 + score/75 + score/85;
     
    char grade = 70;
    grade = grade - grade_index;
    printf("%c", grade);

It Works!
Such a simple solution that I probably would never have come up with...

Thanks for everyone's help.

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 :)

Edited 4 Years Ago by subith86: n/a

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.

But that was not the question! The answer generated was correct under the conditions set by the teacher.

I know. But I still I feel my solution is not the perfect one. Why because, I gave away a non-generic solution. A code is not good if it works in some scenario but doesn't work in other. I gave a hint about the loop hole so that Mxous could put his own effort into correcting it.

The solution was the one intended by the professor. He gave a couple of hints because nearly everyone was complaining about it being impossible. And I think it is a good solution. If F was a 0-34 and D a 35-64, then

grade_index = 2*(score/35) + score/65 - 2*(score/70) + score/75 + score/85;

would make it work. Takes a bit of revision but should work in just about any scenario.

This question has already been answered. Start a new discussion instead.