CS201 – Spring 2001
Prof. Hartman
Homework #3
Due Friday, February 9th
by 5:00 pm
On this and future assignments I will be giving recommended problems from the self-review exercises at the end of each chapter. Actually, you should do all of the self-review exercises, but I’ll be picking out the ones I think are most important. You do not need to turn anything in for the recommended self-review problems. Note that the answers to these problems are a couple of pages after the problems.
Recommended Self-Review problems in Chapter 2, pp140-142: 2.6, 2.9, 2.10, 2.13
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
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.
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.
6)
Modify
program 6 from the last assignment to input a five digit number and say whether
or not it is a palindrome. (A palindrome reads the same forwards as backwards,
so 13531 and 77777 and 91619 are palindromes, but 12345 and 11112 are not.)
Remember to handle numbers that don’t have five digits gracefully. (Do not
assume leading zeros, 400 is not a valid input for this problem.)