Posted by: Haroon Saeed | January 14, 2008

Try your coding skills !

Here is a small C/C++ code snippet,

int n = 20;

for( int i = 0; i < n ; i—) // i minus minus(in case of typo)

printf(“Hello World!”) ;

First try to find the answer to this program. It will obviously be an infinite loop or memory over flow or …

The question is to make this snippet work and print the “Hello World!” 20 times correctly as if the statement i— would have been i++.

The restriction however is to change only one ASCII character in this code. This means you can change, replace, move, introduce only one character to this program and the program still remains a valid C/C++ code.

There are three possible ways I know to do this. May be there are more. If you find two then you are good, if three you are brilliant and if more than extraordinary ;)

Have happy time solving this.


Responses

  1. Solution 1:
    Change the loop exit condition to -i < n from i < n. Since -(-20) is not less than 20, the loop will end on its 20th iteration.

    Solution 2:
    Change i– to n–. So, while i remains 0, n will keep reducing till it touches 0.

    Solution 3:
    Change the loop exit condition to ~i < n from i < n. The not operator reverses the bits. So 0 becomes -1, -1 becomes 0, -2 becomes 1 and so on.

  2. aristotlethegeek,

    Your 3rd solution would make the program to print 21 times. because ~i starts with -1.

    However, if you make the condition “i + n” it will become 0 when i=-20. So that would be the third solution.

    Saeed,

    Do you have any other solution than for these three?

  3. Yes. You are right. So ‘i + n’ is the third solution which is valid C/C++ (but invalid C#, which is the language I was using to tinker around). ~i was a stupid idea considering that the first solution was -i.

  4. Dumindu’s solution is what exactly I know, so you are right, though this wont work in C# (the i + n one) hence aristotlethegeek is also right. So you both have got the right solution :) …good shot.

  5. Since we are strictly restricting to the fact of finding different ways, i would just add i – n is yet another correct one since i thought of this instead of i + n as my second solution. Nonetheless they seem like one solution presented with another operator so still they look 3 to me and i did those in less than 15 min. Any other?

  6. Fayyaz I don’t think ‘i-n’ will work.

    Btw was it really necessary to tell the time it took to solve..?

    Haroon

  7. if ( )
    printf(“hi..”);
    else
    printf(“hello”);

    O/P=hi..hello

    using which condition I will above o/p?

  8. 2 very simple ways… since we can introduce characters
    1. make n= -20 (instead of 20)
    2. make int i=0 int i=40

  9. Hmm, wouldn’t replacing i– with n– do the trick as well? eventually, n will drop to 0.

  10. Oh, already suggested!

  11. Ayesha!! you still can do coding :) |?, thats cool (Y)…well the answer of yours is right..

  12. Coding is in my blood…okay, that was cheesy. You can’t forget the khawaari of four years that easily. Trust me. :P


Leave a response

Your response:

Categories