CS201 – Spring 2003
Prof. Hartman
Homework #3
Due Friday, February 7th
by 5:00 pm
Homework to be turned in (remember that all programs must
follow the guidelines from handout #3.)
1)
Write a program to
print the following:
x
x
x
x
x x
x
x x
x
x
x
x
Your program should use only
output statements that print a single ‘x’
or a single space. Use loops to make your code concise. (Hint: Use two nested
loops, with an ‘if’ test to decide whether to print an ‘x’ or a space.)
2)
Write a program that inputs an integer (we’ll call it n)
and then inputs n integers and outputs the largest two of these. If the
value of n given by the user is not reasonable, handle this gracefully.
3)
Write a program to input two integers x and y,
and print out an x-by-y rectangle of asterisks. (Problem 3 on the
last assignment had you print out a 4-by-7 rectangle of asterisks. If you
solved this with for loops then
this problem will only be three or four lines more.) Don’t forget to handle bad
input gracefully.
4)
Write
a program that inputs a positive integer (handle errors gracefully) and then
prints the following sequence of numbers.
The first number is the number that was input. Each successive number is
obtained in one of two ways. If the previous number is odd, multiply it by 3
and add 1. If the previous number is
even, divide it by 2. The sequence ends when 1 is reached. For example, if the
number 12 is entered, the program should print
12, 6, 3, 10,
5, 16, 8, 4, 2, 1
5)
Write
a program that repeatedly inputs a number from 1 to 9 (handle errors
gracefully) then prints out the number as a word (“one”, “two”, etc.). The
program should terminate when the sentinel value 0 is input.