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

Maybe a simple question

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?

Mxous
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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.

dmanw100
Posting Whiz in Training
242 posts since Apr 2008
Reputation Points: 104
Solved Threads: 27
 

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
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

Mxous
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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);
subith86
Junior Poster
124 posts since Mar 2011
Reputation Points: 30
Solved Threads: 14
 

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.

Mxous
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

That's what is called "thinking around corners". :-)

rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
 

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

subith86
Junior Poster
124 posts since Mar 2011
Reputation Points: 30
Solved Threads: 14
 
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
 
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.

subith86
Junior Poster
124 posts since Mar 2011
Reputation Points: 30
Solved Threads: 14
 

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.

Mxous
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You