Advertisement
Mr_kindle

mincoin.c

Feb 10th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.19 KB | None | 0 0
  1. /*
  2.     Name: Mincoin.c
  3.     Copyright:
  4.     Author: Mr.Kindle
  5.     Date: 22-12-22 11:21
  6.     Description: This code the minimum number of coin required to make a target sum. e.g if we have 4 coins of
  7.      1,3,5,7 and the target sum is 10. then minimum 2 coin will be required to make this sum.
  8.      If target sum is not possible using these coins then it print that the sum cannot be obtained
  9.      using these coins.It also print all the coins which required to make the target sum.
  10. */
  11.  
  12.  
  13.  
  14. #include <malloc.h>
  15. #include <stdio.h>
  16. #include<stdlib.h>
  17. #define INFINITY 99
  18.  
  19. int min(int,int);
  20. void print_coins(int *dpmat, int col, int * coin);
  21.  
  22.  int n, t_sum;//taking both global so that all function can access it.
  23.  
  24. int main(void) {
  25.  
  26.  
  27.   int i, j,col;
  28.   int *coin;
  29.   int * dpmat;
  30.   int * r_coin;//store the coins required for target sum
  31.   int result;
  32.   char reply;
  33.  
  34.   printf("Enter total number of coins of different denomination\n");
  35.   scanf("%d", &n);
  36.   coin = calloc(n, sizeof(int));
  37.  
  38.   printf("\nPlease enter all different denomination of coins : ");
  39.   for (i = 0; i < n; i++) {
  40.     printf("\nEnter coin %d:  ", i+1 );
  41.     scanf("%d", &coin[i]);
  42.   }
  43.   printf("\nEnter target sum:  ");
  44.   scanf("%d", &t_sum);
  45.  
  46. dpmat = calloc(n * (t_sum + 1), sizeof(int));
  47. col = t_sum + 1;//This column is used to access the elements of the dynamically allocated dpmatrix.This col is
  48. //total width of the column of dpmat not the index of the last column.
  49.  
  50.  
  51.   for (i = 0; i < n; i++) {
  52.     for (j = 0; j <= t_sum; j++) {
  53. /*for first row of dpmatrix*/
  54.      if (i == 0)//if value of coin greater than the target at column j
  55.         {
  56.             if(j%coin[i] == 0)
  57.                 dpmat[i*col + j] = j/coin[i];
  58.             else
  59.                 dpmat[i*col +j] = INFINITY;
  60.         }
  61.       else if(coin[i] > j)
  62.         {
  63.             dpmat[i*col +j] = dpmat[(i-1)*col + j];
  64.         }
  65.     else
  66.         dpmat[i*col +j] = min(dpmat[(i-1)*col + j],1+dpmat[i*col + (j - coin[i])]);
  67.     }
  68.   }
  69.  
  70.  
  71.  
  72.   /*The last element of the dpmatrix is the answer hence printing the last element*/
  73.   /*Last element is the cross section of the last row and the last column*/
  74.   result = dpmat[(n - 1)* col + (t_sum)];
  75.   if(result == INFINITY)
  76.     {
  77.         printf("\nThis sum cannot be obtained using these coins: ");
  78.         exit(1);
  79.     }
  80.     else
  81.   printf("\nMinumum number of coin required is = %d",result);
  82.   /*Now calling a function which will print all the required coins*/
  83.     print_coins(dpmat,col,coin);//sending col as argument because dpmat is dynamically allocated and hence in order
  84.     //to access it's element its width is required which is col.
  85.  
  86.  
  87.        
  88.     printf("\nHey! do you want to print the DP matrix for this probem? \n");
  89.     printf("Press 'Y' for yes & 'N' for no: ");
  90.     scanf(" %c",&reply);
  91.     if(reply == 'y' || reply == 'Y')
  92.         {
  93.             printf("\n\nFollowing is your dpmatrix: \n\n");
  94.             printf("\n\n");
  95.             printf("Sum->");
  96.  
  97.             for (i = 0; i <= t_sum; i++)
  98.    
  99.                 printf("%2d ",i);
  100.        
  101.             printf("\n");
  102.             printf("Coin \n");
  103.    
  104.             for (i = 0; i < n; i++) {
  105.                 printf(" %d   ",coin[i]);
  106.                 for (j = 0; j <= t_sum; j++) {
  107.                     printf("%2d ", dpmat[i * col +j]);
  108.                 }
  109.                  printf("\n");
  110.             }
  111.             printf("\n\n*******NOTE*******");
  112.             printf("\n99 means infinite, It shows that this sum cannot be made using this or these coins> .");
  113.         }
  114.  free(coin);
  115.  
  116.  
  117.   free(dpmat);
  118.   return 0;
  119. }//main
  120.  
  121. int min(int a, int b)
  122.     {
  123.         if (a<=b)
  124.             return a;
  125.         else
  126.             return b;
  127.     }//end of min
  128.    
  129. void print_coins(int *dpmat, int col, int * coin)
  130.     {
  131.          /*Now all code following are in order to print all the required coins*/
  132.   /*Its complicated but what can I do guys.*/
  133.     int pointer,i,k=0,sum = t_sum; //we cannot change value of t_sum because then 'col = t_sum + 1' will be changed
  134.         //and if col changed then we cannot access dpmat's element because it is dynamically allocated
  135.  
  136.     int *r_coin;
  137.         /*This pointer will initially point to the last element of dpmat which is result also*/
  138.         pointer = dpmat[(n - 1)* col + (t_sum)];//last element of dpmat which is result
  139.         int column = t_sum ;//currently this column is index of the last column of dpmat
  140.         int row = n-2;//first time the pointer will be in last row hence we will start comparison from 2nd lastrow
  141.         //please understand the difference between 'column' and 'col' declared above.
  142.         /*The required coin may be in large numbers so making r_coin a big array*/
  143.         r_coin = calloc(100, sizeof(int));
  144.        
  145.      
  146.   while(sum)//untill sum != 0
  147.     {
  148.      
  149.         /*keep moving the pointer in above direction in same  column until we get different values*/
  150.         for(i=row;i>=0;i--)
  151.             {  
  152.                 if(pointer == dpmat[i*col + column])
  153.                     {
  154.                         pointer = dpmat[i*col + column];
  155.                        
  156.                     }
  157.                 else
  158.                     break;
  159.             }
  160.              
  161.             /*coin[i+1] is the coin correspondig to the row in which currently the pointer is.*/
  162.             //we store this coin in R_coin array
  163.             r_coin[k++] = coin[i+1];
  164.             //since we got first coin hence will reduce it from the target sum which is sum now.
  165.             sum = sum - coin[i+1];
  166.             //the row in which the pointer is now will be compared with previous row hence now
  167.             row = i; //since i+1 is the row containing pointer hence i will be the previous row
  168.             //remaining sum will be new column for the pointer now
  169.             column = sum;//remaining sum
  170.             /*pointer will point to the last row and current column.Current column is remaining sum*/
  171.             pointer = dpmat[(i+1)* col + column];
  172.         /*repeat the same process untill we get sum == 0*/
  173.     }
  174.     printf("\n\nThe Required coins are following:  \n\n");
  175.     for(i=0;i<k;i++)//'k' is also working as a counter.
  176.         printf("%d ",r_coin[i]);
  177.        
  178.     free(r_coin);
  179.    
  180.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement