* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / homework / PS2.html
blob9b4c82a78fe4e32ba55eca55579f3ae07d81401a
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</title>
10 </head>
11 <body>
12 <H1>Assignment #2</H1>
18 <B>Introductory C Programming
19 <br>
20 <br>
21 UW Experimental College
22 </B><br>
23 <br>
24 <B>Assignment #2
25 </B><p><B>Handouts:
26 </B></p><p><a href="PS2.html">Assignment #2</a>
27 <br><a href="PS1a.html">Assignment #1 Answers</a>
28 <br><a href="http://www.eskimo.com/~scs/cclass/notes/sx2.html">Class Notes, Chapter 3</a>
29 <p><B>Reading Assignment:
30 </B></p><p><a href="http://www.eskimo.com/~scs/cclass/notes/sx2.html">Class Notes, Chapter 2</a>
31 <br><a href="http://www.eskimo.com/~scs/cclass/notes/sx3.html">Class Notes, Chapter 3, Secs. 3.1-3.2</a>
32 <br><a href="http://www.eskimo.com/~scs/cclass/notes/sx3c.html">Class Notes, Chapter 3, Secs. 3.3-3.6</a> (optional)
33 <p><B>Review Questions:
34 </B></p><OL><li>What are the two different kinds of division
35 that the <TT>/</TT> operator can do?
36 Under what circumstances does it perform each?
37 <li>What are the definitions of the ``Boolean'' values
38 <I>true</I> and <I>false</I> in C?
39 <li>Name three uses for the semicolon in C.
40 <li>What would the equivalent code, using a <TT>while</TT> loop, be for the
41 example
42 <pre>
43 for(i = 0; i &lt; 10; i = i + 1)
44 printf("i is %d\n", i);
45 </pre>
47 <li>What is the
48 numeric
49 value of the expression <TT>3 &lt; 4</TT> ?
50 <li>Under what conditions will this code print ``water''?
51 <pre>
52 if(T &lt; 32)
53 printf("ice\n");
54 else if(T &lt; 212)
55 printf("water\n");
56 else printf("steam\n");
57 </pre>
58 <li>What would this code print?
59 <pre>
60 int x = 3;
61 if(x)
62 printf("yes\n");
63 else printf("no\n");
64 </pre>
65 <li>(trick question)
66 What would this code print?
67 <pre>
68 int i;
70 for(i = 0; i &lt; 3; i = i + 1)
71 printf("a\n");
72 printf("b\n");
74 printf("c\n");
75 </pre>
76 </OL><p><B>Tutorial Section</B>
77 </p><p>(This section presents a few small
78 but complete programs
79 and asks you to work with them.
80 If you're comfortable doing
81 at least some of the exercises
82 later in this assignment,
83 there's no reason for you to
84 work through this ``tutorial section.''
85 But if you're not ready to do the exercises yet,
86 this section should give you some practice and get you started.)
87 </p><OL><li>Type in and run this program,
88 and compare its output to
89 that of the original ``Hello, world!'' program
90 (exercise 1 in assignment 1).
91 <pre>
92 #include &lt;stdio.h&gt;
94 int main()
96 printf("Hello, ");
97 printf("world!\n");
98 return 0;
100 </pre>
101 You should notice that the output is identical to
102 that of
103 the original ``Hello, world!'' program.
104 This shows that you can build up output
105 using multiple calls to <TT>printf</TT>, if you like.
106 You mark the end of a line of output
107 (that is, you arrange that further output will begin on a new line)
108 by printing the ``newline'' character, <TT>\n</TT>.
109 <li>Type in and run this program:
110 <pre>
111 #include &lt;stdio.h&gt;
113 int main()
115 int i;
117 printf("statement 1\n");
118 printf("statement 2\n");
119 for(i = 0; i &lt; 10; i = i + 1)
121 printf("statement 3\n");
122 printf("statement 4\n");
124 printf("statement 5\n");
126 return 0;
128 </pre>
129 This program doesn't do anything useful;
130 it's just supposed to show you how <dfn>control flow</dfn>
131 works--how statements are executed one after the other,
132 except when a construction such as the <TT>for</TT> loop
133 alters the flow
134 by arranging that certain statements get executed over and over.
135 In this program,
136 each simple statement is just a call to the <TT>printf</TT> function.
137 <br>
138 <br>
139 Now delete the braces <TT>{}</TT> around statements 3 and 4,
140 and re-run the program.
141 How does the output change?
142 (See also question 8 above.)
143 <li>Type in and run this program:
144 <pre>
145 #include &lt;stdio.h&gt;
147 int main()
149 int i, j;
151 printf("start of program\n");
153 for(i = 0; i &lt; 3; i = i + 1)
155 printf("i is %d\n", i);
156 for(j = 0; j &lt; 5; j = j + 1)
157 printf("i is %d, j is %d\n", i, j);
158 printf("end of i = %d loop\n", i);
161 printf("end of program\n");
163 return 0;
165 </pre>
166 This program doesn't do much useful, either;
167 it's just supposed to show you how loops work,
168 and how loops can be nested.
169 The outer loop runs <TT>i</TT> through the values 0, 1, and 2,
170 and for each of these three values of <TT>i</TT>
171 (that is, during each of the three trips through the outer loop)
172 the inner loop runs, stepping the variable <TT>j</TT> through 5 values, 0 to 4.
173 Experiment with changing the limits (initially 3 and 5) on the two loops.
174 Experiment with interchanging the two loops, that is,
175 by having the outer loop manipulate the variable <TT>j</TT>
176 and the inner loop manipulate the variable <TT>i</TT>.
177 Finally, compare this program to the triangle-printing program
178 of Assignment 1 (exercise 4)
179 and its answer as handed out this week.
180 <li>Type in and run this program:
181 <pre>
182 #include &lt;stdio.h&gt;
184 int main()
186 int day, i;
188 for(day = 1; day &lt;= 3; day = day + 1)
190 printf("On the %d day of Christmas, ", day);
191 printf("my true love gave to me\n");
192 for(i = day; i &gt; 0; i = i - 1)
194 if(i == 1)
196 if(day == 1) printf("A ");
197 else printf("And a ");
198 printf("partridge in a pear tree.\n");
200 else if(i == 2)
201 printf("Two turtledoves,\n");
202 else if(i == 3)
203 printf("Three French hens,\n");
205 printf("\n");
208 return 0;
210 </pre>
211 The result
212 (as you might guess)
213 should be an approximation of the first three verses of a popular
214 (if occasional tiresome)
215 song.
216 <br>
217 <br>
218 a. Add a few more verses (calling birds, golden rings, geese a-laying, etc.).
219 <br>
220 <br>
221 b. Here is a scrap of code to print words for the days instead of digits:
222 <pre>
223 if(day == 1)
224 printf("first");
225 else if(day == 2)
226 printf("second");
227 else if(day == 3)
228 printf("third");
229 </pre>
230 Incorporate this code into the program.
231 <br>
232 <br>
233 c. Here is another way of writing the inner part of the program:
234 <pre>
235 printf("On the %d day of Christmas, ", day);
236 printf("my true love gave to me\n");
237 if(day &gt;= 3)
238 printf("Three French hens,\n");
239 if(day &gt;= 2)
240 printf("Two turtledoves,\n");
241 if(day &gt;= 1)
243 if(day == 1) printf("A ");
244 else printf("And a ");
245 printf("partridge in a pear tree.\n");
247 </pre>
248 Study this alternate method and figure out how it works.
249 Notice that there are no <TT>else</TT>'s between the <TT>if</TT>'s.
250 </OL><p><B>Exercises:
251 </B><br>(As before, these range from easy to harder.
252 Do as many as you like, but at least two or three.)
253 </p><OL><li>Write a program to find out
254 (the hard way, by counting them)
255 how many of the numbers from 1 to 10 are greater than 3.
256 (The answer, of course, should be 7.)
257 Your program should have a loop which steps a variable
258 (probably named <TT>i</TT>)
259 over the 10 numbers.
260 Inside the loop,
261 if <TT>i</TT> is greater than 3,
262 add one to a second variable
263 which keeps track of the count.
264 At the end of the program,
265 after the loop has finished,
266 print out the count.
267 <li>Write a program to compute the average of the ten numbers
268 1, 4, 9, ..., 81, 100,
269 that is, the average of the squares of the numbers from 1 to 10.
270 (This will be a simple modification of Exercise 3 from last week:
271 instead of printing each square as it is computed,
272 add it in to a variable <TT>sum</TT>
273 which keeps track of the sum of all the squares,
274 and then at the end,
275 divide the sum variable by
276 the number of numbers summed.)
277 <br>
278 <br>
279 If you keep track of the sum in a variable of type <TT>int</TT>,
280 and divide by the integer 10,
281 you'll get an integer-only approximation of the average
282 (the answer should be 38).
283 If you keep track of the sum
284 in a variable of type <TT>float</TT> or <TT>double</TT>,
285 on the other hand,
286 you'll get the answer as a floating-point number,
287 which you should print out
288 using <TT>%f</TT> in the <TT>printf</TT> format string,
289 <em>not</em> <TT>%d</TT>.
290 (In a <TT>printf</TT> format string,
291 <TT>%d</TT> prints only integers,
293 <TT>%f</TT> is one way to print floating-point numbers.
294 In this case,
295 the answer should be 38.5.)
296 <li>Write a program to print the numbers between 1 and 10,
297 along with an indication of whether each is even or odd,
298 like this:
299 <pre>
300 1 is odd
301 2 is even
302 3 is odd
304 </pre>
305 (Hint: use the <TT>%</TT> operator.)
306 <li>On page 3 of chapter 3 of the notes
308 is a scrap of code to print the words
309 ``Northeast'', ``Southwest'', etc. corresponding
310 to the <TT>x</TT> and <TT>y</TT> components of a direction of travel.
311 Modify the code to also print ``North'', ``East'',
312 etc. if <TT>x</TT> or <TT>y</TT> is 0.
313 <li>Write a program to print the first 7 positive integers
314 and their factorials.
315 (The factorial of 1 is 1,
316 the factorial of 2 is 1 <TT>*</TT> 2 = 2,
317 the factorial of 3 is 1 <TT>*</TT> 2 <TT>*</TT> 3 = 6,
318 the factorial of 4 is
319 1 <TT>*</TT> 2 <TT>*</TT> 3 <TT>*</TT> 4 = 24,
320 etc.)
321 [Extra credit:
322 why did I only ask for the first 7?]
323 <li>Write a program to print the first 10 Fibonacci numbers.
324 Each Fibonacci number is the sum of the two preceding ones.
325 The sequence starts out 0, 1, 1, 2, 3, 5, 8, ...
326 <li>Make some improvements to the prime number printing program
327 in section
330 of the notes.
331 Other than 2, no even numbers are prime,
332 so have the program test only the <em>odd</em> numbers between 3 and 100.
333 Rather than recomputing <TT>sqrt(i)</TT> each time through the inner loop,
334 compute it just once for each new value of <TT>i</TT>
335 (that is, compute it at the beginning of the body of the outer loop,
336 just before the beginning of the inner loop)
337 and assign its value to another variable.
338 </OL><hr>
339 <hr>
341 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
342 // <a href="copyright.html">Copyright</a> 1995-9
343 // <a href="mailto:scs@eskimo.com">mail feedback</a>
344 </p>
345 </body>
346 </html>