1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

mini challenge for novice programmers

Discussion in 'Programming' started by amitace_223, Sep 1, 2004.

  1. amitace_223

    amitace_223 New Member

    Joined:
    Sep 1, 2004
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    IT student
    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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 ( "-" )
     
  3. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    I think, i need little more detail before going for this challenge,as realy i can't understand what's amit challenge
     
  4. Amit Ray

    Amit Ray New Member

    Joined:
    Jul 12, 2004
    Messages:
    75
    Likes Received:
    4
    Trophy Points:
    0
    Occupation:
    Software Developer
    Home Page:
    I do not know what is the big deal ... bit I feel assigning n = -20 will fulfill your requirement ... isn't it ..

    :cool:

    Amit Ray.
     
  5. Amit Ray

    Amit Ray New Member

    Joined:
    Jul 12, 2004
    Messages:
    75
    Likes Received:
    4
    Trophy Points:
    0
    Occupation:
    Software Developer
    Home Page:
    or maybe by assigning i = 40 .. in the for loop ... appending one character 4 before the existing 0 ...

    Amit Ray.
     
  6. amitace_223

    amitace_223 New Member

    Joined:
    Sep 1, 2004
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    IT student
    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.
     
  7. amitace_223

    amitace_223 New Member

    Joined:
    Sep 1, 2004
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    IT student
    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.
     
  8. amitace_223

    amitace_223 New Member

    Joined:
    Sep 1, 2004
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    IT student
    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.
     
  9. amitace_223

    amitace_223 New Member

    Joined:
    Sep 1, 2004
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    IT student
    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.
     
  10. Amit Ray

    Amit Ray New Member

    Joined:
    Jul 12, 2004
    Messages:
    75
    Likes Received:
    4
    Trophy Points:
    0
    Occupation:
    Software Developer
    Home Page:
    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. :)
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    So here are the three other solutions I can think of
    Code:
    for (int i=0;i + n; i--) 
    Code:
    for (int i=0;-i < n; n--) 
    Code:
    for (int i=40;i < n; i--) 
    Thanks
    Shabbir Bhimani
     
  12. carl

    carl New Member

    Joined:
    Jul 15, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi all experts out there ....

    need some ...new to programming..

    below is the program for which i need to know the the process....I know the aswer which is = 9.....but need to know how come... what is the calculation.

    Consider the following program segment:

    i = 6720; j = 4;

    WHILE ( (i % j) = = 0){

    i = i / j;

    j = j + 1;

    }

    plz help...need it asap.

    thanks & regards,

    carl

    carl.banderas@rediffmail.com
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Please refer to my reply in thread [thread=1013]need help on this program output[/thread]
     

Share This Page