Recent Changes

Saturday, May 29

  1. page Chapter 8 - The UNIX System Interface edited TODO Please note: these solutions use the Unix system interface, so they're not portable. Never…
    TODO
    Please note: these solutions use the Unix system interface, so they're not portable. Nevertheless, they're in K&R, so they appear here despite their inherent non-portability. In any case, they will become portable as soon as everyone's using Unix, as God intended. [Heathfield]
    Ex No
    Pg No
    Exercise
    Solution
    Solved by
    1
    174
    Rewrite the program cat from Chapter 7 using read , write , open and close instead of their standard library equivalents. Perform experiments to determine the relative speeds of the two versions.
    Listing krx801
    Andrew Tesker
    2
    178
    Rewrite fopen and _fillbuf with fields instead of explicit bit operations. Compare code size and execution speed.
    No solution yet posted.
    3
    179
    Design and write _flushbuf , fflush and fclose .
    Listing krx803
    Gregory Pietsch
    4
    179
    The standard library function
    int fseek(FILE *fp, long offset, int origin)
    is identical to lseek except that fp is a file pointer instead of a file descriptor and the return value is an int status, not a position. Write fseek . Make sure that your fseek coordinates properly with the buffering done for the other functions of the library.
    Listing krx804
    Gregory Pietsch
    5
    184
    Modify the fsize program to print the other information contained in the inode entry.
    No solution yet posted.
    6
    189
    The standard library function calloc(n,size) returns a pointer to n objects of size size , with the storage initialized to zero. Write calloc , by calling malloc or by modifying it.
    Listing krx806
    Bryan Williams
    7
    189
    malloc accepts a size request without checking its plausibility; free believes that the block it is asked to free contains a valid size field. Improve these routines so they take more pains with error checking.
    No solution yet posted.
    8
    189
    Write a routine bfree(p,n) that will free an arbitrary block p of n characters into the free list maintained by malloc and free . By using bfree , a user can add a static or external array to the free list at any time.
    No solution yet posted.

    (view changes)
    9:38 am
  2. page Chapter 7 - Input and Output edited TODO Ex No Pg No Exercise Solution Solved by 1 153 Write a program that converts upper ca…
    TODO Ex No
    Pg No
    Exercise
    Solution
    Solved by
    1
    153
    Write a program that converts upper case to lower or lower case to upper, depending on the name it is invoked with, as found in argv[0].
    Listing krx701
    Richard Heathfield, Bryan Williams
    2
    155
    Write a program that will print arbitrary input in a sensible way. As a minimum, it should print non-graphic characters in octal or hexadecimal according to local custom, and break long text lines.
    Listing krx702
    Me
    3
    156
    Revise minprintf to handle more of the facilities of printf .
    Listing krx703
    Gregory Pietsch
    4
    159
    Write a private version of scanf analogous to minprintf from the previous section.
    No solution yet posted.
    5
    159
    Rewrite the postfix calculator of Chapter 4 to use scanf and/or sscanf to do the input and number conversion.
    No solution yet posted.
    6
    165
    Write a program to compare two files, printing the first line where they differ.
    Listing krx706
    "flippant_squirrel"(!)
    7
    165
    Modify the pattern finding program of Chapter 5 to take its input from a set of named files or, if no files are named as arguments, from the standard input. Should the file name be printed when a matching line is found?
    No solution yet posted.
    8
    165
    Write a program to print a set of files, starting each new one on a new page, with a title and a running page count for each file.
    Listing krx708
    Steven Huang
    9
    168
    Functions like isupper can be implemented to save space or to save time. Explore both possibilities.
    Listing krx709
    Gregory Pietsch

    (view changes)
    9:38 am
  3. page Chapter 5 - Pointers and Arrays edited ... 13 118 ... so that <span style="color: #008000;">tail</span> <s…
    ...
    13
    118
    ...
    so that
    <span style="color: #008000;">tail</span> <span style="color: #000080;">-</span><span style="color: #008000;">n</span>
    "tail -n"
    prints the last n lines. The program should behave rationally no matter how unreasonable the input or the value of n. Write the program so it makes the best use of available storage; lines should be stored as in the sorting program of Section 5.6, not in a two-dimensional array of fixed size.
    Listing krx513
    (view changes)
    9:37 am
  4. page Chapter 5 - Pointers and Arrays edited ... 126 Expand dcl to handle declarations with function argument types, qualifiers like const , a…
    ...
    126
    Expand dcl to handle declarations with function argument types, qualifiers like const , and so on.
    No solution yet posted.
    (view changes)
    9:36 am
  5. page Chapter 6 - Structures edited TODO Ex No Pg No Exercise Solution Solved by 1 136 Our version of getword does not proper…
    TODO Ex No
    Pg No
    Exercise
    Solution
    Solved by
    1
    136
    Our version of getword does not properly handle underscores, string constants, comments, or preprocessor control lines. Write a better version.
    Listing krx601
    Ben Pfaff
    2
    143
    Write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters but different somewhere thereafter. Don't count words within strings and comments. Make 6 a parameter that can be set from the command line.
    No solution yet posted.
    3
    143
    Write a cross-referencer that prints a list of all words in a document, and, for each word, a list of the line numbers on which it occurs. Remove noise words like "the," "and," and so on.
    Listing krx603
    Me
    4
    143
    Write a program that prints the distinct words in its input sorted into decreasing order of frequency of occurrence. Precede each word by its count.
    Listing krx604
    Bryan Williams
    5
    145
    Write a function undef that will remove a name and definition from the table maintained by lookup and install .
    Listing krx605
    Paul Griffiths, Gregory Pietsch
    6
    145
    Implement a simple version of the #define processor (i.e., no arguments) suitable for use with C programs, based on the routines of this section. You may also find getch and ungetch helpful.
    No solution yet posted.

    (view changes)
    9:36 am
  6. page Chapter 5 - Pointers and Arrays edited TODO Ex No Pg No Exercise Solution Solved by 1 97 As written, getint treats a + or - not …
    TODO Ex No
    Pg No
    Exercise
    Solution
    Solved by
    1
    97
    As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input.
    Listing krx501
    Gregory Pietsch
    2
    97
    Write getfloat , the floating-point analog of getint . What type does getfloat return as its function value?
    Listing krx502
    Chris Mears, Gregory Pietsch
    3
    107
    Write a pointer version of the function strcat that we showed in Chapter 2: strcat(s,t) copies the string t to the end of s .
    Listing krx503
    Me
    4
    107
    Write the function strend(s,t) , which returns 1 if the string t occurs at the end of the string s , and zero otherwise.
    Listing krx504
    Bryan Williams
    5
    107
    Write versions of the library functions strncpy , strncat , and strncmp , which operate on at most the first n characters of their argument strings. For example, strncpy(s,t,n) copies at most n characters of t to s . Full descriptions are in Appendix B.
    Listing krx505
    Lars Wirzenius
    6
    107
    Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline (Chapters 1 and 4), atoi , itoa , and their variants (Chapters 2, 3, and 4), reverse (Chapter 3), and strindex and getop (Chapter 4).
    Listing krx506
    Gregory Pietsch
    7
    110
    Rewrite readlines to store lines in an array supplied by main , rather than calling alloc to maintain storage. How much faster is the program?
    Listing krx507
    Steven Huang
    8
    112
    There is no error-checking in day_of_year or month_day. Remedy this defect.
    Listing krx508
    Lars Wirzenius
    9
    114
    Rewrite the routines day_of_year and month_day with pointers instead of indexing.
    Listing krx509
    Lars Wirzenius, Gregory Pietsch
    10
    118
    Write the program expr , which evaluates a reverse Polish expression from the command line, where each operator or operand is a separate argument. For example,
    expr 2 3 4 + *
    evaluates 2 X (3+4).
    Listing krx510
    Lars Wirzenius
    11
    118
    Modify the programs entab and detab (written as exercises in Chapter 1) to accept a list of tab stops as arguments. Use the default tab settings if there are no arguments.
    Listing krx511
    Gregory Pietsch
    12
    118
    Extend entab and detab to accept the shorthand
    entab -m +n to mean tab stops every n columns, starting at column m . Choose convenient (for the user) default behavior.
    No solution yet posted.
    13
    118
    Write the program tail, which prints the last n lines of its input. By default, n is 10, say, but it can be changed by an optional argument, so that
    <span style="color: #008000;">tail</span> <span style="color: #000080;">-</span><span style="color: #008000;">n</span>
    prints the last n lines. The program should behave rationally no matter how unreasonable the input or the value of n. Write the program so it makes the best use of available storage; lines should be stored as in the sorting program of Section 5.6, not in a two-dimensional array of fixed size.
    Listing krx513
    Gregory Pietsch, Steven Huang
    14
    121
    Modify the sort program to handle a -r flag, which indicates sorting in reverse (decreasing) order. Be sure that -r works with -n.
    Listing krx514
    Steven Huang
    15
    121
    Add the option -f to fold upper and lower case together, so that case distinctions are not made during sorting; for example, a and A compare equal.
    No solution yet posted.
    16
    121
    Add the -d ("directory order") option, which makes comparisons only on letters, numbers and blanks. Make sure it works in conjunction with -f .
    No solution yet posted.
    17
    121
    Add a field-handling capability, so sorting may be done on fields within lines, each field sorted according to an independent set of options. (The index for this book was sorted with -df for the index category and -n for the page numbers.)
    No solution yet posted.
    18
    126
    Make dcl recover from input errors.
    No solution yet posted.
    19
    126
    Modify undcl so that it does not add redundant parentheses to declarations.
    No solution yet posted.
    20
    126
    Expand dcl to handle declarations with function argument types, qualifiers like const , and so on.

    (view changes)
    9:35 am
  7. page Chapter 4 - Functions and Program Structure edited TODO If you are intending to attempt solutions 4-3 through 4-10, you will find testing your solut…
    TODOIf you are intending to attempt solutions 4-3 through 4-10, you will find testing your solution to be a somewhat less laborious task if you download the following code:
    Kernighan and Ritchie's Polish calculator source
    It was when I was typing up this lot that the problem of copyright first occurred to me - after all, it's quite a substantial quote from the book. Fortunately for me, Dennis Ritchie had no objections (but he did check to make sure there wouldn't be any other verbatim quotes of comparable size!).
    Ex No
    Pg No
    Exercise
    Solution
    Solved by
    1
    71
    Write the function strrindex(s,t) , which returns the position of the rightmost occurrence of t in s , or -1 if there is none.
    Listing krx401
    Rick Dearman
    2
    73
    Extend atof to handle scientific notation of the form 123.45e-6 where a floating-point number may be followed by e or E and an optionally signed exponent.
    Listing krx402
    Dann Corbit
    3
    79
    Given the basic framework, it's straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.
    Listing krx403
    Bob Wightman
    4
    79
    Add commands to print the top element of the stack without popping, to duplicate it, and to swap the top two elements. Add a command to clear the stack.
    Listing krx404
    Bob Wightman
    5
    79
    Add access to library functions like sin , exp , and pow . See <math.h> in Appendix B, Section 4.
    Listing krx405
    Bob Wightman
    6
    79
    Add commands for handling variables. (It's easy to provide twenty-six variables with single-letter names.) Add a variable for the most recently printed value.
    Listing krx406
    Cat 1 Solution by Bob Wightman
    7
    79
    Write a routine ungets(s) that will push back an entire string onto the input. Should ungets know about buf and bufp , or should it just use ungetch ?
    Listing krx407
    Steven Huang
    8
    79
    Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.
    Listing krx408
    Steven Huang
    9
    79
    Our getch and ungetch do not handle a pushed-back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement your design.
    No solution yet posted.
    10
    79
    An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. Revise the calculator to use this approach.
    No solution yet posted.
    11
    83
    Modify getop so that it doesn't need to use ungetch. Hint: use an internal static variable.
    Listing krx411
    Gregory Pietsch
    12
    88
    Adapt the ideas of printd to write a recursive version of itoa ; that is, convert an integer into a string by calling a recursive routine.
    Listing krx412
    Gregory Pietsch
    13
    88
    Write a recursive version of the function reverse(s) , which reverses the string s in place.
    Listing krx413
    Gregory Pietsch
    14
    91
    Define a macro swap(t,x,y) that interchanges two arguments of type t . (Block structure will help.)
    Listing krx414
    Gregory Pietsch, Lars Wirzenius

    (view changes)
    9:35 am
  8. page Chapter 3 - Control Flow edited TODO Ex No Pg No Exercise Solution Solved by 1 58 Our binary search makes two tests insid…
    TODO Ex No
    Pg No
    Exercise
    Solution
    Solved by
    1
    58
    Our binary search makes two tests inside the loop, when one would suffice (at the price of more tests outside). Write a version with only one test inside the loop and measure the difference in run-time.
    Listing krx301
    Paul Griffiths, Colin Barker, Andrew Tesker
    2
    60
    Write a function escape(s,t) that converts characters like newline and tab into visible escape sequences like \n and \t as it copies the string t to s . Use a switch . Write a function for the other direction as well, converting escape sequences into the real characters.
    Listing krx302
    Paul Griffiths
    3
    63
    Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc...xyz in s2 . Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z . Arrange that a leading or trailing - is taken literally.
    Listing krx303
    Paul Griffiths
    4
    64
    In a two's complement number representation, our version of itoa does not handle the largest negative number, that is, the value of n equal to -(2 to the power (wordsize - 1)) . Explain why not. Modify it to print that value correctly regardless of the machine on which it runs.
    Listing krx304
    Paul Griffiths
    5
    64
    Write the function itob(n,s,b) that converts the integer n into a base b character representation in the string s . In particular, itob(n,s,16) formats n as a hexadecimal integer in s .
    Listing krx305
    Paul Griffiths
    6
    64
    Write a version of itoa that accepts three arguments instead of two. The third argument is a minimum field width; the converted number must be padded with blanks on the left if necessary to make it wide enough.

    (view changes)
    9:34 am
  9. page Chapter 2 - Types, Operators and Expressions edited TODO Ex No Pg No Exercise Solution Solved by 1 36 Write a program to determine the ranges…
    TODO Ex No
    Pg No
    Exercise
    Solution
    Solved by
    1
    36
    Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.
    Listing krx201
    Rick Dearman
    2
    42
    Write a loop equivalent to the for loop above without using && or
    .
    Listing krx202
    "flippant_squirrel" (!) and Craig Schroeder
    3
    46
    Write the function htoi(s) , which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F .
    Listing krx203
    Me
    4
    48
    Write an alternate version of squeeze(s1,s2) that deletes each character in the string s1 that matches any character in the string s2 .
    Listing krx204
    Me
    5
    48
    Write the function any(s1,s2) , which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2 . (The standard library function strpbrk does the same job but returns a pointer to the location.)
    Listing krx205
    The pedestrian solution is mine, and the clever one is by Partha Seetala.
    6
    49
    Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.
    Listing krx206
    Me
    7
    49
    Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.
    Listing krx207
    Gregory Pietsch
    8
    49
    Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n bit positions.
    Listing krx208
    Gregory Pietsch
    9
    51
    In a two's complement number system, x &= (x-1) deletes the rightmost 1-bit in x . Explain why. Use this observation to write a faster version of bitcount .
    Listing krx209
    Gregory Pietsch
    10
    52
    Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else .

    (view changes)
    9:34 am
  10. page Chapter 1 - A Tutorial Introduction edited Chapter 1 - A Tutorial Introduction Ex Ex Pg Exercise ... 20 34 ... say every n c…

    Chapter 1 - A Tutorial Introduction
    Ex
    Ex
    Pg
    Exercise
    ...
    20
    34
    ...
    say every n columns.n columns. Should n ben be a variable
    Listing krx120
    Richard Heathfield
    ...
    22
    34
    ...
    before the n -thn -th column of
    Listing krx122
    Category 1 Solution by Rick Dearman
    ...
    Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments do not nest.
    Listing krx123
    ...
    were submitted enen masse fromfrom comp.lang.c, and
    24
    34
    (view changes)
    9:33 am

More