### GALTON BOX

Can you help me finish the Galton Box program? Here is what I have so far:
 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include #include using namespace std; int main(){ int ballnum; int slots; int count = 1; string direction[2] = {"L", "R"}; srand(time(0)); cout << "Enter number of balls to drop: "; cin >> ballnum; cout << "Enter number of slots in the bean machine: "; cin >> slots; for (int j = 0; j

This code tracks each ball path. I just need the visual using stars ***
and make sure the bell curve is like this
*
***
****
***
*
Last edited on
Do you count the first column as 0? or was it a mistake
Let's say there's 4 slots at the bottom.

 ``` [ ] [ ] [ ] [ ] [ ] [ ] [s] [s] [s] [s] ```

 ``` [ ] [ ] [ ] [ ] [ ] [ ] [s] [s] [s] [s] ```

We start always in the only slot available in the top. Let's call it position 0.
If we move left once, that's position 0 of the second row.
If we move left again, that's position 0 of the third row.
Finally, we move left again, that's position 0 of the fourth row.

Let's start at the top again, position 0.
Move right once, position 1.
Move right again, position 2.
Move right again, position 3.

So what we have is that, if you are moving "left", you keep your index the same.
If you are moving "right", you increase your position by one.

 ``12345678910111213141516171819202122232425262728`` `````` std::vector histogram(slots, 0); for (int j = 0; j

Edit with full example:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include #include using namespace std; int main(){ int ballnum; int slots; int count = 1; string direction[2] = {"L", "R"}; srand(time(0)); cout << "Enter number of balls to drop: "; cin >> ballnum; cout << "Enter number of slots in the bean machine: "; cin >> slots; std::vector histogram(slots, 0); for (int j = 0; j

Whoops sorry for tabs vs spaces formatting...
Last edited on
 ``1234567891011121314151617181920212223`` ``````#include #include #include #include #include using namespace std; int main() { srand( time( 0 ) ); int ballnum, slots; cout << "Enter number of balls to drop: "; cin >> ballnum; cout << "Enter number of slots in the bean machine: "; cin >> slots; vector bucket( slots, 0 ); for ( int b = 0; b < ballnum; b++ ) { int counter = 0; for ( int s = 0; s < slots - 1; s++ ) counter += rand() % 2; // Corrected (ahem!) bucket[counter]++; } for ( int s = 0; s < slots; s++ ) cout << string( bucket[s], '*' ) << '\n'; }``````

 ```Enter number of balls to drop: 100 Enter number of slots in the bean machine: 10 ** ****** ************* ****************** ********************* ******************** ************ ******* *```
Last edited on
lastchance, correct me if I'm wrong, but in your program is it possible for counter to equal the value of slots?

` if (counter == bucket.size()) { std::cout << "err" << '\n'; }`
Last edited on
Ganado, you are correct: I was just in the process of correcting it ... (honest!)
Last edited on
drops must equal slots minus 1.