Thread: A simple couple of questions

1. A simple couple of questions

Hello,
I have a couple of questions, both regarding IF.
I'm using Turboc to compile the program if its worth mentioning btw.

The first one- the assignment is this-
Someone rolls a dice, the score is determined like this-
if its an odd number, his score would be the dice * 2. (3*2=score=6)
if its an even number, his score would be the dice * 2 + 1 (4*2+1=score=9).
Now, I googled it many times and I got the same code,
Code:
```int dice,total_score;
printf("enter your dice's score");
scanf("%d",&dice);
if(dice%2==0)
{
total_score=dice*2+1;
printf("you total score is: %d",total_score);
}
else
{
total_score=dice*2;
printf("your total score is: %d",total_score);
}
}```
And everything works, I got my result as I expected, but would someone explain me how this works?
Especially that '%2' part, what does it do? didn't find an explanation for this one.

Second question-
I basically need to know how do I tell the program if X is bigger than 500, but is smaller than 1000 (500<X<1000), then do a certain thing.

An example-
if you buy something in a store that cost more than 500\$, you get a 5% discount. but if you buy something for 1000\$, you get 10% discount.

Obviously the 500<X<1000 didn't work, so I came here. x.x

hope everything is clear enough, thanks in advance,
Yuval.

2. The % is the modulo operator. An expression like a%b means that it divides a by b and returns the remainder.

So dice % 2 tests whether dice is odd. If it is odd, then dividing by two gives a remainder of one and if it's even the remainder is zero.

For your second question, expressions like 500<X<1000 don't work. You have to break it up. You want to express something like if X is greater than 500 and X is less than 1000.

3. Originally Posted by ROFLmonster
Hello,
I have a couple of questions, both regarding IF.
I'm using Turboc to compile the program if its worth mentioning btw.

Now, I googled it many times and I got the same code,
...
And everything works, I got my result as I expected, but would someone explain me how this works?
Especially that '%2' part, what does it do? didn't find an explanation for this one.

Second question-
I basically need to know how do I tell the program if X is bigger than 500, but is smaller than 1000 (500<X<1000), then do a certain thing.

An example-
if you buy something in a store that cost more than 500\$, you get a 5% discount. but if you buy something for 1000\$, you get 10% discount.

Obviously the 500<X<1000 didn't work, so I came here. x.x

hope everything is clear enough, thanks in advance,
Yuval.
Ok... part of your problem is that instead of sitting down and puzzling this out for yourself, you went to goggle and did a little scoop and poop coding. The end result is that you now find yourself in the predicument of having code in your program that you do not understand. Not only is it not possible for you to fix the problems... you haven't learned a darned thing from the experience. Well... except for the dangers of copy and paste coding, that is.

You would be well advised to stop playing with borrowed code or help taken from the internet and spend a bit time actually understanding how programming --C programming in particular-- actually works. Don't beg for help, don't go googling code off those horrible "help" sites... actually learn the programming skill.

