daniel_lawson9999

Daniel Lawson Greedy Program

Sep 19th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #import <stdio.h>
  2. #import <cs50.h>
  3. //Daniel Lawson 9/19/17
  4. int main(void){
  5.     printf("O hai! ");
  6.     double input = 0;
  7.     do{
  8.         printf("How much change is owed?\n");//prompt for change
  9.         input = get_double();//store it
  10.     }while(input<0);//repeat if invalid
  11.  
  12.     int money = (int)(input*100);//convert to pennies
  13.     int counter = 0;//keep track of how many coins it has taken
  14.     int coins[] = {25,10,5,1}; //array that stores the various types if coins
  15.     int coin =0; //keep track of which coin you are on
  16.  
  17.     while(money > 0){//repeat until all money has been taken into account
  18.        int currentCoin = coins[coin]; //get the value of the coin you are on (25, or 10,5,1)
  19.        counter += money / currentCoin; //increase the counter by how many times the current coin goes into the remaining amount of money
  20.        money %= currentCoin; //reduce money to the remainder of the previous division
  21.        coin++; //move to the next coin
  22.     }
  23.     printf("%d/n",counter); //print the minimum change required
  24. }
  25.  
  26.  
  27. /*
  28. desired output example:
  29. O hai! How much change is owed?
  30. 0.41
  31. 4
  32. */
Advertisement
Add Comment
Please, Sign In to add comment