-
October 12th, 2005, 08:19 AM
#1
C Test
Recently I was questioned about a test in C that I couldn´t find the answer. Can anybody Help me to find it?
Code:
...
int i,num = 42;
for (i=0 ; i < num ; i--)
printf("*");
...
The intent is print 42 *. the question is : "Can you fix this code changing just one character? You can insert OR remove OR change just one character of code. "
i++ would solve the problem ,so certainly it has something with boolean operators... As it was not enougth the problem pointed that there were 3 solutions , how many can you see?
thank you.
-
October 12th, 2005, 08:27 AM
#2
Re: C Test
Make num -42
int i, num = -42;
then starting at zero and counting down to -42 will give you 42 *'s
edit: nevermind, I just saw the operator is < and not !=
-
October 12th, 2005, 08:31 AM
#3
Re: C Test
1. for(i=0; -i<n; i--)
2. for(i=0; i+n; i--)
3. for(i=0; i<n; n--)
-
October 12th, 2005, 08:32 AM
#4
Re: C Test
I found 2 of 3 options to solve this problem :
1). add - to the 42 value
so it'll be -42
2). change the < to >
yes yes yes.....
BabyGuru
-
October 12th, 2005, 08:47 AM
#5
Re: C Test
clever rajesh, nice... 1 and 2 will work just fine, but 3 doesn't work, given the variable name. If the variable name matters ,then that's changing too much. But we'll have to hear back from Rabelo to find out.
-
October 12th, 2005, 09:17 AM
#6
Re: C Test
Appreciate others by rating good posts
"Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett
-
October 12th, 2005, 11:23 AM
#7
Re: C Test
wow rajesh_krec !!! Congratulations!!! (the variable name dosen't matter)
Baby Guru: ... ( Just one character....)
Eli Gassert: The first I tried. Doesn´t work because 0 > -42...
sunnypalsingh : Nice shot!!!
I need read more about boolean operators... Anyway I think that it is not a C test, it fits well on an assembler test because i++ would solve the problem without transform the program in a mess for non boolean experts.
Thank you !!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|