small C puzzles

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • madhav_a_kelkar@hotmail.com

    small C puzzles

    i was browsing this problem:

    The following is a piece of C code, whose intention was to print a
    minus sign 20 times. But you can notice that, it doesn't work.

    #include <stdio.h>
    int main()
    {
    int i;
    int n = 20;
    for( i = 0; i < n; i-- )
    printf("-");
    return 0;
    }

    Well fixing the above code is straight-forward. To make the problem
    interesting, you have to fix the above code, by changing exactly one
    character. There are three known solutions. See if you can get all
    those three.


    one solution is to use n-- instead of i--. please let me know the
    other two.
  • William L. Bahn

    #2
    Re: small C puzzles


    <madhav_a_kelka r@hotmail.com> wrote in message
    news:338e571.04 09010104.1f5351 2d@posting.goog le.com...[color=blue]
    > i was browsing this problem:
    >
    > The following is a piece of C code, whose intention was to[/color]
    print a[color=blue]
    > minus sign 20 times. But you can notice that, it doesn't work.
    >
    > #include <stdio.h>
    > int main()
    > {
    > int i;
    > int n = 20;
    > for( i = 0; i < n; i-- )
    > printf("-");
    > return 0;
    > }
    >
    > Well fixing the above code is straight-forward. To make the[/color]
    problem[color=blue]
    > interesting, you have to fix the above code, by changing[/color]
    exactly one[color=blue]
    > character. There are three known solutions. See if you can get[/color]
    all[color=blue]
    > those three.
    >
    >
    > one solution is to use n-- instead of i--. please let me know[/color]
    the[color=blue]
    > other two.[/color]

    Sure....Tell the obvious one...;)

    One is to change i < n to -i < n

    Using ~i will almost work - and would on an implementation that
    uses one's compliment.

    The other is to change the test to i + n

    Good little logic puzzle.




    Comment

    • Randy Howard

      #3
      Re: small C puzzles

      In article <10jbf5p4av35tc 5@corp.supernew s.com>, william@toomuch spam.net
      says...[color=blue]
      >
      > <madhav_a_kelka r@hotmail.com> wrote in message
      > news:338e571.04 09010104.1f5351 2d@posting.goog le.com...[color=green]
      > > i was browsing this problem:
      > >
      > > The following is a piece of C code, whose intention was to[/color]
      > print a[color=green]
      > > minus sign 20 times. But you can notice that, it doesn't work.
      > >
      > > #include <stdio.h>
      > > int main()
      > > {
      > > int i;
      > > int n = 20;
      > > for( i = 0; i < n; i-- )
      > > printf("-");
      > > return 0;
      > > }
      > >
      > > Well fixing the above code is straight-forward. To make the[/color]
      > problem[color=green]
      > > interesting, you have to fix the above code, by changing[/color]
      > exactly one[color=green]
      > > character. There are three known solutions. See if you can get[/color]
      > all[color=green]
      > > those three.
      > >
      > >
      > > one solution is to use n-- instead of i--. please let me know[/color]
      > the[color=green]
      > > other two.[/color]
      >
      > Sure....Tell the obvious one...;)
      >
      > One is to change i < n to -i < n
      >
      > Using ~i will almost work - and would on an implementation that
      > uses one's compliment.
      >
      > The other is to change the test to i + n
      >
      > Good little logic puzzle.[/color]

      Except, as Dan pointed out in the "earlier" version of this same thing
      it still is guaranteed to work in the form above.

      --
      Randy Howard
      To reply, remove FOOBAR.

      Comment

      • anonymous

        #4
        Re: small C puzzles

        > one solution is to use n-- instead of i--. please let me know the[color=blue]
        > other two.[/color]

        One more:

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

        Using bitwise negation.

        Comment

        • Christopher Benson-Manica

          #5
          Re: small C puzzles

          madhav_a_kelkar @hotmail.com spoke thus:
          [color=blue]
          > Well fixing the above code is straight-forward. To make the problem
          > interesting, you have to fix the above code, by changing exactly one
          > character. There are three known solutions. See if you can get all
          > those three.[/color]

          <pedantry>Actua lly, as stated, the problem is impossible - the
          solutions involve *inserting* a character; I read "changing" to be an
          operation that can be done in replace mode in one's editor of
          choice.</pedantry>

          --
          Christopher Benson-Manica | I *should* know what I'm talking about - if I
          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

          Comment

          • Robert Gamble

            #6
            Re: small C puzzles

            On Wed, 01 Sep 2004 17:17:22 +0000, Christopher Benson-Manica wrote:
            [color=blue]
            > madhav_a_kelkar @hotmail.com spoke thus:
            >[color=green]
            >> Well fixing the above code is straight-forward. To make the problem
            >> interesting, you have to fix the above code, by changing exactly one
            >> character. There are three known solutions. See if you can get all
            >> those three.[/color]
            >
            > <pedantry>Actua lly, as stated, the problem is impossible - the
            > solutions involve *inserting* a character; I read "changing" to be an
            > operation that can be done in replace mode in one's editor of
            > choice.</pedantry>[/color]

            Which of the 3 solutions requires inserting a character instead of
            changing a chracater, or can your editor not change a space character to
            something else?

            Rob Gamble

            Comment

            • Christopher Benson-Manica

              #7
              Re: small C puzzles

              Robert Gamble <rob_gamble99@h otmail.com> spoke thus:
              [color=blue]
              > Which of the 3 solutions requires inserting a character instead of
              > changing a chracater, or can your editor not change a space character to
              > something else?[/color]

              Being pedantic is no fun when you turn out to be wrong... although
              changing a space to a non-space would destroy the stylistic unity of
              the program, so I'll take a smidgen of solace from that. Sorry.

              --
              Christopher Benson-Manica | I *should* know what I'm talking about - if I
              ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

              Comment

              • Keith Thompson

                #8
                Re: small C puzzles

                Christopher Benson-Manica <ataru@nospam.c yberspace.org> writes:[color=blue]
                > madhav_a_kelkar @hotmail.com spoke thus:
                >[color=green]
                > > Well fixing the above code is straight-forward. To make the problem
                > > interesting, you have to fix the above code, by changing exactly one
                > > character. There are three known solutions. See if you can get all
                > > those three.[/color]
                >
                > <pedantry>Actua lly, as stated, the problem is impossible - the
                > solutions involve *inserting* a character; I read "changing" to be an
                > operation that can be done in replace mode in one's editor of
                > choice.</pedantry>[/color]

                The solutions involving changing "i" to "-i" or "~i" can be done by
                replacing the preceding space.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                • Bigdakine

                  #9
                  Re: small C puzzles

                  >Subject: Re: small C puzzles[color=blue]
                  >From: Christopher Benson-Manica ataru@nospam.cy berspace.org
                  >Date: 9/1/04 7:17 AM Hawaiian Standard Time
                  >Message-id: <ch5072$jq$1@ch essie.cirr.com>
                  >
                  >madhav_a_kelka r@hotmail.com spoke thus:
                  >[color=green]
                  >> Well fixing the above code is straight-forward. To make the problem
                  >> interesting, you have to fix the above code, by changing exactly one
                  >> character. There are three known solutions. See if you can get all
                  >> those three.[/color]
                  >
                  ><pedantry>Actu ally, as stated, the problem is impossible - the
                  >solutions involve *inserting* a character; I read "changing" to be an
                  >operation that can be done in replace mode in one's editor of
                  >choice.</pedantry>
                  >[/color]

                  Now you tell me.

                  Stuart
                  Dr. Stuart A. Weinstein
                  Ewa Beach Institute of Tectonics
                  "To err is human, but to really foul things up requires a creationist"


                  "Creationis ts aren't impervious to Logic: They're oblivious to it."

                  Comment

                  • William L. Bahn

                    #10
                    Re: small C puzzles


                    "anonymous" <iunknown2k4@ya hoo.co.in> wrote in message
                    news:f7d84cf3.0 409010836.47e2c f2d@posting.goo gle.com...[color=blue][color=green]
                    > > one solution is to use n-- instead of i--. please let me know[/color][/color]
                    the[color=blue][color=green]
                    > > other two.[/color]
                    >
                    > One more:
                    >
                    > for (i = 0; ~i < n; i--)
                    >
                    > Using bitwise negation.[/color]

                    On the vast majority of machines, this will not produce the
                    intended results since bitwise negation and multiplying by
                    negative one are not the same thing.



                    Comment

                    • William L. Bahn

                      #11
                      Re: small C puzzles


                      "Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote
                      in message news:ch5072$jq$ 1@chessie.cirr. com...[color=blue]
                      > madhav_a_kelkar @hotmail.com spoke thus:
                      >[color=green]
                      > > Well fixing the above code is straight-forward. To make the[/color][/color]
                      problem[color=blue][color=green]
                      > > interesting, you have to fix the above code, by changing[/color][/color]
                      exactly one[color=blue][color=green]
                      > > character. There are three known solutions. See if you can[/color][/color]
                      get all[color=blue][color=green]
                      > > those three.[/color]
                      >
                      > <pedantry>Actua lly, as stated, the problem is impossible - the
                      > solutions involve *inserting* a character; I read "changing" to[/color]
                      be an[color=blue]
                      > operation that can be done in replace mode in one's editor of
                      > choice.</pedantry>
                      >
                      > --
                      > Christopher Benson-Manica | I *should* know what I'm talking[/color]
                      about - if I[color=blue]
                      > ataru(at)cybers pace.org | don't, I need to know. Flames[/color]
                      welcome.

                      Huh?

                      Can't changing i-- to n-- can be done in replace mode?
                      Can't replacing a blank space with a minus sign be done in
                      replace mode?
                      Can't replacing a < sign with a + sign be done in replace mode?





                      Comment

                      • William L. Bahn

                        #12
                        Re: small C puzzles


                        "Randy Howard" <randyhoward@FO OverizonBAR.net > wrote in message
                        news:MPG.1b9fa7 685eb561eb9899c d@news.verizon. net...[color=blue]
                        > In article <10jbf5p4av35tc 5@corp.supernew s.com>,[/color]
                        william@toomuch spam.net[color=blue]
                        > says...[color=green]
                        > >
                        > > <madhav_a_kelka r@hotmail.com> wrote in message
                        > > news:338e571.04 09010104.1f5351 2d@posting.goog le.com...[color=darkred]
                        > > > i was browsing this problem:
                        > > >
                        > > > The following is a piece of C code, whose intention was to[/color]
                        > > print a[color=darkred]
                        > > > minus sign 20 times. But you can notice that, it doesn't[/color][/color][/color]
                        work.[color=blue][color=green][color=darkred]
                        > > >
                        > > > #include <stdio.h>
                        > > > int main()
                        > > > {
                        > > > int i;
                        > > > int n = 20;
                        > > > for( i = 0; i < n; i-- )
                        > > > printf("-");
                        > > > return 0;
                        > > > }
                        > > >[/color]
                        > >
                        > > Good little logic puzzle.[/color]
                        >
                        > Except, as Dan pointed out in the "earlier" version of this[/color]
                        same thing[color=blue]
                        > it still is guaranteed to work in the form above.[/color]

                        I guess I'm a bit dense, but when told that the purpose of a
                        piece of code is to print a minus sign 20 times I take that to
                        mean that printing it only 19 times or 21 times or 32769 times
                        doesn't qualify as working.

                        And, are the signed integers guaranteed to wrap around? The
                        unsigned ones are, but are the signed ones? Would anything
                        prevent the expression INT_MIN - 1 from evaluating to INT_MIN? I
                        don't have the standard handy.




                        Comment

                        • CBFalconer

                          #13
                          Re: small C puzzles

                          "William L. Bahn" wrote:[color=blue]
                          >[/color]
                          .... snip ...[color=blue]
                          >
                          > And, are the signed integers guaranteed to wrap around? The
                          > unsigned ones are, but are the signed ones? Would anything
                          > prevent the expression INT_MIN - 1 from evaluating to INT_MIN? I
                          > don't have the standard handy.[/color]

                          No. That is overflow and undefined behaviour.

                          --
                          A: Because it fouls the order in which people normally read text.
                          Q: Why is top-posting such a bad thing?
                          A: Top-posting.
                          Q: What is the most annoying thing on usenet and in e-mail?


                          Comment

                          • Randy Howard

                            #14
                            Re: small C puzzles

                            In article <10jdj7raos3tbb 6@corp.supernew s.com>, william@toomuch spam.net
                            says...[color=blue][color=green][color=darkred]
                            > > >
                            > > > Good little logic puzzle.[/color]
                            > >
                            > > Except, as Dan pointed out in the "earlier" version of this[/color]
                            > same thing[color=green]
                            > > it still is guaranteed to work in the form above.[/color]
                            >
                            > I guess I'm a bit dense, but when told that the purpose of a
                            > piece of code is to print a minus sign 20 times I take that to
                            > mean that printing it only 19 times or 21 times or 32769 times
                            > doesn't qualify as working.[/color]

                            Sorry, I made a rather bad typo above. It should read
                            "not guaranteed to work". Which, would match Dan's post.

                            --
                            Randy Howard
                            To reply, remove FOOBAR.

                            Comment

                            • William L. Bahn

                              #15
                              Re: small C puzzles


                              "Randy Howard" <randyhoward@FO OverizonBAR.net > wrote in message
                              news:MPG.1ba114 4053bae0b19899d 3@news.verizon. net...[color=blue]
                              > In article <10jdj7raos3tbb 6@corp.supernew s.com>,[/color]
                              william@toomuch spam.net[color=blue]
                              > says...[color=green][color=darkred]
                              > > > >
                              > > > > Good little logic puzzle.
                              > > >
                              > > > Except, as Dan pointed out in the "earlier" version of this[/color]
                              > > same thing[color=darkred]
                              > > > it still is guaranteed to work in the form above.[/color]
                              > >
                              > > I guess I'm a bit dense, but when told that the purpose of a
                              > > piece of code is to print a minus sign 20 times I take that[/color][/color]
                              to[color=blue][color=green]
                              > > mean that printing it only 19 times or 21 times or 32769[/color][/color]
                              times[color=blue][color=green]
                              > > doesn't qualify as working.[/color]
                              >
                              > Sorry, I made a rather bad typo above. It should read
                              > "not guaranteed to work". Which, would match Dan's post.[/color]

                              So then is "the form above" referring to the original code or one
                              of the three proposed solutions? If so, which one? Why wouldn't
                              it work?




                              Comment

                              Working...