Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. /*
  2. *
  3. * cs50
  4. * week 1
  5. * greedy.c assignment
  6. *
  7. * by: zangiku, the contrarian flower-lover
  8. *
  9. * A program for calculating the minimum number of US coins needed to make change
  10. *
  11. * Comments include questions -- dear reader, please reply with your answers (and clap your hands) if you're generous and you know it 🙏
  12. *
  13. **/
  14.  
  15. #include <cs50.h>
  16. #include <stdio.h>
  17. //Include math.h for round function.
  18. #include <math.h>
  19.  
  20. //Should these be global variables?
  21.  
  22. int quarter = 25;
  23. int dime = 10;
  24. int nickel = 5;
  25. int penny = 1;
  26.  
  27. //Need to set initial value to 0, or can just do "int numCoins;" ?
  28. int numCoins = 0;
  29.  
  30. //Still unsure if this should be int main(void) or void main(void), and why
  31. int main(void){
  32.  
  33. //Assign local float r (could do this inside for loop?), get user float input, then convert to int
  34. float r;
  35. printf("O hai! How much change is owed? ");
  36. float f = GetFloat();
  37. int floaty = round(f * 100);
  38.  
  39. //Feel like this could be a do/while loop
  40. while (f <= 0){
  41. printf("O hai! How much change is owed? ");
  42. f = GetFloat();
  43. }
  44.  
  45. //First: If change owed is greater than 25 cents, reduce by 25 cents until change owed is less than 25 cents. Will skip if change is initially less than 25 cents. If true, add to counter
  46. for (r = floaty; r >= quarter; r = (r - quarter)){
  47. floaty = floaty - quarter;
  48. numCoins++;
  49. }
  50.  
  51. //Second: If change owed is greater than 10 cents, reduce by 10 cents until change owed is less than 10 cents. Will skip if change is less than 10 cents. If true, add to counter
  52. for (r = floaty; r >= dime; r = (r - dime)){
  53. floaty = floaty - dime;
  54. numCoins++;
  55. }
  56.  
  57. //Third: If change owed is greater than 5 cents, reduce by 5 cents until change owed is less than 5 cents. Will skip if change is less than 5 cents. If true, add to counter
  58. for (r = floaty; r >= nickel; r = (r - nickel)){
  59. floaty = floaty - nickel;
  60. numCoins++;
  61. }
  62.  
  63. //Last: If change owed is greater than 1 cent, reduce by 1 cents until change owed is zero. Will skip if change is zero. If true, add to counter
  64. for (r = floaty; r >= penny; r = (r - penny)){
  65. floaty = floaty - penny;
  66. numCoins++;
  67. }
  68.  
  69. printf("%i\n",numCoins);
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement