code teaser

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Helevitia
    Super MURCer
    • Aug 1999
    • 6628

    #1

    code teaser

    A co-worker presented me with some code the other day. I just started learning so I don't know what the answer is. Apparently it was on an aptitude test for a job here at my work.

    int i, n = 20;
    for (i = 0; i < n; i--)
    printf("-");

    Rules:

    You have to print '-' 20 times by adding or removing or replacing only a single character from the code.

    There are 3 possible solutions for the above question. What are they ?

    Duration:

    10 min.
    Ladies and gentlemen, take my advice, pull down your pants and slide on the ice.
  • Helevitia
    Super MURCer
    • Aug 1999
    • 6628

    #2
    btw, I compiled it just to see what would happen. I also tried changing things but nothing I did would bring it to the desired resolution.
    Ladies and gentlemen, take my advice, pull down your pants and slide on the ice.

    Comment

    • Jammrock
      The Berserker
      • Aug 1999
      • 8661

      #3
      What programming language?

      Jammrock
      “Inside every sane person there’s a madman struggling to get out”
      –The Light Fantastic, Terry Pratchett

      Comment

      • Helevitia
        Super MURCer
        • Aug 1999
        • 6628

        #4
        Originally posted by Jammrock
        What programming language?

        Jammrock
        I used C.

        Dave
        Ladies and gentlemen, take my advice, pull down your pants and slide on the ice.

        Comment

        • Gurm
          MS Fanboy
          • Aug 1999
          • 11460

          #5
          I'll think about it for a while, but I see no obvious solutions to this problem, let alone 3.

          - Gurm
          The Internet - where men are men, women are men, and teenage girls are FBI agents!

          I'm the least you could do
          If only life were as easy as you
          I'm the least you could do, oh yeah
          If only life were as easy as you
          I would still get screwed

          Comment

          • Gurm
            MS Fanboy
            • Aug 1999
            • 11460

            #6
            Oops. Got one.

            Solution 1:

            Change the for loop to read:

            for (i=0; i < n; n--)

            You're decrementing N instead of I.

            - Gurm
            The Internet - where men are men, women are men, and teenage girls are FBI agents!

            I'm the least you could do
            If only life were as easy as you
            I'm the least you could do, oh yeah
            If only life were as easy as you
            I would still get screwed

            Comment

            • ZokesPro
              Serial Murcerer
              • Sep 2000
              • 8325

              #7
              I'm gonna try...

              Original code:

              int i, n = 20;
              for (i = 0; i < n; i--)
              printf("-");

              Change:

              int i, n = 20;
              for (i = 0; i > n; i--)
              printf("-");

              I know a little about C, I'll laugh if I get it right.
              Titanium is the new bling!
              (you heard from me first!)

              Comment

              • Gurm
                MS Fanboy
                • Aug 1999
                • 11460

                #8
                Solution #2:

                Change the for loop to read:

                for (i=0; -i < n; i--)

                That'll work too.

                - Gurm
                The Internet - where men are men, women are men, and teenage girls are FBI agents!

                I'm the least you could do
                If only life were as easy as you
                I'm the least you could do, oh yeah
                If only life were as easy as you
                I would still get screwed

                Comment

                • Helevitia
                  Super MURCer
                  • Aug 1999
                  • 6628

                  #9
                  Originally posted by Gurm
                  Oops. Got one.

                  Solution 1:

                  Change the for loop to read:

                  for (i=0; i < n; n--)

                  You're decrementing N instead of I.

                  - Gurm
                  I just talked to my buddy and this is correct. I also know the second solution. neither of us know the third.

                  Dave
                  Ladies and gentlemen, take my advice, pull down your pants and slide on the ice.

                  Comment

                  • Gurm
                    MS Fanboy
                    • Aug 1999
                    • 11460

                    #10
                    Sorry Zokes.

                    That won't work because i is NEVER greater than n.
                    The Internet - where men are men, women are men, and teenage girls are FBI agents!

                    I'm the least you could do
                    If only life were as easy as you
                    I'm the least you could do, oh yeah
                    If only life were as easy as you
                    I would still get screwed

                    Comment

                    • Helevitia
                      Super MURCer
                      • Aug 1999
                      • 6628

                      #11
                      Originally posted by Gurm
                      Solution #2:

                      Change the for loop to read:

                      for (i=0; -i < n; i--)

                      That'll work too.

                      - Gurm
                      This is correct as well.
                      Ladies and gentlemen, take my advice, pull down your pants and slide on the ice.

                      Comment

                      • ZokesPro
                        Serial Murcerer
                        • Sep 2000
                        • 8325

                        #12
                        Originally posted by Gurm
                        Sorry Zokes.

                        That won't work because i is NEVER greater than n.
                        Oh well. (I'm only on chapter 2 of like 40 on how ot learn C++)
                        Titanium is the new bling!
                        (you heard from me first!)

                        Comment

                        • Rob(QG)
                          MURCer
                          • Apr 2002
                          • 268

                          #13
                          What about?

                          for (i=0; i + n; i--)

                          Worked out #2 too, but gurm beat me to it - grrrr.

                          Comment

                          • Gurm
                            MS Fanboy
                            • Aug 1999
                            • 11460

                            #14
                            Rob: That's it! I was about to post it but you beat me to it!

                            i+n evaluates TRUE until i reaches -20 at which point it evaluates FALSE.

                            Excellent job!

                            - Gurm
                            The Internet - where men are men, women are men, and teenage girls are FBI agents!

                            I'm the least you could do
                            If only life were as easy as you
                            I'm the least you could do, oh yeah
                            If only life were as easy as you
                            I would still get screwed

                            Comment

                            • Rob(QG)
                              MURCer
                              • Apr 2002
                              • 268

                              #15


                              Tricky little blighter that one

                              Comment

                              Working...