Also Turbo C is an antique compiler, 20+ years out of date. You really should get yourself updated to something from this millenium... Have a look at Pelles C (yes that's a link, click on it). It's a lot more up to date, comes with complete documentation, produces 32 and 64 bit executables (not 16) and works natively in Windows. It also has the best IDE I've ever seen.

4. you can express 500<x<1000 as
if(x>500&&x<1000)

5. Originally Posted by CommonTater
Ok... part of your problem is that instead of sitting down and puzzling this out for yourself, you went to goggle and did a little scoop and poop coding. The end result is that you now find yourself in the predicument of having code in your program that you do not understand. Not only is it not possible for you to fix the problems... you haven't learned a darned thing from the experience. Well... except for the dangers of copy and paste coding, that is.

You would be well advised to stop playing with borrowed code or help taken from the internet and spend a bit time actually understanding how programming --C programming in particular-- actually works. Don't beg for help, don't go googling code off those horrible "help" sites... actually learn the programming skill.

Also Turbo C is an antique compiler, 20+ years out of date. You really should get yourself updated to something from this millenium... Have a look at Pelles C (yes that's a link, click on it). It's a lot more up to date, comes with complete documentation, produces 32 and 64 bit executables (not 16) and works natively in Windows. It also has the best IDE I've ever seen.
I didn't copy the whole code, I only looked for a solution for the odd/even thing.
The whole dice and everything that was me, I just tried a 'trick' (which is dice%2 part) to solve my problem, and really after I looked in my notebook a bit more I saw we have learned that, but I just needed an explanation of how that works.
The one, and ONLY line I actually copied was '(dice%2==0)'.

I learn in a class, with a teacher, and I'm gonna be tested on it this year, and next year, and thats also why I use TurboC. its my teacher's program, which he had told us to use and I can't argue with him.
Seriously, don't just attack people. x.x
---
@TheBigH
Thanks, a friend of mine reminded me we actually learned about, just didn't think it was so important. P:
About the bigger and smaller than, I can write-
if(price>500 && price<1000)
can't I? && is AND like those OR, XOR and such as I understood.

EDIT:
@oops..
Hehe was writing the message as you submitted yours I guess. P: thanks for confirming this.

6. Originally Posted by ROFLmonster
About the bigger and smaller than, I can write-
if(price>500 && price<1000)
can't I? && is AND like those OR, XOR and such as I understood.
Yes. Aka "logical and".

7. Originally Posted by MK27
Yes. Aka "logical and".
Right, done it.
I guess that's all, thanks everybody.

8. Originally Posted by ROFLmonster
I learn in a class, with a teacher, and I'm gonna be tested on it this year, and next year, and thats also why I use TurboC. its my teacher's program, which he had told us to use and I can't argue with him.
Seriously, don't just attack people. x.x
It's not an attack.

What you're going to discover after graduation is that your teacher hasn't done you any favours at all.

Trust me... "Two years classroom training on Turbo C" is not something you want to put on a resume.
But that also doesn't qualify you to put "C programming" in there either.

The first thing you'll discover when getting off of the antique DOS compiler is that almost none of what you learned applies to modern GUI based programming. In fact, you will most likely discover that aside from basic C syntax, 90% of what you learned is useless to you.

This is a problem we've seen here before... and no doubt we'll see it again.

Many universities --particularly in India, but also world wide-- are decades behind the times, turning out people with engineering degrees who end up working in phone pits offering free satellite dishes to anyone silly enough to sign a 25 year contract.

It was a suggestion that you modernize before running into these problems.

9. Originally Posted by CommonTater
It's not an attack.

What you're going to discover after graduation is that your teacher hasn't done you any favours at all.

Trust me... "Two years classroom training on Turbo C" is not something you want to put on a resume.
But that also doesn't qualify you to put "C programming" in there either.

The first thing you'll discover when getting off of the antique DOS compiler is that almost none of what you learned applies to modern GUI based programming. In fact, you will most likely discover that aside from basic C syntax, 90% of what you learned is useless to you.

This is a problem we've seen here before... and no doubt we'll see it again.

Many universities --particularly in India, but also world wide-- are decades behind the times, turning out people with engineering degrees who end up working in phone pits offering free satellite dishes to anyone silly enough to sign a 25 year contract.

It was a suggestion that you modernize before running into these problems.
So maybe not 'attacked', but still, it looked like you only posted to say that I know nothing about programming and go read a book.

Also maybe I forgot to mention I'm not gonna work in programming, I'm learning electronics, and I guess C is part of the school program.
I also learn Assembly and in the 10min research I did about it programmers barely use it.

Many people got out of that school (and a few other in that town) to the army (Israeli Unit 8200) with knowledge that served them until now, so I'm pretty sure he knows what he's doing.
Maybe 2 years Turboc'ing won't look good, but sure 3 years of experience in the army would.

I'm not here to fight, I just asked for help in something I had trouble doing.

Popular pages Recent additions