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 #
2 Answers
</title>
12 <H1>Assignment #
2 Answers
</H1>
18 <B>Introductory C Programming
21 UW Experimental College
28 <I>What are the two different kinds of division
29 that the
<TT>/
</TT> operator can do?
30 Under what circumstances does it perform each?
31 </I><p>The two kinds of division are integer division,
32 which discards any remainder,
33 and floating-point division,
34 which yields a floating-point result
35 (with a fractional part, if necessary).
36 Floating-point division is performed
40 (that is, if either number being operated on is floating-point);
41 integer division is performed if both operands are integers.
43 <I>What are the definitions of the ``Boolean'' values
45 </I><p>False is always represented by a zero value.
46 Any nonzero value is considered true.
47 The built-in operators
48 <TT><</TT>,
<TT><=
</TT>,
<TT>></TT>,
<TT>>=
</TT>,
<TT>==
</TT>,
<TT>!=
</TT>,
49 <TT>&&</TT>,
<TT>||
</TT>, and
<TT>!
</TT>
50 always generate
0 for false and
1 for true.
52 <I>Name three uses for the semicolon in C.
53 </I><p>Terminating declarations,
54 terminating statements,
56 separating the three control expressions in a
<TT>for
</TT> loop.
58 <I>What would the equivalent code, using a while loop, be for the example
60 for(i =
0; i
< 10; i = i +
1)
61 printf(
"i is %d\n", i);
64 <p>Just move the first (initialization) expression to before the loop,
65 and the third (increment) expression to the body of the loop:
70 printf(
"i is %d\n", i);
75 <I>What is the numeric value of the expression
<TT>3 < 4</TT> ?
76 </I><p>1 (or ``true''),
77 because
3 is in fact less than
4.
79 <I>Under what conditions will this code print ``water''?
85 else printf(
"steam\n");
87 <p>If
<TT>T
</TT> is greater than or equal to
32 <em>and
</em> less than
212.
88 (If you said ``greater than
32 and less than
212'',
89 you weren't quite right,
90 and this kind of distinction--paying
91 attention to the difference between
92 ``greater than'' and ``greater than or equal''--is
93 often extremely important in programming.)
95 <I>What would this code print?
102 <p>It would print ``yes'', since
<TT>x
</TT> is nonzero.
104 <I>What would this code print?
108 for(i =
0; i
< 3; i = i +
1)
121 The indentation of the statement
<TT>printf(
"b\n");
</TT> is
122 (deliberately, for the sake of the question)
124 It
<em>looks
</em> like it's part of the body of the
<TT>for
</TT> statement,
125 but the body of the
<TT>for
</TT> statement is always a single statement,
126 or a list of statements enclosed in braces {}.
127 In this case, the body of the loop is the call
128 <TT>printf(
"a\n");
</TT>.
130 <TT>printf(
"b\n");
</TT> and
<TT>printf(
"c\n");
</TT>
131 are normal statements following the loop.
132 <p>The code would be
<em>much
</em> clearer if the
134 </TT>line were indented to line up with the
137 If the intent was that ``b'' be printed
3 times, along
138 with ``a'', it would be necessary to put braces
139 <TT>{}
</TT> around the pair of lines
145 <I>Write a program to find out
146 how many of the numbers from
1 to
10 are greater than
3.
148 #include
<stdio.h
>
155 for(i =
1; i
<=
10; i = i +
1)
161 printf(
"%d numbers were greater than 3\n", count);
167 <I>Print ``On the first day'' instead of
168 ``On the
1 day'' in the days-of-Christmas program.
169 </I><p>Simply replace the lines
171 printf(
"On the %d day of Christmas, ", day);
172 printf(
"my true love gave to me\n");
177 if(day ==
1) printf(
"first");
178 else if(day ==
2) printf(
"second");
179 else if(day ==
3) printf(
"third");
180 else if(day ==
4) printf(
"fourth");
181 else if(day ==
5) printf(
"fifth");
182 else if(day ==
6) printf(
"sixth");
183 else printf(
"%d", day);
184 printf(
" day of Christmas, ");
185 printf(
"my true love gave to me\n");
189 else printf(
"%d", day);
191 is an example of ``defensive programming.''
192 If the variable
<TT>day
</TT> ever ends up having a value
193 outside the range
1-
6
194 (perhaps because we later added more verses to the song,
195 but forgot to update the list of words),
196 the program will at least print something.
199 <I>Write a program to compute the average of the ten numbers
200 1,
4,
9, ...,
81,
100.
202 #include
<stdio.h
>
210 for(i =
1; i
<=
10; i = i +
1)
216 printf(
"the average is %f\n", sum / n);
222 <I>Write a program to print the numbers between
1 and
10,
223 along with an indication of whether each is even or odd.
225 #include
<stdio.h
>
231 for(i =
1; i
<=
10; i = i +
1)
234 printf(
"%d is even\n", i);
235 else printf(
"%d is odd\n", i);
242 <I>Print out the
8 points of the compass,
243 based on the
<TT>x
</TT> and
<TT>y
</TT> components of the direction of travel.
248 printf(
"Northeast.\n");
250 printf(
"Southeast.\n");
251 else printf(
"East.\n");
256 printf(
"Northwest.\n");
258 printf(
"Southwest.\n");
259 else printf(
"West.\n");
266 else printf(
"Nowhere.\n");
269 (You will notice that this code also prints
270 ``Nowhere'' in the ninth case,
271 namely when
<TT>x ==
0</TT> and
<TT>y ==
0</TT>.)
273 <I>Write a program to print the first
7 positive integers
274 and their factorials.
276 #include
<stdio.h
>
283 for(i =
1; i
<=
7; i = i +
1)
285 factorial = factorial * i;
286 printf(
"%d %d\n", i, factorial);
292 <p>The answer to the extra credit question is that
8!,
293 which is how mathematicians write ``eight factorial,''
295 which is
<em>not
</em> guaranteed to fit into a variable of type
<TT>int
</TT>.
296 To write a portable program to compute factorials higher than
7,
297 we would have to use
<TT>long int
</TT>.
299 <I>Write a program to print the first
10 Fibonacci numbers.
301 #include
<stdio.h
>
310 for(i =
1; i
<=
10; i = i +
1)
312 printf(
"%d %d\n", i, fibonacci);
314 fibonacci = fibonacci + prevfib;
321 (There are many ways of writing this program.
322 As long as the code you wrote printed
323 1,
1,
2,
3,
5,
8,
13,
21,
34,
55,
324 it's probably correct.
325 If the code you wrote is clearer than my solution above,
329 <I>Make some improvements to the prime number printing program.
330 </I><p>Here is a version incorporating both of the suggested improvements:
332 #include
<stdio.h
>
333 #include
<math.h
>
342 for(i =
3; i
<=
100; i = i +
2)
345 for(j =
2; j
< i; j = j +
1)
348 break; /* not prime */
363 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
364 //
<a href=
"copyright.html">Copyright
</a> 1995-
9
365 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>