Handouts:
Assignment #2
Assignment #1 Answers
Class Notes, Chapter 3
Reading Assignment:
Class Notes, Chapter 2
Class Notes, Chapter 3, Secs. 3.1-3.2
Class Notes, Chapter 3, Secs. 3.3-3.6 (optional)
Review Questions:
for(i = 0; i < 10; i = i + 1) printf("i is %d\n", i);?
if(T < 32) printf("ice\n"); else if(T < 212) printf("water\n"); else printf("steam\n");
int x = 3; if(x) printf("yes\n"); else printf("no\n");
int i; for(i = 0; i < 3; i = i + 1) printf("a\n"); printf("b\n"); printf("c\n");
Tutorial Section
(This section presents a few small but complete programs and asks you to work with them. If you're comfortable doing at least some of the exercises later in this assignment, there's no reason for you to work through this ``tutorial section.'' But if you're not ready to do the exercises yet, this section should give you some practice and get you started.)
#include <stdio.h> int main() { printf("Hello, "); printf("world!\n"); return 0; }You should notice that the output is identical to that of the original ``Hello, world!'' program. This shows that you can build up output using multiple calls to printf, if you like. You mark the end of a line of output (that is, you arrange that further output will begin on a new line) by printing the ``newline'' character, \n.
#include <stdio.h> int main() { int i; printf("statement 1\n"); printf("statement 2\n"); for(i = 0; i < 10; i = i + 1) { printf("statement 3\n"); printf("statement 4\n"); } printf("statement 5\n"); return 0; }This program doesn't do anything useful; it's just supposed to show you how control flow works--how statements are executed one after the other, except when a construction such as the for loop alters the flow by arranging that certain statements get executed over and over. In this program, each simple statement is just a call to the printf function.
#include <stdio.h> int main() { int i, j; printf("start of program\n"); for(i = 0; i < 3; i = i + 1) { printf("i is %d\n", i); for(j = 0; j < 5; j = j + 1) printf("i is %d, j is %d\n", i, j); printf("end of i = %d loop\n", i); } printf("end of program\n"); return 0; }This program doesn't do much useful, either; it's just supposed to show you how loops work, and how loops can be nested. The outer loop runs i through the values 0, 1, and 2, and for each of these three values of i (that is, during each of the three trips through the outer loop) the inner loop runs, stepping the variable j through 5 values, 0 to 4. Experiment with changing the limits (initially 3 and 5) on the two loops. Experiment with interchanging the two loops, that is, by having the outer loop manipulate the variable j and the inner loop manipulate the variable i. Finally, compare this program to the triangle-printing program of Assignment 1 (exercise 4) and its answer as handed out this week.
#include <stdio.h> int main() { int day, i; for(day = 1; day <= 3; day = day + 1) { printf("On the %d day of Christmas, ", day); printf("my true love gave to me\n"); for(i = day; i > 0; i = i - 1) { if(i == 1) { if(day == 1) printf("A "); else printf("And a "); printf("partridge in a pear tree.\n"); } else if(i == 2) printf("Two turtledoves,\n"); else if(i == 3) printf("Three French hens,\n"); } printf("\n"); } return 0; }The result (as you might guess) should be an approximation of the first three verses of a popular (if occasionally tiresome) song.
if(day == 1) printf("first"); else if(day == 2) printf("second"); else if(day == 3) printf("third");Incorporate this code into the program.
printf("On the %d day of Christmas, ", day); printf("my true love gave to me\n"); if(day >= 3) printf("Three French hens,\n"); if(day >= 2) printf("Two turtledoves,\n"); if(day >= 1) { if(day == 1) printf("A "); else printf("And a "); printf("partridge in a pear tree.\n"); }Study this alternate method and figure out how it works. Notice that there are no else's between the if's.
Exercises:
(As before, these range from easy to harder.
Do as many as you like, but at least two or three.)
1 is odd 2 is even 3 is odd ...(Hint: use the % operator.)
This page by Steve Summit // Copyright 1995-9 // mail feedback