Advertisement
jjessie

CS50 / greedy_func.c

Jun 21st, 2011
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. /*#####################
  2. # <greedy_func.c>              
  3. #  06-21-11 John Jessie
  4. #  <Harvard Computer Science 50>
  5. #  Pset 1 Problem 2 - remixed
  6. #  **Now using a function**
  7. #  **And pointers**
  8. #
  9. #  greedy algorithm to determine fewest
  10. #  coins to pay out amount from input
  11. #  use only quarters, dimes, nickels, and pennies.
  12. #  < for funsies I put some dollar stuff in :-} >
  13. #
  14. #####################*/
  15. #include <cs50.h>
  16. #include <stdio.h>
  17. #include <math.h>
  18.  
  19. int makechange(int *mypennies, int mycoin_amt, int *counter1, int *counter2);
  20.  
  21. int main(void) {
  22.     // get input
  23.     printf("O hai!  How much change is owed?\n$");
  24.     float change = GetFloat();
  25.     // check input
  26.     if (change <= 0) {
  27.         printf("Please enter a positive number");
  28.         float change = GetFloat();
  29.     }
  30.     int my_pennies = (int) round(change * 100);
  31.     printf("\nOk so I owe you $%.2f\n", change);
  32.     // initialize some variables
  33.     int coins, quarters, dimes, nickels, pennys;
  34.     coins = quarters = dimes = nickels = pennys = 0;
  35.     // nested do inside of do-while loop to calculate
  36.     // coin usage.
  37.     do
  38.     {
  39.    //  quarters
  40.         while (my_pennies >= 25) {
  41.             makechange(&my_pennies, 25, &coins, &quarters);
  42.         }
  43.     // dimes
  44.         while (my_pennies >= 10) {
  45.             makechange(&my_pennies, 10, &coins, &dimes);
  46.         }
  47.     // nickels
  48.         while (my_pennies >= 5) {
  49.             makechange(&my_pennies, 5, &coins, &nickels);
  50.         }
  51.     // Pennies
  52.         while (my_pennies >= 1) {
  53.             makechange(&my_pennies, 1, &coins, &pennys);
  54.         }
  55.     }
  56.     while (my_pennies > 0);
  57.    
  58.     // Output results
  59.     printf("\nYour change will be in the form of:\n%d quarters\t%d dimes\n%d nickels\t%d pennies\n", quarters, dimes, nickels, pennys);
  60.     printf("Here's how many coins I gave you: %d\n", coins);
  61.     // calculate dollars and remainder quarters
  62.     int dollars = quarters / 4;
  63.     int rem_d = quarters % 4;
  64.     // output dollar based results
  65.     printf("\nIf i could have used dollars you would have received:\n%d dollars\t%d quarters\t%d dimes\n%d nickels\t%d pennies.\n", dollars, rem_d, dimes, nickels, pennys);
  66.     printf("I would have given you %d coins and %d dollar bills.", (coins - (dollars * 4)), dollars);
  67. }
  68.  
  69. int makechange(int *mypennies, int mycoin_amt, int *counter1, int *counter2) {
  70.     *mypennies = *mypennies - mycoin_amt;
  71.     *counter1 = *counter1 + 1;
  72.     *counter2 = *counter2 + 1;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement