Thread: Help with simple alghorithm

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    Help with simple alghorithm

    I cant seem to understand why would it put out ABCDEFH.

    The first number in the if (x%3==2) condition is 5.

    So 65+(5/3) = 66 2/3. Its not even an integer its a float. Then why would it put out an "A".

    Code:
    for(int x=0; x<26; x++)
        {
         if(x%3==2)
         putchar(65+(x/3));
        }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    The first number in the if (x%3==2) condition is 5.
    You forgot about 2.

    Quote Originally Posted by Ducky
    Its not even an integer its a float
    There are no floats here since integer division is used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    That's pretty wild. On my Mac, under Xcode, this is my output:
    Code:
    [Session started at 2009-01-09 08:37:16 -0600.]
    ABCDEFGH
    The Debugger has exited with status 0.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Yes of course its integer division. Thank you!

    You forgot about 2.
    Yes i did. 2%3 is 2. Thats why the "A". Thanks again!

    Dino your output is normal.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM