CS21 Spring 2004
Homework 6
Due Sunday, February 20, before 11:30pm


Answer the following Review Questions from the C book on your own, but do not turn them in. We will post answers on the CS21 web page next week. Please complete the following two programs and turn them in.

  1. Write a program that prompts the user to enter 20 (use a constant so that this amount can be easily changed) integer values representing grades between 0 and 100 inclusive, reads in the values into an array, then outputs:

    1. the lowest and highest grades in the array
    2. the mean (average) score (similar to problem 11.3)
    3. the standard deviation of the set of grades (similar to problem 11.4)
    4. an ASCII histogram of the scores (similar to problem 11.7)

    Sample output from a working program is shown below.

    Your program should have a function, GetIntegerArray, that takes as arguments an array and a number of values to read in, and reads in values entered by the user into the array. It also should have four separate functions that take the array and its effective size as arguments and return (1) the lowest value in the array, (2) the highest value in the array, (3) the mean, and (4) the standard deviation. In addition you should have a function, MakeHistogram, that creates the histogram array from the grade array. It should take as arguments the histogram array, the grade array and its effective size, the low and high range values (0, 100 for this problem, but write your function to work for any range) and the histogram bucket width (10 for this problem, but write your function to work for any bucket width). Finally, you should add a function PrintHistogram, that takes the histogram array and its effective size, the range values, and the bucket width, and prints the ASCII histogram to the screen.

    We recommend that you implement and test this program incrementally. For example, first, implement and test GetIntegerArray, and once that works, next implement and test GetMaxValue, and so on.

    To make testing easier, you can change the size of the array to something smaller like 5 or 10, and then change it to 20 later. Also, you can redirect standard input from a file that contains your input values rather than entering them by hand each time. For example, in emacs I could create a file named "testinput" with the following contents:

    40
    41
    50
    67
    100
    99
    98
    83
    43
    33
    56
    67
    96
    55
    34
    76
    46
    88
    54
    22
    
    I can then run my program and redirect stdin (use < on the command line) from testinput:
    % ./a.out < testinput
    
      # the prompt output from my program would appear here 
      # even though the input is being read from testinput rather than
      # from the user entering values one at a time
    
      ************ Grade Report *************
      min grade:     22   
      max grade:     100  
      mean grade:    62.40
      standard dev:  24.17
      ****************************************
    
      0-9     | 
      10-19   | 
      20-29   | *
      30-39   | **
      40-49   | ****
      50-59   | ****
      60-69   | **
      70-79   | *
      80-89   | **
      90-99   | ***
      100     | *
    

  2. Probability Board (like problem 11.10, but without the graphics part) Solve the probability board problem described in 11.10, but instead of using the graphics library to display the results, use your PrintHistogram function from problem 1 to print out the resulting histogram to the terminal window. Your program should prompt the user to enter the number of bins in the probability board (between 5 and 50), and the number of marbles to drop (between 10 and 100), and then print out the ASCII histogram representing the probability board. In multiple runs of your program with the same number of bins and marbles, you should see different resulting histograms.

    Here are some sample runs of my program:

    % ./a.out
    Enter the number of bins in the probability board> 11
    Enter the number of marbles to drop> 50
    
    Probability Board for 50 marbles and 11 bins
    
    0       | 
    1       | **
    2       | **
    3       | *
    4       | *************
    5       | **********
    6       | ****************
    7       | ****
    8       | **
    9       | 
    10      | 
    
    % ./a.out
    
    Enter the number of bins in the probability board> 5
    Enter the number of marbles to drop> 20
    
    Probability Board for 20 marbles and 5 bins
    
    0       | **
    1       | ***
    2       | ************
    3       | **
    4       | *
    
    % ./a.out
    
    Enter the number of bins in the probability board> 5
    Enter the number of marbles to drop> 20
    
    Probability Board for 20 marbles and 5 bins
    
    0       | **
    1       | ******
    2       | ******
    3       | ******
    4       |