Advanced Greedy Algorithm for Change Machine


/ Published in: C
Save to your folder(s)

see the change


Copy this code and paste it in your HTML
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. int main(void)
  6. {
  7. // ichangeD is equal to the change in dollars
  8. float ichangeD;
  9.  
  10. //ichangeC is equal to the change in cents
  11. int ichangeC;
  12.  
  13. do
  14. {
  15. // ask for user the change and covert it to cents
  16. printf("What is the amount of spare change owed (in dollars)?\n");
  17. ichangeD = GetFloat();
  18. ichangeC = round(ichangeD * 100);
  19.  
  20. }
  21. while(ichangeD < 0);
  22.  
  23. printf("So %.2f dollars or %.0d cents are needed!\n", ichangeD, ichangeC);
  24.  
  25. // declare variables of different possible monetary values
  26. int DaCount = 0;
  27. int DaDCount = 0;
  28. int ldollars = 0;
  29. int gdollars = 0;
  30. int hdollars = 0;
  31. int jdollars = 0;
  32. int quarters = 0;
  33. int dimes = 0;
  34. int nickles = 0;
  35. int pennies = 0;
  36.  
  37. while(ichangeC % 2000 != ichangeC)
  38. {
  39. jdollars++;
  40. DaDCount++;
  41. ichangeC = ichangeC - 2000;
  42. }
  43.  
  44. printf("%d twenty dollar bills needed\n", jdollars);
  45.  
  46.  
  47. // since remainder of something less that a certain number is that number that you
  48. // trying to divide, use a while loop to do this until something happens
  49. // then move on to the next monetary value while keeping track of count of money
  50. while(ichangeC % 1000 != ichangeC)
  51. {
  52. hdollars++;
  53. DaDCount++;
  54. ichangeC = ichangeC - 1000;
  55. }
  56.  
  57. printf("%d ten dollar bills needed\n", hdollars);
  58.  
  59. while(ichangeC % 500 != ichangeC)
  60. {
  61. ldollars++;
  62. DaDCount++;
  63. ichangeC = ichangeC - 500;
  64. }
  65.  
  66. printf("%d five dollar bills needed\n", ldollars);
  67.  
  68. while(ichangeC % 100 != ichangeC)
  69. {
  70. gdollars++;
  71. DaDCount++;
  72. ichangeC = ichangeC - 100;
  73. }
  74.  
  75. printf("%d one dollar bills needed\n", gdollars);
  76.  
  77. printf("%d bills in total needed\n", DaDCount);
  78.  
  79. while(ichangeC % 25 != ichangeC)
  80. {
  81. quarters ++;
  82. DaCount++;
  83. ichangeC = ichangeC - 25;
  84. }
  85.  
  86. printf("%d quarters needed\n", quarters);
  87.  
  88. while(ichangeC % 10 != ichangeC)
  89. {
  90. dimes++;
  91. DaCount++;
  92. ichangeC = ichangeC - 10;
  93. }
  94.  
  95. printf("%d dimes needed\n", dimes);
  96.  
  97. while(ichangeC % 5 != ichangeC)
  98. {
  99. nickles++;
  100. DaCount++;
  101. ichangeC = ichangeC - 5;
  102. }
  103.  
  104. printf("%d nickles needed\n", nickles);
  105.  
  106. while(ichangeC % 1 != ichangeC)
  107. {
  108. pennies++;
  109. DaCount++;
  110. ichangeC = ichangeC - 1;
  111. }
  112.  
  113. printf("%d pennies needed\n", pennies);
  114.  
  115. printf("%d coins in total are needed!\n", DaCount);
  116.  
  117. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.