Created
December 29, 2018 07:37
-
-
Save michaelmotzkus/ba03fc4394ec207af4b939d913a0f1e0 to your computer and use it in GitHub Desktop.
CS50 - Week 1 - Walkthrougs - greedy.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
#include <math.h> | |
float change; | |
int coins = 0; | |
int input_change(void); | |
int sum_coins(int coin_value); | |
int main(void) | |
{ | |
change = input_change(); | |
int coin_value[] = { 25, 10, 5, 1 }; | |
for(int i = 0; i < sizeof(coin_value)/sizeof(int); i++ ) | |
{ | |
sum_coins(coin_value[i]); | |
} | |
printf("Coins: %i\n", coins); | |
} | |
int input_change(void) | |
{ | |
do | |
{ | |
change = get_float("Please enter a change value between 0.0 and 0.99: "); | |
} | |
while(change < 0 || change >= 1); | |
return change * 100; | |
} | |
//Test, if change can be divided by value of coin_value | |
int sum_coins(coin_value) | |
{ | |
if(change / coin_value >= 1) | |
{ | |
coins = coins + change / coin_value; | |
change = fmod(change, coin_value); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment