Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Programming in C without using if statements?


  • Please log in to reply
13 replies to this topic

#1 altair05

altair05

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:05:40 PM

Posted 26 January 2011 - 04:54 PM

My team and I are working on a lab assignment that we have approx 95% finished except we're stuck on this one part.

We have 5 letter grades: A, B, C, D, F each associated with a number range

A = 90-100
B= 80 <90
C= 70<80
D= 60<70
F<60

We calculate a grade, say 85%. How do we program it so that the letter grade turns out to be a B without using if/else if/else, switch, conditional expressions, relation and logical operators, and the bool data type. We haven't learned this stuff yet so our professor doesn't want us touching the topics.

BC AdBot (Login to Remove)

 


#2 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:05:40 PM

Posted 26 January 2011 - 04:58 PM

Without using any conditional statements and boolean operators, I do not think its possible.

#3 Eyesee

Eyesee

    Bleepin Teck Shop


  • BC Advisor
  • 3,545 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:In the middle of Kansas
  • Local time:05:40 PM

Posted 26 January 2011 - 05:23 PM

What about a case statement?
In the beginning there was the command line.

#4 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:05:40 PM

Posted 26 January 2011 - 05:59 PM

What about a case statement?


The OP already has said about switch ... case.

#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:40 PM

Posted 26 January 2011 - 06:12 PM

If your prof can do it without conditional expressions, then he is a genius. You could calculate the average without a conditional statement, but you could not equate it to a letter grade. My guess is that you are either misunderstanding the specifications of the assignment, or your prof is a sadist.

#6 PropagandaPanda

PropagandaPanda


  • Malware Response Team
  • 10,433 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:40 PM

Posted 26 January 2011 - 06:25 PM

Hello.

I could think of a at least one way. This method doesn't really make sense since learning if statements usually comes before arrays.

Perhaps you can tell us the exact requirement word-for-word to eliminate any misunderstanding?

With Regards,
The Panda

Edited by groovicus, 26 January 2011 - 06:56 PM.


#7 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:40 PM

Posted 26 January 2011 - 06:58 PM

Panda, sorry, I had to remove your suggestion since that is not how we do things here (as far as homework is concerned). I will say though that I just logged on to suggest almost the exact same solution; ie array[91] = 'A'. I also seem to remember learning about conditionals before data structures.

Edited by groovicus, 26 January 2011 - 06:59 PM.


#8 altair05

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:05:40 PM

Posted 27 January 2011 - 01:25 AM

Honestly, I have no clue on how to proceed with this problem. I've heard that using arrays would work from a number of people, but the thing is that we haven't learned arrays in class. Not that I couldn't do it, I took Java and I could most likely figure someway of using an array to work with the problem.

Anyways, I have another lecture today so I'm gonna see what the professor has to say about this and check back with you guys. If nothing new pops up, my team and I'll most likely have to visit our TAs during their office hours.

If your prof can do it without conditional expressions, then he is a genius.


Not sure yet, but maybe I'll find out soon.

#9 JosiahK

JosiahK

  • Members
  • 269 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:11:40 PM

Posted 27 January 2011 - 08:06 PM

Wow! That is one mean assignment. I guess that if you're allowed to do some really complex Integer division and an ascii table lookup you could hack something together.
A mess like this VB.net code
Chr(70 - (Math.Floor(gradenumber / 51)) * (Math.Floor(gradenumber / 10) - 4) + Math.Floor(gradenumber / 100))

But if your professor seriously expects you to come out with that kind of junk he can't be teaching you very good programming (and sadist doesn't describe it). There are tools for every kind of job, and If and switch statements are the code for this.

Gets the character associated with the number between the brackets
Sets the highest code (lowest grade) to Ascii 70, or F. Subtracts one per additional grade (the calculated subtraction value)
Multiplies the subraction value for any less than 50 scores by 0 to take them out of the equation and leave them as F
Multiplies the subtraction value by the integer of 10% of the score. 95 = 9, 80 = 8, etc.
Subtracts 4 from the Subtraction Value, so that 95 now becomes 5 and 80 becomes 4.
Sorts out a bug when the score is 100 and it registers as @
Quod non mortiferum, fortiorem me facit.
I don't read minds. Please help everyone by answering any questions and reporting on the results of any instructions. Query any concerns and explain problems or complications.

#10 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:05:40 PM

Posted 27 January 2011 - 09:46 PM

@Josiah
I had thought of that too but I could not think of what to do of lower 60 limit. Nice thinking there :) :clapping:

#11 altair05

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:05:40 PM

Posted 28 January 2011 - 04:18 AM

I just found out that we have to mash something together using ASCII. Don't know, but JosiahK might be right.

#12 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:40 PM

Posted 28 January 2011 - 08:30 AM

but JosiahK might be right.

Yes.

#13 PropagandaPanda

PropagandaPanda


  • Malware Response Team
  • 10,433 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:40 PM

Posted 28 January 2011 - 10:29 AM

Hello.

No problem Grovy. :) I'd thought this was a special case because of the strange requirements.

With Regards,
THe Panda

#14 NewCalculus

NewCalculus

  • Members
  • 1 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:40 PM

Posted 23 June 2018 - 06:39 AM

The answer to this question is found here:

 

https://www.linkedin.com/pulse/code-without-use-conditionals-john-gabriel

 

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users