Help with arrays in java.

  • #1
I'm new in java and
I'm trying to come up with a program that stimulates the bean machine(also known as Galton box)
Balls are dropped from the opening of the board. Everytime a ball hits, there is a 50% chance to fall to left and 50% chance to fall to the right. the piles of balls are accumulated in the slots at the bottom of the board.

I'm just trying to set up my algorithm, but I don't know how to set up my arrays. i.e, the entreis in the arrays, the Givens, results, etc.

This is a Galton box and how it works in case you don't know:
http://upload.wikimedia.org/wikipedia/commons/thumb/7/78/Galton_Box.svg/507px-Galton_Box.svg.png

I just don't know where to start.. how to set up my arrays.
Any help is appreciated.:smile:
 

Answers and Replies

  • #2
365
0
You can store the data in the form of a Pascal's triangle.
The top slot will be numbered 1.
The second row will have slots 2 and 3, third row 4,5,6, and so on.

Code:
       1
      2 3
     4 5 6
    7 8 9 10
This way, the marble/ball falls in slot one will have equal chances of falling into slots 2 or 3.
From 2 (row 2) there will be equal chances of falling into 4 or 5, and from 5 (row 3) it will have equal chances for falling into slots 8 or 9, and so on.
In short, from slot m on row n, the two possible destination slots are m+n and m+n+1.
You will only need to set up an array to store all the slots, start with slot one, keep track of the row number and keep the ball falling! The number of slots required is n(n+1)/2, where n is the number of rows.
 

Related Threads on Help with arrays in java.

  • Last Post
Replies
1
Views
4K
  • Last Post
Replies
4
Views
4K
  • Last Post
Replies
9
Views
6K
  • Last Post
Replies
6
Views
2K
  • Last Post
2
Replies
35
Views
4K
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
5
Views
629
  • Last Post
Replies
3
Views
2K
Top