Problem Set 2
Up Problem Set 0 Problem Set 1 Problem Set 2 Problem Set 3 Problem Set 4 Problem Set 5 Problem Set 6 Problem Set 7 Problem Set 8 Problem Set 9 Problem Set 10

 

Control Structures

Due 11:59pm Monday Sept 25

  1. Do program design exercise 11 in Chapter 3 of the text.
     
    Solution.
     

  2. Do program design exercise 16 in Chapter 3 of the text.
     
    Solution.
     

  3. Write a program to generate a histogram, as follows:

    bullet

    Have the user input a sequence of numbers between 0 and 99, using any negative number as the "sentinel" that ends the sequence.  If a non-negative number is outside the range 0-99, ignore it and ask the user to try again.

    bullet

    Keep track of how many numbers are between 0 and 9, 10 and 19, 20 and 29, and so on, up to 90 and 99.  (Hint 1: use a different variable for each bin.  Hint 2: use the && operator discussed in class to form the logical "and" of two conditions).

    bullet

    Then print a histogram that shows how many numbers occurred in each bin.  For example, suppose there were 4, 5, 7, 3, 0, 4, 6, 8, 9, and 2 numbers in each of the 10 bins, respectively.  Then the output should look like this:

     0- 9: ****
    10-19: *****
    20-29: *******
    30-39: ***
    40-49:
    50-59: ****
    60-69: ******
    70-79: ********
    80-89: *********
    90-99: **
     
    (
    Hint 3: Define a method to print the *'s.)

    Solution.