mini challenge for novice programmers

amitace_223's Avatar
Light Poster
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
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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 ( "-" )
alok's Avatar
Ambitious contributor
I think, i need little more detail before going for this challenge,as realy i can't understand what's amit challenge
Amit Ray's Avatar, Join Date: Jul 2004
Contributor
I do not know what is the big deal ... bit I feel assigning n = -20 will fulfill your requirement ... isn't it ..



Amit Ray.
Amit Ray's Avatar, Join Date: Jul 2004
Contributor
or maybe by assigning i = 40 .. in the for loop ... appending one character 4 before the existing 0 ...

Amit Ray.
amitace_223's Avatar
Light Poster
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.
amitace_223's Avatar
Light Poster
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.
amitace_223's Avatar
Light Poster
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.
amitace_223's Avatar
Light Poster
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.
Amit Ray's Avatar, Join Date: Jul 2004
Contributor
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.