Thread: Quick question

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    5

    Quick question

    Hi all,

    just a quick question, do you know why the code below would the give the following answers ive given, i understand why they input on the 4 separate lines but the amount of times it prints the letter 'x' is confusing me.

    Code:
    int i=1;
    while (i<=24)
    {
            printf("%c", i%6 ==0 ? '\n' : 'x');
            i++
    }
    Why would this give the answer: Generate 5 letters 'x' on 4 separate lines.


    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by mattio87 View Post
    Why would this give the answer: Generate 5 letters 'x' on 4 separate lines.
    Why do you expect something different ?
    Kurt

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    5
    Yes, i would have thought it would have printed 4 x's not five.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    1 % 6 = 1 -> 'x'
    2 % 6 = 2 -> 'x'
    3 % 6 = 3 -> 'x'
    4 % 6 = 4 -> 'x'
    5 % 6 = 5 -> 'x'
    6 % 6 = 0 -> '\n'
    7 % 6 = 1 -> 'x'
    ....
    Kurt

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    5
    I still dont understand it Kurt, it says i=1 i<=24, so it uses 1 -> 24.

    24/6 = 4, giving the amount of separate lines.

    I have no idea where the 5 letters of X still come from though

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I hope that we agree that your code prints a total of 24 chars.
    24/6 = 4 '\n' and 20 'x'
    Kurt

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    5
    If so, why does the answer say that it prints 5 letters 'x'?

    Am i just being stupid? lol

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If the answer says that it prints only 5 x's then the answer is wrong.
    It prints 4 times 5 x's.
    Kurt

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Well, as you can see in Kurt's post, all the values between two different multiples of 6 print an x. So you can do the following to find how many 'x' are printed between two consecutive multiples:

    ((k+1) * 6) - ((k * 6) + 1) = 5

    k*6 is a multiple of 6 and will result in a linebreak.
    k*6 + 1 is the first character that will result in an x being printed
    (k+1)*6 is the next multiple of 6

    The difference of the two clearly results in 5.

    And yes, the output is:

    Code:
    xxxxx
    xxxxx
    xxxxx
    xxxxx

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    5
    Thanks very much, i understand it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM