Objective of the HW: Array, methods, passing array as a parameter to a method
Game: Bean machine
The bean machine, also known as, the Galton box, is a device for statistics experiments named after English scientist Sir Francis Galton. It consists of an upright board with evenly separated nails (or pegs) in triangular form, as shown below. Balls are dropped from the opening of the board. Every time a ball hits a nail, it has 50% chance of falling to the left (L) or to the right(R). Piles of balls are accumulated in the slots at the bottom of the board. Write a program that simulates the bean machine. Your program should prompt the user to enter the number of balls and the number of the slots in the machine. Simulate the falling of each ball by printing its path. For example, path of the first ball in the machine (with in the pegs) is RLRRLR. Path of the second ball (first one the bottom from left) is RLLLLLLLRR. Display the final buildup of the balls in the slots in a histogram.
Enter the number of balls to drop: 5
Enter the number of slots in the bean machine: 7
Output is the following
Ball 1: LRLRLRR
Ball 2: RRLLLRR
Ball 3: LLRLLRR
Ball 4: RRLLLLL
Ball 5: LRLRRLR
HISTOGRAM OF BEAN MACHINE
| | | | * | | | |
| | | | * | | | |
| | * | * | * | | | |
1 2 3 4 5 6 7
HINT: Create an array named slots. Each element in slots stores the number of balls in a slot. Each ball falls into a lot via a path. The number of Rs in a path is the position of the slot where the ball falls. For example, for the path LRLRLRR, the ball falls into slot.
You need to have the following methods beside main method in the code:
Play( int , int ): methods shows the move of a ball (L or R etc) and updates array that keeps track of ball.
Display(int ) : method that displays HISTOGRAM OF BEAN MACHINE
Max(int): takes array and returns the maximum element if the array