Help with simple alghorithm

This is a discussion on Help with simple alghorithm within the C++ Programming forums, part of the General Programming Boards category; I cant seem to understand why would it put out ABCDEFH. The first number in the if (x%3==2) condition is ...

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

    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));
        }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    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
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    856
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

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, 10: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, 07:52 AM

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