Advertisement
kaenan

Monezi - Lab10

Dec 5th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. /*
  2.  * DONE!
  3.  */
  4.  
  5. #include<stdlib.h>
  6. #include<stdio.h>
  7. #include<stdarg.h>
  8.  
  9. #define NEWLINE puts("");
  10.  
  11. void *ec_malloc(unsigned long size);
  12. void *ec_realloc(void *ptr, unsigned long size);
  13. void cleanup(unsigned int, ...);
  14.  
  15. #define mem_arr(ptr, size, type) ptr = (type *) ec_malloc(size * sizeof(type))
  16.  
  17. /* Read array. */
  18. void readarr(int [], unsigned long);
  19.  
  20. /* Print array. */
  21. void printarr(int [], unsigned long);
  22.  
  23.  
  24. #define arrswap(arr, pos_a, pos_b, type) { \
  25.     type aux = arr[pos_b];\
  26.     arr[pos_b] = arr[pos_a];\
  27.     arr[pos_a] = aux;\
  28. }
  29.  
  30. int isSmaller(void *a, void *b)
  31. {
  32.     int *x = (int *) a;
  33.     int *y = (int *) b;
  34.  
  35.     if (*x > *y) {
  36.         return *x;
  37.     }
  38.     if (*x < *y) {
  39.         return *x - *y;
  40.     }
  41.  
  42.     return 0;
  43. }
  44.  
  45.  
  46. int get_change(int coin_types[], unsigned int nr_coin_types, int change, int solution[]);
  47.  
  48. int main()
  49. {
  50.     int nr_coin_types, required_change, nr_coins_for_change;
  51.     int *coin_types, *change;
  52.  
  53.     printf("Nr. of coin types: ");
  54.     scanf("%d", &nr_coin_types);
  55.  
  56.     // Allocate memory for the coin types.
  57.     mem_arr(coin_types, nr_coin_types, int);
  58.  
  59.     // Read and sort the coin types.
  60.     readarr(coin_types, nr_coin_types);
  61.     qsort(coin_types, nr_coin_types, sizeof(int), isSmaller);
  62.  
  63.  
  64.     printf("Change: ");
  65.     scanf("%d", &required_change);
  66.  
  67.     // Allocate memory for the change.
  68.     mem_arr(change, required_change * nr_coin_types, int);
  69.  
  70.     // Get the change.
  71.     nr_coins_for_change = get_change(coin_types, nr_coin_types, required_change, change);
  72.  
  73.  
  74.     // Print the change.
  75.     if (nr_coins_for_change) {
  76.         printf("The minimum number of coins to form the required change: %d\n", nr_coins_for_change);
  77.  
  78.         printarr(change, nr_coins_for_change);
  79.     }
  80.  
  81.  
  82.     // Keep console window open.
  83.     getchar();
  84.  
  85.  
  86.     // Free memory.
  87.     cleanup(2, coin_types, change);
  88.  
  89.     return 0;
  90.  
  91. }
  92.  
  93. int get_change(int coin_types[], unsigned int nr_coin_types, int required_change, int change[])
  94. {
  95.     int current_change = 0, current_coin_type = nr_coin_types - 1, nth_coin = 0;
  96.  
  97.     /* Loop until current_change == change.*/
  98.     while (current_change < required_change && current_coin_type >= 0) {
  99.  
  100.         /* Add current type of coin to current change, if possible. */
  101.         while (current_change + coin_types[current_coin_type] <= required_change) {
  102.             current_change += coin_types[current_coin_type];
  103.  
  104.             change[nth_coin++] = coin_types[current_coin_type];
  105.         }
  106.  
  107.         /* If not try using the next type of coin. */
  108.         current_coin_type--;
  109.     }
  110.  
  111.     if (current_change < required_change) {
  112.         printf("The required change %d, cannot be formed with the given coin types: ", required_change);
  113.         printarr(coin_types, nr_coin_types);
  114.         return 0;
  115.     }
  116.  
  117.     return nth_coin;
  118. }
  119.  
  120.  
  121. void readarr(int arr[], unsigned long n)
  122. {
  123.     for (unsigned long i = 0; i < n; i++) {
  124.         scanf("%d", arr + i);
  125.     }
  126. }
  127.  
  128. void printarr(int arr[], unsigned long n)
  129. {
  130.     for (unsigned long i = 0; i < n; i++) {
  131.         printf("%d ", arr[i]);
  132.     }
  133.     NEWLINE;
  134. }
  135.  
  136.  
  137. void *ec_realloc(void *ptr, unsigned long size)
  138. {
  139.     void *new_ptr;
  140.  
  141.     new_ptr = realloc(ptr, size);
  142.  
  143.     if (new_ptr == NULL) {
  144.         puts("[Error] ec_realloc(): realloc() returned NULL.");
  145.     }
  146.  
  147.     return new_ptr;
  148. }
  149. void *ec_malloc(unsigned long size)
  150. {
  151.     return ec_realloc(NULL, size);
  152. }
  153.  
  154. void cleanup(unsigned int argc, ...)
  155. {
  156.     va_list args;
  157.  
  158.     va_start(args, argc);
  159.  
  160.     for (unsigned int nth_arg = 0; nth_arg < argc; ++nth_arg) {
  161.         free(va_arg(args, void*));
  162.     }
  163.  
  164.     va_end(args);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement