Advertisement
Guest User

Buckets

a guest
Nov 6th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.71 KB | None | 0 0
  1. /*
  2. Buckets.cpp, written by Josh Mermelstein
  3. This program prompts the user for the number of buckets they want, and the sizes of those buckets
  4. it then prompts for the desired quantity of water to be made from those buckets
  5. it does simple tests to ensure all these values are positive and consistent with each other
  6. finally, it outputs a linear combination of the bucket sizes equal to the goal quantity
  7.  
  8. consider including: a function to check if any two buckets are the same size
  9.             and ask the user if they wish to replace one of them
  10.  
  11.             move the calculation of the gcd of the set of buckets[] outside of get_goal
  12.             as it is used quite frequently.
  13.  
  14. Last edit       11/06/11
  15. */
  16.  
  17. #include <iostream>
  18. #include <math.h>
  19. using namespace std;
  20.  
  21. int get_int(string message, int index);
  22. void get_sizes(int bucket[], int array_size);
  23. int gcd(int a, int b);
  24. int get_goal(int bucket[], int array_size);
  25. void calc_coefficients(int buckets[], int array_size, int coefficient[]);
  26. void reduce_coefficients(int coefficient[], int array_size, int bucket[]);
  27. void print(int buckets[], int array_size, int coefficient[], int goal);
  28.  
  29. int main()
  30. {
  31.     //stage 0, various declarations and the input of user data
  32.     //seeming working perfectly as of 10/28 at 1:01 AM.
  33.     int array_size = get_int("how many buckets would you like to use? ", -1);
  34.     int bucket[array_size];
  35.     int coefficient[array_size];
  36.     get_sizes(bucket, array_size);
  37.     int goal = get_goal(bucket, array_size);
  38.     int collective_gcd = 0;
  39.  
  40.     //stage 1, we fill coefficient[] with the appropriate values such that
  41.     //the dot product of it and bucket[] is the gcd of the bucket sizes
  42.     calc_coefficients(bucket, array_size, coefficient);
  43.  
  44.     //stage 2, we scale coefficient[] by goal/gcd of the elements of bucket
  45.     collective_gcd = bucket[0];
  46.     for(int i = 1 ; i < array_size ; i++)  //finds gcd of all bucket sizes
  47.     {
  48.         collective_gcd = gcd(bucket[i], collective_gcd);
  49.     }
  50.  
  51.     for(int i = 0; i < array_size; i++)
  52.     {
  53.         coefficient[i] *= (goal/collective_gcd);
  54.     }
  55.     //stage 3, we reduce the coefficients
  56.     reduce_coefficients(coefficient, array_size, bucket);
  57.     //stage 4, we print
  58.     print(bucket, array_size, coefficient, goal);
  59.  
  60.     return 0;
  61. }
  62. /*
  63. name    get_int
  64. passed  a string to display
  65. returns a user supplyed int
  66. */
  67. int get_int(string message, int index)
  68. {
  69.     int user_input;
  70.     cout << message;
  71.     if(index >= 0)
  72.         cout << '#' << index << ": ";
  73.     cin >> user_input;
  74.     if(user_input <= 0)
  75.     {
  76.         cout << "please enter a value greater than 0 " << endl;
  77.         user_input = get_int(message, index);
  78.     }
  79.    
  80.     return user_input;
  81. }
  82.  
  83. /*
  84. name    get_sizes
  85. passed  an array and an int of its sizes
  86. returns nothing
  87. */
  88. void get_sizes(int bucket[], int array_size)
  89. {
  90.     for(int i = 0 ; i < array_size ; i++)
  91.     {
  92.         bucket[i] = get_int("how much water can be held in bucket ", i);
  93.     }
  94. }
  95.  
  96. /*
  97. name    gcd
  98. passed  two ints
  99. returns their greates common divisor
  100. note    this is just the euclidean algorithem implemeted in a while loop
  101. */
  102. int gcd(int a, int b)
  103. {
  104.     if(b > a)
  105.         return gcd(b, a);
  106.    
  107.     int c;
  108.     c = a % b;
  109.     if(c == 0)
  110.         return b;
  111.     while(c != 0)
  112.     {
  113.         c = a % b;
  114.         a = b;
  115.         b = c;
  116.     }
  117.     return a;
  118. }
  119.  
  120. /*
  121. name    get_goal
  122. passed  an array of bucket sizes and an int of the size of that array
  123. returns the goal quantity
  124. notes   gets it own function to make checking if the goal is possible easier
  125.     checks to see if the goal is a divisor of the gcd of the set of buckets
  126.     and if the goal is between 0 and the sum of the sizes of the buckets
  127. */
  128. int get_goal(int bucket[], int array_size)
  129. {
  130.     int max_volume = 0;
  131.     int goal = get_int("how much water would you like? ", -1);
  132.     int running_gcd = bucket[0];
  133.     for(int i = 1 ; i < array_size ; i++)  //finds gcd of all bucket sizes
  134.     {
  135.         running_gcd = gcd(bucket[i], running_gcd);
  136.     }
  137.  
  138.     for(int j = 0 ; j < array_size ; j++) //finds sum of buckets volumes
  139.     {
  140.         max_volume += bucket[j];
  141.     }
  142.  
  143.     if((goal % running_gcd != 0) || (goal > max_volume) || (goal < 0))
  144.     {
  145.         cout << "The desired value cannot be made from the bucket sizes specified." << endl;
  146.         cout << "Please enter a value that is an integer multiple of " << running_gcd;
  147.         cout << " and is between 0 and " << max_volume << endl;
  148.         return get_goal(bucket, array_size);
  149.     }
  150.  
  151.     return goal;
  152. }
  153.  
  154. /*
  155. name    calc_coefficients
  156. passed  an array of bucket sizes
  157.     an int of the size of that array
  158.     and array of equal size to hold the coefficents produced
  159. returns nothing
  160. notes   not actually the extended euclidean algorithem
  161. */
  162. void calc_coefficients(int buckets[], int array_size, int coefficient[])
  163. {
  164.     int running_gcd = gcd(buckets[0], buckets[1]);
  165.     int co_gcd = 0;
  166.     int future_gcd = 0;
  167.     for(int i = 0; i < array_size ; i++)
  168.     {
  169.         coefficient[i] = 0;
  170.     }
  171.  
  172.     while((buckets[0] * coefficient[0]) + (buckets[1] * coefficient[1]) != running_gcd)
  173.     {
  174.         if((buckets[0] * coefficient[0]) + (buckets[1] * coefficient[1]) > running_gcd)
  175.         {
  176.             coefficient[1]--;
  177.         }
  178.         else if((buckets[0] * coefficient[0]) + (buckets[1] * coefficient[1]) < running_gcd)
  179.         {
  180.             coefficient[0]++;
  181.         }
  182.     }
  183.  
  184.     for(int j = 2 ; j < array_size ; j++)
  185.     {
  186.         coefficient[j] = 0;
  187.         co_gcd = 0;
  188.         future_gcd = gcd(running_gcd, buckets[j]);
  189.         while((buckets[j] * coefficient[j]) + (running_gcd * co_gcd) != future_gcd)
  190.         {  
  191.             if((buckets[j] * coefficient[j]) + (running_gcd * co_gcd) > future_gcd)
  192.             {
  193.                 coefficient[j]--;
  194.             }
  195.             else if((buckets[j] * coefficient[j]) + (running_gcd * co_gcd) < future_gcd)
  196.             {
  197.                 co_gcd++;
  198.             }
  199.         }
  200.        
  201.         //multiple all previous coefficients by co_gcd
  202.         for(int k = 0 ; k < j ; k++)
  203.         {
  204.             coefficient[k] *= co_gcd;
  205.         }
  206.         co_gcd = 0;
  207.         running_gcd = future_gcd;
  208.     }
  209.    
  210. }
  211. /*
  212. name    reduce coefficients
  213. passed  the matrix of coefficients
  214. returns nothing
  215. purpose reduce the coefficients
  216. */
  217. void reduce_coefficients(int coefficient[], int array_size, int bucket[])
  218. {
  219.     for(int i = 0; i < array_size; i++)
  220.     {
  221.         for(int j = i; j < array_size ; j++)
  222.         {
  223.             if(coefficient[i] > 0 && coefficient[j] < 0)
  224.             {
  225.                 //first number too big, second too small
  226.                 while(coefficient[i] > bucket[j])
  227.                 {
  228.                     //note, it is unnecessary to handle the case of first too small, second too big as a result of the way the previous functions do their output.
  229.                     coefficient[i] -= bucket[j];
  230.                     coefficient[j] += bucket[i];
  231.                 }              
  232.             }
  233.         }
  234.     }
  235. }
  236.  
  237. /*
  238. name    print
  239. passed  the two arrays of bucket sizes and coefficients, the sizes of those arrays, and the goal value.
  240. returns nothing
  241. purpose print the found solution
  242. */
  243. void print(int buckets[], int array_size, int coefficient[], int goal)
  244. {
  245.     for(int i = 0; i < array_size - 1; i++)
  246.     {
  247.         cout << '(' << coefficient[i] << ")*" << buckets[i] << " + ";
  248.     }
  249.     cout << '(' << coefficient[array_size - 1] << ")*" << buckets[array_size - 1] << " = " << goal << endl;
  250. }
  251.  
  252.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement