Chapter 7: repetition
Questions

 
1.     Write a class SHOP that sells teddy bears. The shop stores the number and price of a teddy bear. The user inputs a number, you check if you have enough, and reply "OK" or "Nope". If you have enough bears, then sell that number to the happy shopper (decrement the number of bears and increment the money). Develop the code in four steps:

a)     Code the class template and feature headers.
b)     Code a routine to process a single user input.
c)     Wrap a loop around the test, and exit the loop when all the bears have been sold.
d)     Add the ability to exit the loop at any time. Show the number of bears left at that time.

2.     Write a routine factorial to calculate n! (n factorial) using a loop.

3.     Write code to produce the following pattern; use routines for modularity:

          *
        ***
      *****
        ***
          *

4.     Adapt your code to produce the following pattern:

          *
        +++
      - - - - -
        ^^^
          !

5.     Write the code needed for a simple menu system. The menu gets a single character, and outputs a message or takes some action. The choices and actions are:

        a, A         "eh?"
        d, D         ask for a number, double it, and display the result
        e, E         exit the menu

6.     Validate the user input. If the user's choice is not valid, tell them and get a new choice; repeat this until a valid choice is entered.

7.     Write a recursive routine to find the power of a number. The arguments will be the number (REAL), the power (INTEGER), and the function returns a REAL value.

8.     The Fibonacci numbers begin with the sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and so on. Each Fibonacci number is the sum of the previous two numbers; the first two are defined to be the value 1. Write a recursive routine to show the first ten Fibonacci numbers (tricky).