Announcement

Collapse
No announcement yet.

code teaser

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

  • #16
    Interesting, good job Gurm and Rob.

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

    Comment


    • #17
      damn it, i jsut came home

      pc++;

      Comment


      • #18
        You know, I've been thinking about the third answer and it is actually very spectacular. I mean, think about it for a second. You have to take a condition that almost never uses the "+" sign to reach that condition.

        Rob, how did you figure it out? I'm going to be impressed about this all day
        Ladies and gentlemen, take my advice, pull down your pants and slide on the ice.

        Comment


        • #19
          In C and C++, and many other languages, 0 = False and everything else equals True. That's the reason why he found out.

          Good job by the way!
          Why SysAdmins like Unix?
          unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep.

          Comment


          • #20
            In C it is quite common to see a construct such as:

            if (a) { blah blah }

            as a shorthand for:

            if (a!=0) { blah blah }

            Basically all expressions in C return a value, and if the return value is treated as either true or false (as in this case), then you have something to work with.

            Comment


            • #21
              I suppose there is a logical way of getting the answer once you have answer 2:

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

              loops when -i < n
              stops when -i == n
              therefore stops when 0==i+n

              Wish I could say I had worked it out that scientifically

              Comment


              • #22
                int i, n = 20;
                for (i = 0; i < n; i--)
                printf("-");
                Ah, I see. It's a debug test.

                LOL ... my mind is in a different plane of existence today. And it's not a very smart plane of existence.

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

                Comment


                • #23
                  My buddy at work wants to knwo if anyone else knows of any other code teasers? Alo, anybody know the link to some of the old brain teasers posted throughout the years?

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

                  Comment


                  • #24
                    FWIW, I figured you could also change the loop to:

                    for (i=40; i&lt;n; i--)

                    but I think that will actually print 21 dashes, not 20.

                    Rob, you're a genius.
                    Blah blah blah nick blah blah confusion, blah blah blah blah frog.

                    Comment


                    • #25
                      wow Rob

                      LOL@Ribbit!

                      Comment


                      • #26
                        Originally posted by Ribbit
                        FWIW, I figured you could also change the loop to:

                        for (i=40; i&lt;n; i--)

                        but I think that will actually print 21 dashes, not 20.

                        Rob, you're a genius.
                        if i=40 and n=20 how does i&lt;n run even once?

                        Fun problem, sorry I missed it.
                        chuck
                        Last edited by cjolley; 16 October 2003, 17:21.
                        Chuck
                        秋音的爸爸

                        Comment


                        • #27
                          Sorry Ribbit, Chuck's right. That wouldn't execute ever.

                          - 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


                          • #28
                            A friend of mine got Ribbit's solution too. It seemed right at first
                            <font size="-4">User error:
                            Replace user and try again.
                            System 1: P4 2.8@3.25, P4C800-E Deluxe, 1024MB 3200 CL2, 160+120 GB WD, XP Pro, Skystar 2, Matrox Parhelia 128R, Chieftec Dragon Full Tower (Silver).
                            System 2: P4 2.0, Intel 845, 1024MB Generic RAM, 80GB WD, XP Pro, Promise Ultra133 TX2, GF3 Ti500. Resides in a neat Compaq case.
                            </font>

                            Comment


                            • #29

                              n3rd4l3rt!!!!!!!

                              Comment


                              • #30
                                another way might be even be the simplest

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

                                how about the above? my C is rusty but just changing n for i for the decrement should work.. no compiler with me so u have to check for me..

                                Edit: home at last to explain the logic
                                after you decrease n 20 times, u will have printed "-" 20 times and by now n=0 as the printf statement is executed before the n-- ,
                                and since i=0, i < n would cause the loop to end as zero is not equal to zero, this is a kind of reverse logic
                                Last edited by Belwarrior; 17 October 2003, 05:09.
                                Life is a bed of roses. Everyone else sees the roses, you are the one being gored by the thorns.

                                AMD PhenomII555@B55(Quadcore-3.2GHz) Gigabyte GA-890FXA-UD5 Kingston 1x2GB Generic 8400GS512MB WD1.5TB LGMulti-Drive Dell2407WFP
                                ***Matrox G400DH 32MB still chugging along happily in my other pc***

                                Comment

                                Working...
                                X