Question: Programming Exercise Chapter 7 Problem 17 Create a virtual. digital Galton Board. Your code will use a function - char dropMarble() to determine the marbles

Programming Exercise Chapter 7 Problem 17

Create a virtual. digital Galton Board.

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 am stuck because all I need to output is that for slot 0-8 there are # of balls in each depending on how many balls you drop. I think I am trying to make it more complicated than it should be.

Here is what I have so far:

#include #include #include #include using namespace std;

//Return a L or R at random int marblePos(string str, char r) { int count = 0;

for(int i = 0; i < str.size(); i++) { if(str[i] == r)count++; }

return count; }

char dropMarble(int slot[8], int numOfSlots) { char marblePath = ""; for(int i = 0; i < numberOfSlots - 1; i++) { int r = (rand() * 10 + 1) % 2;

if (r > 0) { marblePath + "R"; } else { marblePath = marblePath + "L"; } } int position = marblePos(marblePath, 'R'); slot[position]++;

return marblePath; }

bool isRowEmpty(int slots[8], int rowNumb, int numberOfSlots) { for (int i = 0; i < numberOfSlots; ++i) { if(slots[i] != 0) { return false; } } return true; }

bool isRowEmpty(int slots[8], int rowNumb) { for(int i = 0; i < numberOfSlots; ++i) { if(slots[i] == rowNum) { return false; } } return true; }

void arrayOfIntegers(int slots[], int numberOfMarbles, int numberOfSlots) { while(!isEmpty(slots, numberOfSlots)) { if(isRowEmpty(slots, numberOfBalls, int numberOfSlots)) { numberOfMarbles--; continue; }

numberOfMarbles --; cout << "" << endl; } }

int main() { srand(time(NULL));

int numberOfMarbles; int numberOfRows = 7; int arrayOfIntegers; char marblePath;

cout << "Enter the number of balls to drop: "; cin >> numberofBalls;

string marblePaths[numberOfMarbles];

int slots[8];

for(int i = 0; i < numberOfSlots; ++i) { slots[i] = 0; }

for (int i = 0; i < numberOfMarbles; i++) { marblePath[i] = dropMarble(slots, numberOfSlots);

}

cout << "" << endl;

print arrayOfIntegers(slots, numberOfMarbles, numberOfSlots);

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!