Question: C++ program to create a virtual Dalton Board. Assignment is: Your code will use a function - char dropMarble() to determine the marbles direction when

C++ program to create a virtual Dalton Board.

Assignment is: Your code will use a function - char dropMarble() to determine the marbles direction when it hits any individual peg. It will use input from the user to determine how many marbles are dropped. Your virtual Galton Board will have 7 rows of pegs and 8 slots for the marbles to drop into. The information about how many marbles will drop into each slot will be stored in an array int slots[8]. Your code will output the contents of each slot after all the (virtual) marbles have dropped. You will need to use selection structures, loops, and nested loops to solve this problem.

I have gotten this far but fail to understand how to fix my code in regards to j and columns[j] not being in the main scope. How do I get this fixed so that It outputs what my intentions are?

Here is the code I have:

#include

#include

using namespace std;

//Decide a L or R direction at random

char dropMarble()

{

//If 1 then L

if(rand() % 2)

{

return 'L';

}

//If 2 then R

else

{

return 'R';

}

}

void dropMarble(int columns[], int cols)

{

int i = 0, j = 0;

char LorR;

while((i + j) != cols)

{

LorR = dropMarble();

if(LorR == 'R')

{

//Marble goes right

++j;

}

else

{

//Marble goes left

++i;

}

}

cout << endl;

//Increment the count of marbles in the columns

++columns[j];

}

void printColumns(int columns[], int cols)

{

for(int i = 0; i< cols; ++i)

{

cout << columns[i] << "";

}

cout << endl;

}

int main()

{

srand(time(NULL));

int numberOfMarbles;

int numberOfColumns = 8;

int slots[numberOfColumns];

//Initialize the count of marbles in the columns to zero

for(int i = 1; i <= numberOfColumns; ++i)

{

slots[i] = 0;

}

cout << "Enter the number of marbles to drop: " << endl;

cin >> numberOfMarbles;

for(int i = 1; i <= numberOfMarbles; ++i)

{

cout << "The number of marbles in slot " << j << " is " << columns[j] << endl;

dropMarble(slots, numberOfMarbles);

printColumns(slots, numberOfMarbles);

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!