Lab 9: Arrays

Objectives

After completing this lab, you should be able to:

Update your repository

As always, start by running the following command in your repository to pull the most recent changes:

git pull

Logistics

All files referenced in this lab will be found in the lab09 subdirectory of your repository.

Is rand really Random?

We've used the rand method a few times now to help us generate random values in our programs. Generating random numbers is something we often need to do, and it's fairly important to get the job done properly (i.e., the numbers we get really ought to be random!)

To figure out if the numbers generated by rand really are random, we can make use of an array. For this exercise, you'll use rand to simulate rolls of an N-sided dice, and an array to store the results of those rolls (exactly how this is done is left to you, of course). After the simulation, your program should print out the number of times each outcome occurs, along with its corresponding percentage of the total number of rolls.

The following is a sample session with a completed program:

Enter the number of sides for your dice:
6 
Enter the number of rolls to simulate:
10

1 rolled 2 times (20.0%)
2 rolled 1 times (10.0%)
3 rolled 0 times (0.0%)
4 rolled 5 times (50.0%)
5 rolled 2 times (20.0%)
6 rolled 0 times (0.0%)

Exercise 1

Implement the program described above (allowing user input to specify both the number of faces of the dice and the number of rolls) and save it in the file lab9ex1.rb. Use your program to convince yourself (and the TA) that rand does in fact generate uniformly distributed random numbers!

Scaled Histograms

Remember the program you wrote earlier in the semester to generate bar graphs based on values stored in variables? For this exercise, you'll write a much improved version.

Your program will allow the user to input a character to use for drawing the graph, and will also read in a number that determines the width of the longest bar in the graph. After this, the user can enter as many numbers as he/she desires (each one corresponding to a separate bar in the graph) -- a blank line signifies the end of input.

The most interesting bit about your modified program is that it will automatically compute, based on all input values, a scaling factor used to scale values either "up" or "down" to fit the width specified earlier. A little bit of arithmetic wizardry is called for here!

The following are sample sessions with a completed program. First, a graph that is scaled "up":

Enter a character to use for the graph:
*
Enter the width (in characters) of the graph:
20
Enter a value in the graph:
5
Enter a value in the graph:
2
Enter a value in the graph:
10
Enter a value in the graph:
1
Enter a value in the graph:


Here's your graph:
**********
****
********************
**

And a graph that is scaled "down":

Enter a character to use for the graph:
@
Enter the width (in characters) of the graph:
20
Enter a value in the graph:
100
Enter a value in the graph:
90
Enter a value in the graph:
50
Enter a value in the graph:
18
Enter a value in the graph:
65
Enter a value in the graph:


Here's your graph:
@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@
@@@@@@@@@@
@@@
@@@@@@@@@@@@@

Exercise 2

Implement the scaled histogram program, described above, and save it as lab9ex2.rb. Demonstrate your work to the TA.