Guess the code!

This is a discussion on Guess the code! within the C++ Programming forums, part of the General Programming Boards category; Now I got bored and thought of starting a "game". Everyone can write code in this thread. Maybe a function ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Guess the code!

    Now I got bored and thought of starting a "game".

    Everyone can write code in this thread. Maybe a function or a simple algorithm or just a single code line.
    I'll start with a piece of code. Someone else shall then guess what the code does and write their own piece of code, preferable in C or C++, and not to long.

    So, here's the code in php taggs:
    PHP Code:
    (& (1)) == 
    What does it do?
    Last edited by TriKri; 11-06-2006 at 04:12 PM.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It cuts the x axis at 0 and 1? Duh, you have used so so the answer to that is no.

    That was probably a really stupid answer as I dont understand what you are saying or find it in anyway fun!
    Last edited by bumfluff; 11-06-2006 at 04:05 PM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess it tests if n is a power (or exponent, what-you-call-it) of 2. But why didn't you post C++ code?

    Code:
    #include <iostream>
    
    bool isExponentOfTwo(unsigned int n)
    {
        return (n&(n-1))==0;
    }
    int main()
    {
        std::cout << std::boolalpha;
        for (unsigned int i=1; i <= 32; i++)
            std::cout << i << " is a exponent of 2? " <<
            isExponentOfTwo(i) << std::endl;
        std::cin.get();
    }

  4. #4
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Right! It checks whether n is a power of two or not.

    Oh, I forgot to tell that it is not necessary to put the code in a context. Or maybe I'm just lazy!
    It is C++ code, but if you are going to write in any other language like assembly, you'll have specify it.
    Last edited by TriKri; 11-06-2006 at 04:27 PM.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    I cannot go wrong if I answer instead that it does a bitwise comparison of a value by that same value minus one and compares the result to 0.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Ok, my turn:

    Code:
    COORD c; 
       c.X = x - 1 ; 
       c.Y = y - 1 ;
    It's real easy, but I wanted to see if any noobs knew it

    Somone else post a really difficult one

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Gee, I don't know, but if you replace x - 1 by x + 1, you have my salary graphic for the next 20 years
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    That is a top answer.
    Mario, you have been around the boards for a while, give us a real brain teaser

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    unsigned char bit = 1 << (CHAR_BIT - 1);
    Code:
    unsigned short bit = (((unsigned short)-1) >> 1) + 1;
    Code:
    unsigned int bit = (-1U >> 1) + 1
    Code:
    unsigned long bit = ULONG_MAX / 2 + 1;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Il get back to you dave, I have to sleep now il check the thread tomorrow. I am not backing out of it but its like 2am now and I have just finished my assignment, its due tomorrow, been working on it all night.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    What's the other bad thing about having your arms amputated?

    Think of that while you give an answer to this:
    I'm not anywhere close to be a C++ guru to even pretend I can create brain teasers... So, I'll cheat with someone else's brain teaser.

    Without using macros or any other preprocessor tricks, have an apparently infinite number of plus signs appear consecutively, without spaces in between, and still be perfectly valid code? (note: no, not inside a comment. code that the compiler will process)
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Quote Originally Posted by Mario F.
    have an apparently infinite number of plus signs appear consecutively
    Code:
    std::string plus = "++++++++++++++++++++++++++++++++++++....";
    or maybe
    Code:
    for (;;) { std::cout<<"+"; }
    will display an infinite number of plus signs to the screen........ ?
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    To clarify it:

    How can you make this compile without using any kind of preprocessor tricks and using any other number of plus signs?
    Code:
    std::cout << ++++++++++++++++++++++++++++++++++++++++++++++++++val;
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Well, thats interesting...
    Code:
    #include <iostream>
    
    int main () {
        int val = 0;
        std::cout << ++++++++++++++++++++++++++++++++++++++++++++++++++val;
        std::cin.get();
        return 0;
    }
    That compiles and runs fine... I get 25
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    Quote Originally Posted by Dave_Sinkula
    Code:
    unsigned char bit = 1 << (CHAR_BIT - 1);
    Code:
    unsigned short bit = (((unsigned short)-1) >> 1) + 1;
    Code:
    unsigned int bit = (-1U >> 1) + 1
    Code:
    unsigned long bit = ULONG_MAX / 2 + 1;
    That would produce the highest exponent of two storable in each of those datatypes.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Page 1 of 7 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. banking2
    By sweet2awy in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2003, 03:01 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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