Announcement

Collapse
No announcement yet.

code teaser

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

  • #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


    • #3
      What programming language?

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

      Comment


      • #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


        • #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


          • #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


            • #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


              • #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


                • #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


                  • #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


                    • #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


                      • #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


                        • #13
                          What about?

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

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

                          Comment


                          • #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


                            • #15


                              Tricky little blighter that one

                              Comment

                              Working...
                              X