Like Tree2Likes
  • 2 Post By CommonTater

A simple couple of questions

This is a discussion on A simple couple of questions within the C Programming forums, part of the General Programming Boards category; Hello, I have a couple of questions, both regarding IF. I'm using Turboc to compile the program if its worth ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    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. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    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.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ROFLmonster View Post
    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. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    you can express 500<x<1000 as
    if(x>500&&x<1000)

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    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.
    Last edited by ROFLmonster; 12-14-2011 at 08:18 AM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ROFLmonster View Post
    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".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Quote Originally Posted by MK27 View Post
    Yes. Aka "logical and".
    Right, done it.
    I guess that's all, thanks everybody.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ROFLmonster View Post
    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. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    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 subscribe to a feed

Similar Threads

  1. A couple (hopefully) simple questions.
    By SirStrange in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2010, 09:35 PM
  2. Couple of questions
    By kzar in forum C++ Programming
    Replies: 13
    Last Post: 10-08-2005, 01:13 PM
  3. Couple Questions...
    By GameGenie in forum C++ Programming
    Replies: 4
    Last Post: 08-26-2005, 02:57 PM
  4. A couple questions
    By Flakster in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2005, 01:22 PM
  5. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21