Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * cs50
- * week 1
- * greedy.c assignment
- *
- * by: zangiku, the contrarian flower-lover
- *
- * A program for calculating the minimum number of US coins needed to make change
- *
- * Comments include questions -- dear reader, please reply with your answers (and clap your hands) if you're generous and you know it 🙏
- *
- **/
- #include <cs50.h>
- #include <stdio.h>
- //Include math.h for round function.
- #include <math.h>
- //Should these be global variables?
- int quarter = 25;
- int dime = 10;
- int nickel = 5;
- int penny = 1;
- //Need to set initial value to 0, or can just do "int numCoins;" ?
- int numCoins = 0;
- //Still unsure if this should be int main(void) or void main(void), and why
- int main(void){
- //Assign local float r (could do this inside for loop?), get user float input, then convert to int
- float r;
- printf("O hai! How much change is owed? ");
- float f = GetFloat();
- int floaty = round(f * 100);
- //Feel like this could be a do/while loop
- while (f <= 0){
- printf("O hai! How much change is owed? ");
- f = GetFloat();
- }
- //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
- for (r = floaty; r >= quarter; r = (r - quarter)){
- floaty = floaty - quarter;
- numCoins++;
- }
- //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
- for (r = floaty; r >= dime; r = (r - dime)){
- floaty = floaty - dime;
- numCoins++;
- }
- //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
- for (r = floaty; r >= nickel; r = (r - nickel)){
- floaty = floaty - nickel;
- numCoins++;
- }
- //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
- for (r = floaty; r >= penny; r = (r - penny)){
- floaty = floaty - penny;
- numCoins++;
- }
- printf("%i\n",numCoins);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement