* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / homework / PS2a.html
blob64044cee0ea8a99a92f4786483f97dc0c54518f1
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 #2 Answers</title>
10 </head>
11 <body>
12 <H1>Assignment #2 Answers</H1>
18 <B>Introductory C Programming
19 <br>
20 <br>
21 UW Experimental College
22 <br>
23 <br>
24 Assignment #2 ANSWERS
25 </B><br>
26 <br>
27 <p>Question 1.
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
37 if either operand
38 (or both)
39 is floating-point
40 (that is, if either number being operated on is floating-point);
41 integer division is performed if both operands are integers.
42 <p>Question 2.
43 <I>What are the definitions of the ``Boolean'' values
44 true and false in C?
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>&lt;</TT>, <TT>&lt;=</TT>, <TT>&gt;</TT>, <TT>&gt;=</TT>, <TT>==</TT>, <TT>!=</TT>,
49 <TT>&amp;&amp;</TT>, <TT>||</TT>, and <TT>!</TT>
50 always generate 0 for false and 1 for true.
51 <p>Question 3.
52 <I>Name three uses for the semicolon in C.
53 </I><p>Terminating declarations,
54 terminating statements,
55 and
56 separating the three control expressions in a <TT>for</TT> loop.
57 <p>Question 4.
58 <I>What would the equivalent code, using a while loop, be for the example
59 </I><pre>
60 for(i = 0; i &lt; 10; i = i + 1)
61 printf("i is %d\n", i);
62 </pre>
63 <I></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:
66 <pre>
67 i = 0;
68 while(i &lt; 10)
70 printf("i is %d\n", i);
71 i = i + 1;
73 </pre>
74 <p>Question 5.
75 <I>What is the numeric value of the expression <TT>3 &lt; 4</TT> ?
76 </I><p>1 (or ``true''),
77 because 3 is in fact less than 4.
78 <p>Question 6.
79 <I>Under what conditions will this code print ``water''?
80 </I><pre>
81 if(T &lt; 32)
82 printf("ice\n");
83 else if(T &lt; 212)
84 printf("water\n");
85 else printf("steam\n");
86 </pre>
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.)
94 <p>Question 7.
95 <I>What would this code print?
96 </I><pre>
97 int x = 3;
98 if(x)
99 printf("yes\n");
100 else printf("no\n");
101 </pre>
102 <p>It would print ``yes'', since <TT>x</TT> is nonzero.
103 <p>Question 8.
104 <I>What would this code print?
105 </I><pre>
106 int i;
108 for(i = 0; i &lt; 3; i = i + 1)
109 printf("a\n");
110 printf("b\n");
111 printf("c\n");
112 </pre>
113 <p>It would print
114 <pre>
120 </pre>
121 The indentation of the statement <TT>printf("b\n");</TT> is
122 (deliberately, for the sake of the question)
123 misleading.
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>.
129 The two statements
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
133 <TT>printf("b\n");
134 </TT>line were indented to line up with the
135 <TT>printf("c\n");
136 </TT>line.
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
140 <TT>printf("a\n");
141 </TT>and
142 <TT>printf("b\n");
143 </TT>.
144 <p>Exercise 1.
145 <I>Write a program to find out
146 how many of the numbers from 1 to 10 are greater than 3.
147 </I><p><pre>
148 #include &lt;stdio.h&gt;
150 int main()
152 int i;
153 int count = 0;
155 for(i = 1; i &lt;= 10; i = i + 1)
157 if(i &gt; 3)
158 count = count + 1;
161 printf("%d numbers were greater than 3\n", count);
163 return 0;
165 </pre>
166 <p>Tutorial 4b.
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
170 <pre>
171 printf("On the %d day of Christmas, ", day);
172 printf("my true love gave to me\n");
173 </pre>
174 with
175 <pre>
176 printf("On the ");
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");
186 </pre>
187 The final
188 <pre>
189 else printf("%d", day);
190 </pre>
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.
198 <p>Exercise 2.
199 <I>Write a program to compute the average of the ten numbers
200 1, 4, 9, ..., 81, 100.
201 </I><p><pre>
202 #include &lt;stdio.h&gt;
204 int main()
206 int i;
207 float sum = 0;
208 int n = 0;
210 for(i = 1; i &lt;= 10; i = i + 1)
212 sum = sum + i * i;
213 n = n + 1;
216 printf("the average is %f\n", sum / n);
218 return 0;
220 </pre>
221 <p>Exercise 3.
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.
224 </I><p><pre>
225 #include &lt;stdio.h&gt;
227 int main()
229 int i;
231 for(i = 1; i &lt;= 10; i = i + 1)
233 if(i % 2 == 0)
234 printf("%d is even\n", i);
235 else printf("%d is odd\n", i);
238 return 0;
240 </pre>
241 <p>Exercise 4.
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.
244 </I><p><pre>
245 if(x &gt; 0)
247 if(y &gt; 0)
248 printf("Northeast.\n");
249 else if(y &lt; 0)
250 printf("Southeast.\n");
251 else printf("East.\n");
253 else if(x &lt; 0)
255 if(y &gt; 0)
256 printf("Northwest.\n");
257 else if(y &lt; 0)
258 printf("Southwest.\n");
259 else printf("West.\n");
261 else {
262 if(y &gt; 0)
263 printf("North.\n");
264 else if(y &lt; 0)
265 printf("South.\n");
266 else printf("Nowhere.\n");
268 </pre>
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>.)
272 <p>Exercise 5.
273 <I>Write a program to print the first 7 positive integers
274 and their factorials.
275 </I><p><pre>
276 #include &lt;stdio.h&gt;
278 int main()
280 int i;
281 int factorial = 1;
283 for(i = 1; i &lt;= 7; i = i + 1)
285 factorial = factorial * i;
286 printf("%d %d\n", i, factorial);
289 return 0;
291 </pre>
292 <p>The answer to the extra credit question is that 8!,
293 which is how mathematicians write ``eight factorial,''
294 is 40320,
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>.
298 <p>Exercise 6.
299 <I>Write a program to print the first 10 Fibonacci numbers.
300 </I><p><pre>
301 #include &lt;stdio.h&gt;
303 int main()
305 int i;
306 int fibonacci = 1;
307 int prevfib = 0;
308 int tmp;
310 for(i = 1; i &lt;= 10; i = i + 1)
312 printf("%d %d\n", i, fibonacci);
313 tmp = fibonacci;
314 fibonacci = fibonacci + prevfib;
315 prevfib = tmp;
318 return 0;
320 </pre>
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,
326 so much the
327 better!)
328 <p>Exercise 7.
329 <I>Make some improvements to the prime number printing program.
330 </I><p>Here is a version incorporating both of the suggested improvements:
331 <pre>
332 #include &lt;stdio.h&gt;
333 #include &lt;math.h&gt;
335 int main()
337 int i, j;
338 double sqrti;
340 printf("%d\n", 2);
342 for(i = 3; i &lt;= 100; i = i + 2)
344 sqrti = sqrt(i);
345 for(j = 2; j &lt; i; j = j + 1)
347 if(i % j == 0)
348 break; /* not prime */
349 if(j &gt; sqrti)
350 { /* prime */
351 printf("%d\n", i);
352 break;
357 return 0;
359 </pre>
360 <hr>
361 <hr>
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>
366 </p>
367 </body>
368 </html>