DragonOsman

Untitled

Oct 9th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. // Osman Zakir
  2. // 10 / 9 / 2016
  3. // Introduction to Computer Science
  4. // Problem Set 1, greedy.c
  5. // Prompt the user for the amount of change owed, then implement greedy algorithm to determine
  6. // the least amount of coins possible that can be given back to the user/customer.  Remember to always use
  7. // the biggest value coins possible first.
  8. #include <cs50.h>
  9. #include <stdio.h>
  10. #include <math.h>
  11.  
  12. int main(void)
  13. {
  14.     float change = 0;
  15.     do
  16.     {
  17.         printf("O hai! How much change is owed?\n");
  18.         change = GetFloat();
  19.     }
  20.     while (change <= 0);
  21.    
  22.     double change_in_cents = round(change);
  23.     int num_coins_owed = 0;
  24.    
  25.     while (change_in_cents - 25 != 0)
  26.     {
  27.         num_coins_owed++;
  28.         change_in_cents -= 25;
  29.     }
  30.     while (change_in_cents - 10 != 0)
  31.     {
  32.         num_coins_owed++;
  33.         change_in_cents -= 10;
  34.     }
  35.     while (change_in_cents - 5 != 0)
  36.     {
  37.         num_coins_owed++;
  38.         change_in_cents -= 5;
  39.     }
  40.     while (change_in_cents - 1 != 0)
  41.     {
  42.         num_coins_owed++;
  43.         change_in_cents -= 1;
  44.     }
  45.    
  46.     printf("%d\n", (int)num_coins_owed);
  47. }
Add Comment
Please, Sign In to add comment