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. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>Assignment #
3</title>
12 <H1>Assignment #
3</H1>
18 <B>Introductory C Programming
21 UW Experimental College
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>
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
46 Which is the first element?
48 <li>What's wrong with this scrap of code?
51 for(i =
1; i
<=
5; i = i +
1)
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?
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.
69 /* print an addition table for
1+
1 up to
10+
10 */
71 #include
<stdio.h
>
76 /* print header line: */
78 for(j =
1; j
<=
10; j = j +
1)
82 for(i =
1; i
<=
10; i = i +
1)
85 for(j =
1; j
<=
10; j = j +
1)
86 printf(
" %3d", i + j);
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>).
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,
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.
123 #include
<stdio.h
>
128 for(i =
1; i
<=
10; i = i +
1)
130 for(j =
1; j
<=
10; j = j +
1)
141 (Why are there parentheses around
146 What if they were left out?)
149 Modify the program to print a pattern of
<TT>.
</TT> and
<TT>#
</TT> characters,
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
159 you're asked to create a simple function to compute the squares of numbers.
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:
168 #include
<stdio.h
>
173 int squares[
11]; /* [
0.
.10]; [
0] ignored */
175 for(i =
1; i
<=
10; i = i +
1)
178 printf(
"n\tsquare\n");
179 for(i =
1; i
<=
10; i = i +
1)
180 printf(
"%d\t%d\n", i, squares[i]);
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
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.
197 The program also uses the tab character,
<TT>\t
</TT>,
199 to make the columns line up.
202 Modify the program so that it also declares and fills in
203 a
<TT>cubes
</TT> array containing the cubes
206 and prints them out in a third column.
207 <li>We're going to write a simple
208 (
<em>very
</em> simple!)
210 We'll write a function
<TT>printsquare
</TT>
211 which prints a square
212 (made out of asterisks)
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.
218 #include
<stdio.h
>
220 int printsquare(int);
225 for(i =
1; i
<=
10; i = i +
1)
233 int printsquare(int n)
236 for(i =
0; i
< n; i = i +
1)
238 for(j =
0; j
< n; j = j +
1)
245 Type the program in and run it.
250 to print ``open'' squares,
258 instead of filled squares.
259 You'll have to print the box in three parts:
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.
267 change the loop in the top-level
<TT>main
</TT> function to
268 <TT>for(i =
3; i
<=
10; i = i +
1)
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
278 int a[] = {
1,
2,
3,
4,
5,
6};
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.
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
298 <li>Write the function
300 void printnchars(int ch, int n)
302 which is supposed to print the character
<TT>ch
</TT>,
<TT>n
</TT> times.
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
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.
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
°C =
5/
9 <TT>*
</TT> (
°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
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
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>.
333 For extra credit, also compute the standard deviation, which can
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!)
341 #include
<math.h
>
343 at the top of the file, you'll be able to call
<TT>sqrt()
</TT>.
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
351 <li>Write either the function
355 which returns random integers from
1 to
<TT>n
</TT>, or the function
357 int randrange2(int m, int n)
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.
363 The header file
<TT><stdlib.h
></TT> defines a constant,
<TT>RAND_MAX
</TT>, which
364 is the maximum number returned by the
<TT>rand()
</TT> function.
366 way of reducing the range of the
<TT>rand()
</TT> function is like this:
368 rand() / (RAND_MAX / N +
1)
370 (where
<TT>N
</TT> is the range of numbers you want).
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:
388 6:
15 ***************
389 7:
28 ****************************
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>