Advertisement
Guest User

Problem 7.17

a guest
May 28th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. //
  2. // cos220, May Term 2015, Project 6
  3. // text example 7.17, page 301
  4. // Evan Hanzl
  5.  
  6. #include <iostream>
  7. #include <cstdlib>
  8. using namespace std;
  9.  
  10. int path(int numberOfSlots);
  11. int max(int slots[]);
  12. void printResults(int slots[], int sizeOfSlots);
  13.  
  14.  
  15.  
  16. // Main function
  17.  
  18. int main() // Good except line 41
  19. {
  20.         int numberOfBalls;
  21.         int numberOfSlots;
  22.         const int size = 50;
  23.         int slots[size];
  24.  
  25.         // Get number of balls
  26.         cout << "Enter the number of balls to drop: " << endl;
  27.         cin >> numberOfBalls;
  28.  
  29.         // Get number of slots
  30.         cout << "Enter the number of slots: " << endl;
  31.         cin >> numberOfSlots;
  32.  
  33.         // Print the paths
  34.         for (int i = 0; i < numberOfBalls; i++)
  35.         {
  36.                 slots[path(numberOfSlots)]++;
  37.         }
  38. }
  39.  
  40. // Function to determine position and print the paths
  41. int path(int numberOfSlots) // Good
  42. {
  43.         int position = 0;
  44.  
  45.         for (int i = 1; i < numberOfSlots; i++)
  46.         {
  47.                 int direction = rand() % 2;
  48.                 if (direction == 1)
  49.                 {
  50.                         cout << "L";
  51.                 }
  52.                 else
  53.                 {
  54.                         cout << "R";
  55.                         position++;
  56.                 }
  57.         }
  58.         cout << endl;
  59.  
  60.         return position;
  61. }
  62.  
  63.  
  64. // Function to print the results
  65. void printResults(int slots[], int sizeOfSlots)
  66. {
  67.     for (int i = 0; i < sizeOfSlots; i++)
  68.     {
  69.         int numOfBallsInSlot = slots[i];
  70.  
  71.         if (numOfBallsInSlot > 0)
  72.         {
  73.             for (int j = 0; j < numOfBallsInSlot; j++)
  74.             {
  75.                 cout << "O";
  76.             }
  77.             cout << endl;
  78.         }
  79.         else
  80.         {
  81.             // Zero balls, output blank line
  82.             cout << endl;
  83.         }
  84.     }
  85. }
  86.  
  87. // Function to get max element in slots
  88. int max(int slots[])
  89. {
  90.         int result = slots[0];
  91.  
  92.         for (unsigned int i = 1; i < sizeof(slots) / sizeof(slots[0]); i++)
  93.         {
  94.                 if (result < slots[i])
  95.                     result = slots[i];
  96.         }
  97.         return result;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement