public
anonymous / gist:5041708
Created

  • Download Gist
gistfile1_withcomments.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Note that all of these variables are initialized with undetermined values. This will be important later. */
int quarters, dimes, nickles, pennies;
float quarters_filter, dimes_filter, nickles_filter;
printf("You need change? How much?");
float dollar_amount = GetFloat();
/* I'm curious if you know why this works. */
quarters = dollar_amount / 0.25;
 
/*
This loop will execute at least once, regardless of the value of dollar_amount.
This is the point of do/while. You want to use while here so that the loop is only
entered if dollar_amount has any quarters to begin with.
*/
do
{
/* I have no idea what quarters_filter is supposed to do */
quarters_filter = dollar_amount - 0.25;
}
while (dollar_amount > 0.25 );
float quarter_reciprocal = quarters_filter - quarters;
dimes = quarter_reciprocal / 0.10;
do
{
dimes_filter = quarter_reciprocal - 0.10;
}
while (quarter_reciprocal > 0.10);
float dime_reciprocal = dimes_filter - dimes;
nickles = dime_reciprocal / 0.05;
do
{
nickles_filter = dime_reciprocal - 0.05;
}
while (dime_reciprocal > 0.05);
float nickle_reciprocal = nickles_filter - nickles;
pennies = nickle_reciprocal / 0.01;
printf("%d\n", quarters);
printf("%d\n", dimes);
printf("%d\n", nickles);
printf("%d\n", pennies);
return 0;
}
 
/* As I would write it following your approach */
void main()
{
int quarters, dimes, nickles, pennies;
printf("You need change? How much?");
float dollar_amount = GetFloat();
quarters = dollar_amount / 0.25;
 
while (dollar_amount >= 0.25)
{
dollar_amount -= 0.25;
}
dimes = dollar_amount / 0.10;
 
while (dollar_amount >= 0.10)
{
dollar_amount -= 0.10;
}
nickles = dollar_amount / 0.05;
while (dollar_amount >= 0.05)
{
dollar_amount -= 0.05;
}
pennies = dollar_amount / 0.01;
printf("%d\n", quarters);
printf("%d\n", dimes);
printf("%d\n", nickles);
printf("%d\n", pennies);
return 0;
}
 
/* As I would write it following a different approach */
void main()
{
int quarters, dimes, nickles, pennies;
printf("You need change? How much?");
float dollar_amount = GetFloat();
quarters = dollar_amount / 0.25;
dollar_amount -= quarters * 0.25;
dimes = dollar_amount / 0.10;
dollar_amount -= dimes * 0.05;
nickles = dollar_amount / 0.05;
dollar_amount -= nickles * 0.05;
pennies = dollar_amount / 0.01;
printf("%d\n", quarters);
printf("%d\n", dimes);
printf("%d\n", nickles);
printf("%d\n", pennies);
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.