* X more docs for C
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / homework / PS3.html
blobc4749a08f517580c40de2d3b1bb92527c510d2e1
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>Assignment #3</title>
10 </head>
11 <body>
12 <H1>Assignment #3</H1>
18 <B>Introductory C Programming
19 <br>
20 <br>
21 UW Experimental College
22 </B><br>
23 <br>
24 <B>Assignment #3
25 </B><p><B>Handouts:
26 </B></p><p><a href="PS3.html">Assignment #3</a>
27 <br><a href="PS2a.html">Answers to Assignment #2</a>
28 <br><a href="http://www.eskimo.com/~scs/cclass/notes/sx4.html">Class Notes, Chapter 4</a>
29 <br><a href="http://www.eskimo.com/~scs/cclass/notes/sx5.html">Class Notes, Chapter 5</a>
30 <p><B>Reading Assignment:
31 </B></p><p><a href="http://www.eskimo.com/~scs/cclass/notes/sx3c.html">Class Notes, Chapter 3, Secs. 3.3-3.5</a>
32 <br><a href="http://www.eskimo.com/~scs/cclass/notes/sx4.html">Class Notes, Chapter 4, Secs. 4.1-4.1.1, 4.3</a>
33 <br><a href="http://www.eskimo.com/~scs/cclass/notes/sx5.html">Class Notes, Chapter 5, Secs. 5.1-5.3</a>
34 <br>(optional)
35 Class Notes,
36 Secs. <a href="http://www.eskimo.com/~scs/cclass/notes/sx3f.html">3.6</a>,
37 <a href="http://www.eskimo.com/~scs/cclass/notes/sx4ba.html">4.1.2-4.2</a>,
38 <a href="http://www.eskimo.com/~scs/cclass/notes/sx4d.html">4.4</a>,
39 <a href="http://www.eskimo.com/~scs/cclass/notes/sx5d.html">5.4</a>
40 <p><B>Review Questions:
41 </B></p><OL><li>How many elements does the array
42 <pre>
43 int a[5];
44 </pre>
45 contain?
46 Which is the first element?
47 The last?
48 <li>What's wrong with this scrap of code?
49 <pre>
50 int a[5];
51 for(i = 1; i &lt;= 5; i = i + 1)
52 a[i] = 0;
53 </pre>
54 <li>How might you rewrite the dice-rolling program (from the notes,
55 chapter 4, p. 2) without arrays?
56 <li>What is the difference between a <dfn>defining instance</dfn>
57 and an <dfn>external declaration</dfn>?
58 <li>What are the four important parts of a function?
59 Which three does a caller need to know?
60 </OL><br>
61 <br>
62 <p><B>Tutorial Section
63 </B><OL><li>Here is another nested-loop example,
64 similar to exercise 4 of assignment 1,
66 and to tutorial 3 of assignment 2.
67 This one prints an addition table for sums from 1+1 to 10+10.
68 <pre>
69 /* print an addition table for 1+1 up to 10+10 */
71 #include &lt;stdio.h&gt;
73 int main()
75 int i, j;
76 /* print header line: */
77 printf(" ");
78 for(j = 1; j &lt;= 10; j = j + 1)
79 printf(" %3d", j);
80 printf("\n");
81 /* print table: */
82 for(i = 1; i &lt;= 10; i = i + 1)
84 printf("%2d", i);
85 for(j = 1; j &lt;= 10; j = j + 1)
86 printf(" %3d", i + j);
87 printf("\n");
89 return 0;
91 </pre>
92 The first <TT>j</TT> loop prints the top, header row of the table.
93 (The initial <TT>printf(" ");</TT>
94 is to make it line up with the rows beneath,
95 which will all begin with a value of <TT>i</TT>.)
96 Then, the <TT>i</TT> loop prints the rest of the table,
97 one row per value of <TT>i</TT>.
98 For each value of <TT>i</TT>, we print that value
99 (on the left edge of the table),
100 and then print the sums
101 resulting from adding that value of <TT>i</TT>
102 to ten different values of <TT>j</TT>
103 (using a second, inner loop on <TT>j</TT>).
104 <br>
105 <br>
106 Make a simple modification to the program to print a multiplication table,
107 or a subtraction table.
108 <li>Here is yet another nested-loop example.
109 It is very similar to the one above,
110 except that rather than printing the sum <TT>i+j</TT>,
111 it determines whether the sum is even or odd,
112 using the expression
113 <TT>(i+j) % 2
114 </TT>.
115 The <TT>%</TT> operator, remember, gives the remainder when dividing,
118 even number gives a remainder of 0 when dividing by 2.
119 Depending on whether the sum is even or odd,
120 the program prints an asterisk or a space.
121 Type in and run the program, and look at the pattern that results.
122 <pre>
123 #include &lt;stdio.h&gt;
125 int main()
127 int i, j;
128 for(i = 1; i &lt;= 10; i = i + 1)
130 for(j = 1; j &lt;= 10; j = j + 1)
132 if((i + j) % 2 == 0)
133 printf("* ");
134 else printf(" ");
136 printf("\n");
138 return 0;
140 </pre>
141 (Why are there parentheses around
142 <TT>i + j</TT>
143 in the expression
144 <TT>(i + j) % 2
145 </TT>?
146 What if they were left out?)
147 <br>
148 <br>
149 Modify the program to print a pattern of <TT>.</TT> and <TT>#</TT> characters,
150 or X's and O's.
151 If you wish, experiment by taking the remainder
152 when dividing by 3 or 4, instead.
153 Since the remainder when dividing by 3 can be 0, 1, or 2,
154 you could use a cascaded <TT>if</TT>/<TT>else</TT> statement
155 to print one of three characters for each sum
156 (or one of four if taking the remainder when dividing by 4).
157 <li>Later in this assignment
158 (exercise 3),
159 you're asked to create a simple function to compute the squares of numbers.
160 Sometimes,
161 rather than writing a function
162 which will compute a value each time it's called,
163 it's useful to build an array
164 containing all the values we might need.
165 Here is a program which declares an array,
166 then fills it with the squares of the numbers from 1 to 10:
167 <pre>
168 #include &lt;stdio.h&gt;
170 int main()
172 int i;
173 int squares[11]; /* [0..10]; [0] ignored */
174 /* fill array: */
175 for(i = 1; i &lt;= 10; i = i + 1)
176 squares[i] = i * i;
177 /* print table: */
178 printf("n\tsquare\n");
179 for(i = 1; i &lt;= 10; i = i + 1)
180 printf("%d\t%d\n", i, squares[i]);
181 return 0;
183 </pre>
184 There's one slight trick in the declaration of the <TT>squares</TT> array.
185 Remember that arrays in C are based at 0.
186 So if we wanted an array to hold the squares of 10 numbers,
187 and if we declared it as
188 <TT>int squares[10];
189 </TT>the array's 10 elements would range from
190 <TT>squares[0]</TT> to <TT>squares[9]</TT>.
191 This program wants to use elements
192 from <TT>squares[1]</TT> to <TT>squares[10]</TT>,
193 so it simply declares the array as having size 11,
194 and wastes the 0th element.
195 <br>
196 <br>
197 The program also uses the tab character, <TT>\t</TT>,
198 in its printouts,
199 to make the columns line up.
200 <br>
201 <br>
202 Modify the program so that it also declares and fills in
203 a <TT>cubes</TT> array containing the cubes
204 (third powers)
205 of the numbers 1-10,
206 and prints them out in a third column.
207 <li>We're going to write a simple
208 (<em>very</em> simple!)
209 graphics program.
210 We'll write a function <TT>printsquare</TT>
211 which prints a square
212 (made out of asterisks)
213 of a certain size.
214 Then we'll use our favorite for-<TT>i</TT>-equals-1-to-10 loop
215 to call the function 10 times,
216 printing squares of size 1 to 10.
217 <pre>
218 #include &lt;stdio.h&gt;
220 int printsquare(int);
222 int main()
224 int i;
225 for(i = 1; i &lt;= 10; i = i + 1)
227 printsquare(i);
228 printf("\n");
230 return 0;
233 int printsquare(int n)
235 int i, j;
236 for(i = 0; i &lt; n; i = i + 1)
238 for(j = 0; j &lt; n; j = j + 1)
239 printf("*");
240 printf("\n");
242 return 0;
244 </pre>
245 Type the program in and run it.
246 Then
248 if you can modify
250 to print ``open'' squares,
251 like this:
252 <pre>
253 ****
256 ****
257 </pre>
258 instead of filled squares.
259 You'll have to print the box in three parts:
260 first the top row,
261 then a number of ``<TT>* *</TT>'' rows,
262 and finally the bottom row.
263 There's obviously no way to print ``open'' boxes of size 1 or 2,
264 so don't worry about those cases.
265 (That is,
266 you can
267 change the loop in the top-level <TT>main</TT> function to
268 <TT>for(i = 3; i &lt;= 10; i = i + 1)
269 </TT>.)
270 </OL><br>
271 <br>
272 </p><p><B>Exercises
273 </B>(pick three or four):
274 </p><OL><li>Write code to sum the elements of an array of <TT>int</TT>.
275 (Write it as a function, if you like.)
276 Use it to sum the array
277 <pre>
278 int a[] = {1, 2, 3, 4, 5, 6};
279 </pre>
280 (The answer, of course, should be 21).
281 <li>Write a loop to call the <TT>multbytwo()</TT> function (chapter
282 5, section 5.1, p. 1) on the numbers 1-10.
283 For extra credit,
284 compile your <TT>main</TT> function and
285 the <TT>multbytwo()</TT> function
286 as two source files, one function per file.
287 <li>Write a <TT>square()</TT> function and use it to print the squares
288 of the numbers 1-10:
289 <pre>
293 4 16
295 9 81
296 10 100
297 </pre>
298 <li>Write the function
299 <pre>
300 void printnchars(int ch, int n)
301 </pre>
302 which is supposed to print the character <TT>ch</TT>, <TT>n</TT> times.
303 (Remember
304 that <TT>%c</TT> is the <TT>printf</TT> format to use for printing characters.)
305 For example, the call <TT>printnchars('x', 5)</TT> would print 5 <TT>x</TT>'s. Use
306 this function to rewrite the triangle-printing program of assignment
307 1 (exercise 4).
308 <li>Write a function to compute the factorial of a number, and use
309 it to print the factorials of the numbers 1-7.
310 (Extra credit:
311 print the factorials of the numbers 1-10.)
312 <li>(Kernighan and Ritchie)
313 Write a function <TT>celsius()</TT> to convert
314 degrees Fahrenheit to degrees Celsius.
315 (The conversion formula
316 is &#176;C = 5/9 <TT>*</TT> (&#176;F - 32).)
317 Use it to print a Fahrenheit-to-Centigrade table
318 for -40 to 220 degrees Fahrenheit, in increments of 10 degrees.
319 (Remember that <TT>%f</TT> is the <TT>printf</TT> format to use for printing floating-point
320 numbers.
321 Also, remember
322 that the integer expression <TT>5/9</TT> gives 0,
323 so you <em>won't</em> want to use integer division.)
324 <li>Modify the dice-rolling program
325 (Sec. 4.1)
326 so that it computes the average
327 of all the rolls of the pair of dice.
328 Remember that integer division
329 truncates, so you'll have to declare some of your variables as
330 <TT>float</TT> or <TT>double</TT>.
331 <br>
332 <br>
333 For extra credit, also compute the standard deviation, which can
334 be expressed as
335 <blockquote>sqrt((sum(x*x) - sum(x)*sum(x)/n) / (n - 1))
336 </blockquote>(where the notation sum(x) indicates summing all values of x,
337 as usually expressed with the Greek letter sigma;
338 there is <em>not</em> such a sum() function in C!)
339 If you put the line
340 <pre>
341 #include &lt;math.h&gt;
342 </pre>
343 at the top of the file, you'll be able to call <TT>sqrt()</TT>.
344 <br>
345 <br>
346 (See the note in Assignment #2, Exercise 5, about compiling with
347 the math library under Unix.
348 There are better ways of computing
349 the standard deviation, but the above expression will suffice
350 for our purposes.)
351 <li>Write either the function
352 <pre>
353 randrange(int n)
354 </pre>
355 which returns random integers from 1 to <TT>n</TT>, or the function
356 <pre>
357 int randrange2(int m, int n)
358 </pre>
359 which returns random integers in the range <TT>m</TT> to <TT>n</TT>. Use your function
360 to streamline the dice-rolling program a bit.
361 <br>
362 <br>
363 The header file <TT>&lt;stdlib.h&gt;</TT> defines a constant, <TT>RAND_MAX</TT>, which
364 is the maximum number returned by the <TT>rand()</TT> function.
365 A better
366 way of reducing the range of the <TT>rand()</TT> function is like this:
367 <pre>
368 rand() / (RAND_MAX / N + 1)
369 </pre>
370 (where <TT>N</TT> is the range of numbers you want).
371 <br>
372 <br>
373 Extra credit: investigate the behavior of <TT>randrange(2)</TT>, both using
374 the ``obvious'' range-reduction technique (the modulus operator <TT>%</TT>,
375 as shown in the notes) and this improved method. If your library's
376 implementation of <TT>rand()</TT> is a good one, you won't see a difference.
377 But if you're not so lucky, you may see something very surprising.
378 <li>Rewrite the dice-rolling program to also print a histogram.
379 For example, if there are 21 rolls of 7, the output line ``<TT>7: 21</TT>''
380 should also contain a horizontal row of 21 asterisks.
381 (Use the <TT>printnchars</TT> function from exercise 4, if you wish.)
382 The output might look like this:
383 <pre>
384 2: 2 **
385 3: 5 *****
386 4: 4 ****
387 5: 10 **********
388 6: 15 ***************
389 7: 28 ****************************
390 8: 12 ************
391 9: 9 *********
392 10: 7 *******
393 11: 5 *****
394 12: 3 ***
395 </pre>
396 </OL><hr>
397 <hr>
399 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
400 // <a href="copyright.html">Copyright</a> 1995-9
401 // <a href="mailto:scs@eskimo.com">mail feedback</a>
402 </p>
403 </body>
404 </html>