Chapter 6: selection
Questions

 
1.   What is meant by structured programming?

2.   What symbols are used in a flowchart? What conventions are used in a flowchart? How many lines of code are in a box?

3.   Draw a flowchart for dining at a restaurant.

4.   List the relational operators. What arguments are taken by the relational operators? What is returned? What is the precedence order of the relational operators?

5.   List the Boolean operators. What arguments are taken by Boolean operators? What is returned? What is the precedence order of the Boolean operators?

6.   What is the precedence order for the numeric, relational, and Boolean operators?

7.    Evaluate the following expressions:
•     32 * 6 <= 9 / 43 + 5 and 'a' > 'A' or "cat" /= "category"
•     not (33 // 3 = 11 * 8 \\ 8) xor "myHeight" > "yourHeight"
•     4 - 6 ^ 2 / 3 < 5 and then 't' < 'z' and then not (p = Void)
•     (i = 0) or (j / i ) = k, where i = 0, j = 3, k = 2
•     (i = 0) or else (j / i ) = k, where i = 0, j = 3, k = 2
•     (i = 0) or else (j / i ) = k, where i = 3, j = 3, k = 1

8.    Which part of structured programming does an if statement implement?

9.    What are the three forms of an if statement? How many times does end occur in each?

10    Write the code to show whether you need an umbrella or not. Read in the rainfall for the last half hour as a REAL number. If it is raining (rain > 0), then output a message to take an umbrella; if not, take your sunglasses.

11.   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 two steps:
a)     Code the class template and feature headers.
b)     Code a routine to process a single user input.

12.    Draw a diagram to show the logic of this problem:

If it is later than 7:30, then I get up; otherwise I stay in bed. If I get up then I have breakfast. For breakfast, I have a cup of coffee and something to eat. If it is summer, I eat corn flakes; if winter I eat porridge; otherwise I eat toast. If I eat toast, then I spread it with butter and either vegemite, peanut butter, or jam. On the first and last days of the month, I use vegemite. Otherwise, on even days I use peanut butter and on odd days I use jam. If the year is 2000, I skip the toast.

13.     Using the if statement, implement a routine to display the following advice:

        Age         Reaction

        16             child
        17             eager
        18             hardworking
        19             let me out of here
        20 +          not to be trusted

Do not consider values other than those shown here; that is the responsibility of the caller.

14.     What is the format of an inspect statement? What is the restriction on its use? What are the three ways to specify selection values? Can these be combined? What happens if the current value of the test does not match any listed value?

15.     Use an inspect statement to implement the following selection and output:

        Age          Reaction

        16-20         child
        21-25         eager
        26-30         hardworking
        31-35         let me out of here
        36 +           not to be trusted

16.     For problem 15, use an inspect statement with one inspect value per branch, not a range. Is this a better solution than you used for question 15? Why?