Solutions for the Kernighan and Ritchie

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

    #1

    Solutions for the Kernighan and Ritchie

    Hi everyone,

    I've read the Kernighan and Ritchie some time ago, and then again, but
    this time I've done the full set of exercises. I figured it would be
    nice to share them :



    Your comments are welcome.

    I've of course didn't peek in Tondo and Gimpel's book or Richard
    Heathfield.

    Regards,
  • freeasinfreedom

    #2
    Re: Solutions for the Kernighan and Ritchie

    On Oct 2, 6:10 pm, another...@gmai l.com wrote:
    Hi everyone,
    >
    I've read the Kernighan and Ritchie some time ago, and then again, but
    this time I've done the full set of exercises. I figured it would be
    nice to share them :
    >

    >
    Your comments are welcome.
    >
    I've of course didn't peek in Tondo and Gimpel's book or Richard
    Heathfield.
    >
    Regards,
    Nice!

    Since you like sharing your knowledge, you might be interested in
    contributing to the free software community by adding you project to
    savannah (savannah.gnu.o rg). In this way you could educate many
    people.

    Happy Hacking!

    Comment

    • user923005

      #3
      Re: Solutions for the Kernighan and Ritchie

      On Oct 2, 9:10 am, another...@gmai l.com wrote:
      Hi everyone,
      >
      I've read the Kernighan and Ritchie some time ago, and then again, but
      this time I've done the full set of exercises. I figured it would be
      nice to share them :
      >

      >
      Your comments are welcome.
      >
      I've of course didn't peek in Tondo and Gimpel's book or Richard
      Heathfield.
      It might be nice to merge these solutions with Richard's collection
      that is online.

      Comment

      • Richard Heathfield

        #4
        Re: Solutions for the Kernighan and Ritchie

        user923005 said:
        On Oct 2, 9:10 am, another...@gmai l.com wrote:
        >Hi everyone,
        >>
        >I've read the Kernighan and Ritchie some time ago, and then again, but
        >this time I've done the full set of exercises. I figured it would be
        >nice to share them :
        >>
        >http://linkefong.free.fr/downloads/t...utions-1.0.zip
        >>
        >Your comments are welcome.
        >>
        >I've of course didn't peek in Tondo and Gimpel's book or Richard
        >Heathfield.
        >
        It might be nice to merge these solutions with Richard's collection
        that is online.
        It's true that I started the collection, but I have long since palmed it
        off onto the clc wiki, which may be found at this URL:


        The K&R2 solutions page is: http://clc-wiki.net/wiki/K%26R2_solutions

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • anotherlin@gmail.com

          #5
          Re: Solutions for the Kernighan and Ritchie

          I'll be glad to see them added to the clc-wiki. The admin has my
          blessing to upload them.

          I've basically answered all exercises, even those missing from
          Richard's collection.

          But first, is there somebody to check them for correctness? Any
          comments?

          Comment

          • Flash Gordon

            #6
            Re: Solutions for the Kernighan and Ritchie

            anotherlin@gmai l.com wrote, On 02/10/08 23:09:
            I'll be glad to see them added to the clc-wiki. The admin has my
            blessing to upload them.
            Thanks for getting his/her blessing first. Please include a link to the
            original site when uploading them.
            I've basically answered all exercises, even those missing from
            Richard's collection.
            >
            But first, is there somebody to check them for correctness? Any
            comments?
            After uploading them they can be edited. So I would suggest upoading
            them then posting links to them here asking for comment.
            --
            Flash Gordon
            If spamming me sent it to smap@spam.cause way.com
            If emailing me use my reply-to address
            See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

            Comment

            • Ben Bacarisse

              #7
              Re: Solutions for the Kernighan and Ritchie

              anotherlin@gmai l.com writes:
              Hi everyone,
              >
              I've read the Kernighan and Ritchie some time ago, and then again, but
              this time I've done the full set of exercises. I figured it would be
              nice to share them :
              >

              >
              Your comments are welcome.
              Looks good. That was a quick glance and if I have time I'll look
              again. On one style issue, I was baffled by the large number of blank
              lines. I won't make a fuss about style but it looked odd to me.

              I spent a bit more time on one that I am currently familiar with (the
              RPN calculator from chapter 4) and I am disappointed to see you also
              have lost the original (correct in my opinion) K&R design of passing
              getop the buffer size so you can prevent overflow. I'd count this as
              a error.

              --
              Ben.

              Comment

              • user923005

                #8
                Re: Solutions for the Kernighan and Ritchie

                On Oct 2, 9:10 am, another...@gmai l.com wrote:
                Hi everyone,
                >
                I've read the Kernighan and Ritchie some time ago, and then again, but
                this time I've done the full set of exercises. I figured it would be
                nice to share them :
                >

                >
                Your comments are welcome.
                >
                I've of course didn't peek in Tondo and Gimpel's book or Richard
                Heathfield.
                The test driver for exercise 4.2 does not test the atof() function:

                int main ()

                {
                printf("%g\n", 123.45e-6);
                return 0;
                }

                Probably, you meant to do this:

                #include <stdio.h>
                int main ()

                {
                printf("%g\n", atof("123.45e-6"));
                return 0;
                }

                Comment

                • Richard Bos

                  #9
                  Re: Solutions for the Kernighan and Ritchie

                  freeasinfreedom <faifgnu@gmail. comwrote:
                  On Oct 2, 6:10 pm, another...@gmai l.com wrote:
                  I've read the Kernighan and Ritchie some time ago, and then again, but
                  this time I've done the full set of exercises. I figured it would be
                  nice to share them :

                  http://linkefong.free.fr/downloads/t...utions-1.0.zip
                  [ Edited for honesty ]
                  Since you like sharing your knowledge, you might be interested in
                  contributing to the free software community by adding you project to
                  spammy (spamspam.spam. gnu.org).
                  Or rather, do not, since I cannot find an Earthly reason why anyone
                  would think answers to K&R exercises have anything to do with the
                  contents of that page.

                  Freedom? Freedom to advertise where it isn't wanted, apparently.

                  Richard

                  Comment

                  • anotherlin@gmail.com

                    #10
                    Re: Solutions for the Kernighan and Ritchie

                    Looks good.  That was a quick glance and if I have time I'll look
                    again.  On one style issue, I was baffled by the large number of blank
                    lines.  I won't make a fuss about style but it looked odd to me.
                    Well, I prefer things well separated. I use vi inside an 80x70
                    characters
                    xterm so there doesn't seem to be so much of them. I agree things can
                    look
                    differently and styles are a matter of personal taste.
                    I spent a bit more time on one that I am currently familiar with (the
                    RPN calculator from chapter 4) and I am disappointed to see you also
                    have lost the original (correct in my opinion) K&R design of passing
                    getop the buffer size so you can prevent overflow.  I'd count this as
                    a error.
                    I started with the getop() function p78 of my 2nd edition book (41st
                    printing)
                    and there is no buffer size (prototype is int getop (char s[])). In
                    fact, none
                    of the exercises of this chapter specifically ask for checking buffer
                    overflow.
                    However, I agree that it would have been better a better answer.

                    Comment

                    • anotherlin@gmail.com

                      #11
                      Re: Solutions for the Kernighan and Ritchie

                      The test driver for exercise 4.2 does not test the atof() function:
                      [...]

                      Yes, you're right. I made a mistake and actually the atof() is not
                      correctly
                      tested and it has a bug! :( The exponent is not handled correctly, it
                      can be
                      fixed easily however. Thank you for pointing out this thing.

                      Comment

                      • anotherlin@gmail.com

                        #12
                        Re: Solutions for the Kernighan and Ritchie

                        I'll be glad to see them added to the clc-wiki. The admin has my
                        blessing to upload them.
                        Thanks for getting his/her blessing first. Please include a link to the
                        original site when uploading them.
                        Ok, English is not my main language (I live in France). By "blessing",
                        I
                        meant "I authorize the admin to put the content of the zip archive on
                        the
                        clc-wiki. I do so very *happily*." Hence the "blessing". I apologize
                        if I
                        may have offended you.

                        I read wikis a lot for various topics. But so far, I do not know how
                        to
                        write on them. And besides, it may be good first to check if the
                        proposed
                        answers are correct (exercise 4-2 has a bug as pointed out) before
                        putting
                        them on the wiki? Unless, the way is to put on the wiki and then
                        correct
                        them as needed?

                        Comment

                        • Ben Bacarisse

                          #13
                          Re: Solutions for the Kernighan and Ritchie

                          anotherlin@gmai l.com writes:
                          <snip>
                          >I spent a bit more time on one that I am currently familiar with (the
                          >RPN calculator from chapter 4) and I am disappointed to see you also
                          >have lost the original (correct in my opinion) K&R design of passing
                          >getop the buffer size so you can prevent overflow.  I'd count this as
                          >a error.
                          >
                          I started with the getop() function p78 of my 2nd edition book (41st
                          printing)
                          and there is no buffer size (prototype is int getop (char s[])). In
                          fact, none
                          of the exercises of this chapter specifically ask for checking buffer
                          overflow.
                          However, I agree that it would have been better a better answer.
                          Well that explains why so many people have the same "fault". In my
                          K&R (I don't have the 2nd edition) getop takes a char * and an int.
                          Hardly surprising, the code check to ensure that the buffer passed is
                          never overrun.

                          I can't imagine why they'd go backwards, but it solves the mystery of
                          so many solutions that look, to me, like backward steps!

                          --
                          Ben.

                          Comment

                          • Flash Gordon

                            #14
                            Re: Solutions for the Kernighan and Ritchie

                            anotherlin@gmai l.com wrote, On 03/10/08 20:31:
                            >>I'll be glad to see them added to the clc-wiki. The admin has my
                            >>blessing to upload them.
                            >Thanks for getting his/her blessing first. Please include a link to the
                            >original site when uploading them.
                            >
                            Ok, English is not my main language (I live in France). By "blessing",
                            I
                            meant "I authorize the admin to put the content of the zip archive on
                            the
                            clc-wiki. I do so very *happily*." Hence the "blessing". I apologize
                            if I
                            may have offended you.
                            There was no offence taken. I miss-read what you said as saying that the
                            admin of the site where they currently reside had given his/her blessing.

                            Oh, and I only have admin access because I host it. I don't have any
                            special control in terms of what is posted to the Wiki.
                            I read wikis a lot for various topics. But so far, I do not know how
                            to
                            write on them. And besides, it may be good first to check if the
                            proposed
                            answers are correct (exercise 4-2 has a bug as pointed out) before
                            putting
                            them on the wiki? Unless, the way is to put on the wiki and then
                            correct
                            them as needed?
                            Wiki's are editable, so you can put them up and correct them. You do
                            have to create yourself an account first.
                            --
                            Flash Gordon
                            If spamming me sent it to smap@spam.cause way.com
                            If emailing me use my reply-to address
                            See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

                            Comment

                            Working...