]> zdv2.bktei.com Git - BK-2023-05.git/blob - src/tm-notes/ch3.tm
feat(src/tm-notes):Convert notes.tm into a book. Add exercise notes.
[BK-2023-05.git] / src / tm-notes / ch3.tm
1 <TeXmacs|2.1.1>
2
3 <project|book.tm>
4
5 <style|<tuple|book|style-bk>>
6
7 <\body>
8 <chapter|K&R Notes>
9
10 <section|Ch. 1: A Tutorial Introduction>
11
12 <subsection|Sec. 1.1: Getting Started>
13
14 \;
15
16 <\description>
17 <item*|Exercise 1-1>Run the \P<verbatim|hello, world>\Q program on your
18 system. Experiment with leaving out parts of the program, to see what
19 error messages you get.
20
21 <item*|Exercise 1-2>Experiment to find out what happens when
22 <cpp|printf>'s argument string contains <cpp|\\><em|c>, where <em|c> is
23 some character not listed above.
24 </description>
25
26 <subsection|Sec. 1.2: Variables and Arithmetic Expressions>
27
28 <\description>
29 <item*|Exercise 1-3>Modify the temperature conversion program to bring a
30 heading above the table.
31
32 <item*|Exercise 1-4>Write a program to bring the corresponding Celsius to
33 Fahrenheit table.
34 </description>
35
36 <subsection|Sec. 1.3: The For Statement>
37
38 <\description>
39 <item*|Exercise 1-5>Modify the temperature conversion program to print
40 the table in reverse order, that is, from <math|300> degrees to <math|0>.
41 </description>
42
43 <subsection|Sec. 1.4: Symbolic Constants>
44
45 <subsection|Sec. 1.5: Character Input and Output>
46
47 <subsubsection|Sec. 1.5.1: File Copying>
48
49 <\description>
50 <item*|Exercise 1-6>Verify that the expression <cpp|getchar() != EOF> is
51 <math|0> or <math|1>.
52
53 <item*|Exercise 1-7>Write a program to print the value of <cpp|EOF>.
54 </description>
55
56 <subsubsection|Sec. 1.5.2: Character Counting>
57
58 <subsubsection|Sec. 1.5.3: Line Counting>
59
60 <\description>
61 <item*|Exercise 1-8>Write a program to count blanks, tabs, and newlines.
62
63 <item*|Exercise 1-9>Write a program to copy its input to its output,
64 replacing each string of one or more blanks by a single blank.
65
66 <item*|Exercise 1-10>Write a program to copy its input to its output,
67 replacing each tab by <cpp|\\t>, each backspace by <cpp|\\b>, and each
68 backslash by <cpp|\\\\>. This makes tabs and backspaces visible in an
69 unambiguous way.
70 </description>
71
72 <subsubsection|Sec. 1.5.4: Word Counting>
73
74 <\description>
75 <item*|Exercise 1-11>How would you test the word program? What kinds of
76 input are most likely to uncover bugs if there are any?
77
78 <item*|Exercise 1-12>Write a program that prints its input one word per
79 line.
80 </description>
81
82 <subsection|Sec. 1.6: Arrays>
83
84 <\description>
85 <item*|Exercise 1-13>Write a program to print a histogram of the lengths
86 of words in its input. IT is easy to draw the histogram with the bars
87 horizontal; a vertical orientation is more challenging.
88
89 <item*|Exercise 1-14>Write a program to print a histogram of the
90 frequencies of different characters in its input.
91 </description>
92
93 <subsection|Sec 1.7: Functions>
94
95 <\description>
96 <item*|Exercise 1-15>Rewrite the temperature conversion program of
97 Section 1.2 to use a function for conversion.
98 </description>
99
100 <subsection|Sec 1.8: Arguments\UCall by Value>
101
102 <subsection|Sec 1.9: Character Arrays>
103
104 <\description>
105 <item*|Exercise 1-16>Revise the main routine of the longest-line program
106 so it will correctly print the length of arbitrarily long input lines,
107 and as much as possible of the text.
108
109 <item*|Exercise 1-17>Write a program to print all input lines that are
110 longer than <math|80> characters.
111
112 <item*|Exercise 1-18>Write a program to remove trailing blanks and tabs
113 from each line of input, and to delete entirely blank lines.
114
115 <item*|Exercise 1-19>Write a function <cpp|reverse(s)> that reverses the
116 character string <cpp|s>. Use it to write a program that reverses its
117 input a line at a time.
118 </description>
119
120 <subsection|Sec 1.10: External Variables and Scope>
121
122 <\description>
123 <item*|Exercise 1-20>Write a program <cpp|detab> that replaces tabs in
124 the input with the proper number of blanks to space to the next tab stop.
125 Assume a fixed set of tab stops, say every <em|n> columns. Should <em|n>
126 be a variable or a symbolic parameter?
127
128 <item*|Exercise 1-21>Write a program <cpp|entab> that replaces strings of
129 blanks by the minimum number of tabs and blanks to achieve the same
130 spacing. Use the same tab stops as for <cpp|detab>. When either a tab or
131 a single blank would suffice to each a tab stop, which should be given
132 preference?
133
134 <item*|Exercise 1-22>Write a program to \Pfold\Q long input lines into
135 two or more shorter lines after the last non-blank character that occurs
136 before the <em|n>-th column of input. Make sure your program does
137 something intelligent with very long lines, and if there are no blanks or
138 tabs before the specified column.
139
140 <item*|Exercise 1-23>Write a program to remove all comments from a
141 <name|C> program. Don't forget to handle quoted strings and character
142 constants properly. <name|C> comments do not nest.
143
144 <item*|Exercise 1-24>Write a program to check a <name|C> program for
145 rudimentary syntax errors like unbalanced parentheses, brackets and
146 braces. Don't forget about quotes, both single and double, escape
147 sequences, and comments. (This program is hard if you do it in full
148 generality.)
149 </description>
150
151 <section|Ch2: Types, Operators, and Expressions>
152
153 <subsection|Sec 2.1: Variable names>
154
155 <subsection|Sec 2.2: Data Types and Sizes>
156
157 <\description>
158 <item*|Exercise 2-1>Write a program to determine the ranges of
159 <cpp|char>, <cpp|short>, <cpp|int>, and <cpp|long> variables, both
160 <cpp|signed> and <cpp|unsigned>, by printing appropriate values from
161 standard headers and by direct computation. Harder if you compute them:
162 determine the ranges of the various floating-point types.
163 </description>
164
165 <subsection|Sec 2.3: Constants>
166
167 <subsection|Sec 2.4: Declarations>
168
169 <subsection|Sec 2.5: Arithmetic Operators>
170
171 <subsection|Sec 2.6: Relational and Logical Operators>
172
173 <\description>
174 <item*|Exercise 2-2>Write a loop equivalent to the <cpp|for> loop above
175 without using <cpp|&&> or <cpp|\|\|>.
176 </description>
177
178 <subsection|Sec 2.7: Type Conversions>
179
180 <\description>
181 <item*|Exercise 2-3>Write the function <cpp|htoi(s)>, which converts a
182 string of hexadecimal digits (including an optional <cpp|0x> or <cpp|0X>)
183 into its equivalent integer value. The allowable digits are <cpp|0>
184 through <cpp|9>, <cpp|a> through <cpp|f>, and <cpp|A> through <cpp|F>.
185 </description>
186
187 <subsection|Sec 2.8: Increment and Decrement Operators>
188
189 <\description>
190 <item*|Exercise 2-4>Write an alternate version of <cpp|squeeze(s1,s2)>
191 that deletes each character in <cpp|s1> that matches any character in the
192 <em|string> <cpp|s2>.
193
194 <item*|Exercise 2-5>Write the function <cpp|any(s1,s2)>, which returns
195 the first location in the string <cpp|s1> where any character fromt he
196 string <cpp|s2> occurs, or <cpp|-1> if <cpp|s1> contains no characters
197 from <cpp|s2>. (The standard library function <cpp|strpbrk> does the same
198 job but returns a pointer to the location.)
199 </description>
200
201 <subsection|Sec 2.9: Bitwise Operators>
202
203 <\description>
204 <item*|Exercise 2-6>Write a function <cpp|setbits(x,p,n,y)> that returns
205 <cpp|x> with the <cpp|n> bits that begin at position <cpp|p> set to the
206 rightmost <cpp|n> bits of <cpp|y>, leaving the other bits unchanged.
207
208 <item*|Exercise 2-7>Write a function <cpp|invert(x,p,n)> that returns
209 <cpp|x> with the <cpp|n> bits that begin at position <cpp|p> inverted
210 (i.e. <math|1> changed into <math|0> and vice versa), leaving the others
211 unchanged.
212
213 <item*|Exercise 2-8>Write a function <cpp|rightrot(x,n)> that returns the
214 value of the integer <cpp|x> rotated to the right by <cpp|n> bit
215 positions.
216 </description>
217
218 <subsection|Sec 2.10: Assignment Operators and Expressions>
219
220 <\description>
221 <item*|Exercise 2-9>In a two\<#2019\>s complement number system, <cpp|x
222 &= (x-1)> deletes the rightmost <math|1>-bit in <cpp|x>. Explain why. Use
223 this observation to write a faster version of <cpp|bitcount>.
224 </description>
225
226 <subsection|Sec 2.11: Conditional Expressions>
227
228 <\description>
229 <item*|Exercise 2-10>Rewrite the function <cpp|lower>, which converts
230 upper case letters to lower case, with a conditional expression instead
231 of <cpp|if-else>.
232 </description>
233
234 <subsection|Sec 2.12: Precedence and Order of Evaluation>
235
236 \;
237
238 \;
239 </body>
240
241 <\initial>
242 <\collection>
243 <associate|page-medium|papyrus>
244 </collection>
245 </initial>
246
247 <\references>
248 <\collection>
249 <associate|auto-1|<tuple|1|1|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
250 <associate|auto-10|<tuple|1.1.5.3|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
251 <associate|auto-11|<tuple|1.1.5.4|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
252 <associate|auto-12|<tuple|1.1.6|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
253 <associate|auto-13|<tuple|1.1.7|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
254 <associate|auto-14|<tuple|1.1.8|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
255 <associate|auto-15|<tuple|1.1.9|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
256 <associate|auto-16|<tuple|1.1.10|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
257 <associate|auto-17|<tuple|1.2|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
258 <associate|auto-18|<tuple|1.2.1|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
259 <associate|auto-19|<tuple|1.2.2|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
260 <associate|auto-2|<tuple|1.1|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
261 <associate|auto-20|<tuple|1.2.3|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
262 <associate|auto-21|<tuple|1.2.4|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
263 <associate|auto-22|<tuple|1.2.5|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
264 <associate|auto-23|<tuple|1.2.6|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
265 <associate|auto-24|<tuple|1.2.7|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
266 <associate|auto-25|<tuple|1.2.8|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
267 <associate|auto-26|<tuple|1.2.9|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
268 <associate|auto-27|<tuple|1.2.10|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
269 <associate|auto-28|<tuple|1.2.11|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
270 <associate|auto-29|<tuple|1.2.12|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
271 <associate|auto-3|<tuple|1.1.1|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
272 <associate|auto-30|<tuple|1.2.13|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
273 <associate|auto-4|<tuple|1.1.2|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
274 <associate|auto-5|<tuple|1.1.3|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
275 <associate|auto-6|<tuple|1.1.4|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
276 <associate|auto-7|<tuple|1.1.5|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
277 <associate|auto-8|<tuple|1.1.5.1|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
278 <associate|auto-9|<tuple|1.1.5.2|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
279 </collection>
280 </references>
281
282 <\auxiliary>
283 <\collection>
284 <\associate|toc>
285 <vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|1<space|2spc>K&R
286 Exercises> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
287 <no-break><pageref|auto-1><vspace|0.5fn>
288 </associate>
289 </collection>
290 </auxiliary>