Spring Semester 2005

Bonus Problems: Preparation for the TBS
1. Suppose you determine that
• the fire extinguisher in your kitchen loses x percent of its foam every day.

How long before it drops below a certain threshold (y percent), at which point it is no longer serviceable?

Write a program that

• lets the user input the x and y
• and then reports how many weeks the fire extinguisher will last.

2. Write a program that

• lets the user input a sequence of integers
• terminated by any negative value.

The program should then report the largest and smallest values that were entered.

3. How many guesses does it take to guess a secret number between 1 and n?

For example,

• I'm thinking of a number between 1 and 100.
• I'll tell you whether your guess is too high or too low.
• Obviously, an intelligent first guess would be 50 (why?).
• If that's too low, an intelligent second guess would be 75. And so on.
If we continue to divide the range in half, we'll eventualy get down to to one number.

• Because you can divide 100 seven times (50, 25, 12, 6, 3, 1, 0),
• it will take at most seven guesses to guess a number between 1 and 100.
Write a program that
• lets the user input a positive integer, n, and then
• reports how many guesses it would take to guess a number between 1 and n.

4. A palindrome is a string that is spelled the same way backward and forward. For example,

• mom,
• 747, and

are all examples of palindromes. Two more examples are

• able was i ere i saw elba and
• a man a plan a canal panama.

Write a program that lets the user type a word or phrase and then determines whether the string is a palindrome.

5. Design and implement a program that plays Time Bomb (also known as Hangman) with the user.

Here's how the game works. The computer picks a secret word and then prints one asterisk for each letter in the word:

* * * *
The user guesses at the letters in the word. For every correct guess, an asterisk is replaced by a letter:
* e * * *
• For every incorrect guess, the time bomb's fuse is grows shorter.
• When the fuse disappears, after say, six incorrect guesses, the bomb explodes.
Store the secret words in a delimited string and invent your own representation for the time bomb.

(Just printing the count of guesses would suffice I think).

6. Design and implement a program that plays the following game with the user.

• Let the user pick a letter between A and Z (uppercase letters).
• Then let the computer guess, the secret letter.

For every guess the player has to tell the computer whether it's too high or too low.

The computer should be able to guess the letter within five guesses.

Do you see why?

Last updated by Adrian German for A201/A597/I210 on `Tue Mar 29 15:32:24 EST 2005`