Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // cos220, May Term 2015, Project 6
- // text example 7.17, page 301
- // Evan Hanzl
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- int path(int numberOfSlots);
- int max(int slots[]);
- void printResults(int slots[], int sizeOfSlots);
- // Main function
- int main() // Good except line 41
- {
- int numberOfBalls;
- int numberOfSlots;
- const int size = 50;
- int slots[size];
- // Get number of balls
- cout << "Enter the number of balls to drop: " << endl;
- cin >> numberOfBalls;
- // Get number of slots
- cout << "Enter the number of slots: " << endl;
- cin >> numberOfSlots;
- // Print the paths
- for (int i = 0; i < numberOfBalls; i++)
- {
- slots[path(numberOfSlots)]++;
- }
- }
- // Function to determine position and print the paths
- int path(int numberOfSlots) // Good
- {
- int position = 0;
- for (int i = 1; i < numberOfSlots; i++)
- {
- int direction = rand() % 2;
- if (direction == 1)
- {
- cout << "L";
- }
- else
- {
- cout << "R";
- position++;
- }
- }
- cout << endl;
- return position;
- }
- // Function to print the results
- void printResults(int slots[], int sizeOfSlots)
- {
- for (int i = 0; i < sizeOfSlots; i++)
- {
- int numOfBallsInSlot = slots[i];
- if (numOfBallsInSlot > 0)
- {
- for (int j = 0; j < numOfBallsInSlot; j++)
- {
- cout << "O";
- }
- cout << endl;
- }
- else
- {
- // Zero balls, output blank line
- cout << endl;
- }
- }
- }
- // Function to get max element in slots
- int max(int slots[])
- {
- int result = slots[0];
- for (unsigned int i = 1; i < sizeof(slots) / sizeof(slots[0]); i++)
- {
- if (result < slots[i])
- result = slots[i];
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement