mini challenge for novice programmers

Light Poster
1Sep2004,15:18   #1
amitace_223's Avatar
hi all,

i m a lil new at this.....so forgive any mistakes

here's the riddle :
the following appears in a c program:

main()
{
n=20;
for(i=0;i<n;i--)
printf("-\n");
}

now the challenge is to get this code to display 20 dashes i.e 20 ( "-" ) one after the other each on a separate line by doing either one of the following
a.appending a SINGLE character from the code.
b.deleting a SINGLE character from the code.
c.replacing a SINGLE character from the code.

note the emphasis on the word single.....lol.......the point is tht only one character may be altered ar added( u get the idea).....and the result upon execution must be 20 dashesone after the other each on a separate line........(as is obvious by the \n in the printf statement)

note tht this is for novice programmers only.........u uberhackers can stay out of it!!

n if some superprogrammer does give it a try......hebetter come up with a solution in less than 10 sec....lol.

o yea and by the way........there's more than one solution to this ........so lets see how many u guys can come up with
Go4Expert Founder
3Sep2004,08:58   #2
shabbir's Avatar
Code:
for(i=0;i<n;i--)
isn't this an infinite loop.

but the challenge is
now the challenge is to get this code to display 20 dashes i.e 20 ( "-" )
Ambitious contributor
4Sep2004,12:29   #3
alok's Avatar
I think, i need little more detail before going for this challenge,as realy i can't understand what's amit challenge
Contributor
4Sep2004,19:28   #4
Amit Ray's Avatar
I do not know what is the big deal ... bit I feel assigning n = -20 will fulfill your requirement ... isn't it ..



Amit Ray.
Contributor
4Sep2004,19:32   #5
Amit Ray's Avatar
or maybe by assigning i = 40 .. in the for loop ... appending one character 4 before the existing 0 ...

Amit Ray.
Light Poster
4Sep2004,21:27   #6
amitace_223's Avatar
dear amit,

i should point out tht if u assign n=-20 then the loop will not be executed at all......isnt it obvious? .if n=-20, then i is no longer less than n (since i is 0).

as for ur second suggestion , this too falls flat on its face because of the same reason......i is no longer less than n if n=20 and i=40.

perhaps u shouldnt have assumed this problem to be too simple.
it is more deceiving than it seems.

however i guarantee u there are at leaast 3 solutions to this that i know of.

now with this fresh perspective, try and solve the problem again.
remember to note down how much time it takes u to solve it.

until next time,

amit.
Light Poster
4Sep2004,21:29   #7
amitace_223's Avatar
oh and by the way,

to those of u wondering if im talkin to myself here....i assure u im not.

my name happens to be amit too..............jus like amit ray ,our admin.

thts me ^ up there talkin to our admin......lol.
Light Poster
4Sep2004,21:36   #8
amitace_223's Avatar
this one is for shabbir.

dude,

the question doesnt not state tht the given loop displays 20 dashes.......neither did i ever suggest or imply in any manner that the loop given as it is in the question is not infinite.

read the question.

it says display 20 dashes by changing or adding or removing one character........it never says anything about the performance of the original code.
the output of the given code as it appears in the question is not to be considered at all........it is the output of the modified code tht is to be worked on.

i hope i have cleared all ur doubts.

any other queries regarding this problem are most welcome.

amitace_223,
ace.
Light Poster
4Sep2004,22:08   #9
amitace_223's Avatar
this one's for u alok.

i personally think the question is complete in itself and is self explanatory.
i have tried every bit to ensure that the language is simple ,lucid,concise and to the point.

however, i'll try another approach to explain the question.

the aim of the question is to get the following output:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

note that this output should be received on the execution of the program by doing only ONE of the following:
1.changing any character in the given program code.
2.adding any character to the given program code.
3.deleting any character from the given program code.

i believe this must take care of ur doubt , alok.

any other queriesregarding this problem are most welcome.

amitace_223,
ace.
Contributor
5Sep2004,02:55   #10
Amit Ray's Avatar
Eeeeeekks ... well i must be sleeping when i replied .... i mistook the < to be > .. anyways ..

changing the for loop to : for (int i=0;-i < n; i--) would be one answer ... ( hoping u did not miss out declaring 'i' intentionally ... ) ..

as for the other possibilities .. maybe later ..

Amit Ray.