0

im curently doing pset4, but redoing the psets one by one for practice since I feel like im not yet ready to make pset4. Just finished pset2, Cash. The code is right and works, i was just wondering if there is a way to make it cleaner, or with less lines, since im rewriting alot of stuff that I believe could be done in a much shorter way, mostly in the printf parts. Maybe with a for loop and replacing a variable with the change left after aplying the %? Any tips are really appreciated, thanks!

The code:

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    float change;
    int quarter = 25, dime = 10, nickel = 5, penny = 1, sum, sum_two, sum_three, sum_four;
    do
    {
        change = get_float("Change owed: ");
    }
    while (change < 0);

    int cents_change = round(change * 100);

    sum = cents_change % quarter;
    sum_two = sum % dime;
    sum_three = sum_two % nickel;
    sum_four = sum_three % penny;
    if (sum == 0)
    {
        printf("%i\n", cents_change / quarter);
    }
    else if (sum != 0 && sum_two == 0)
    {
        printf("%i\n", ((cents_change - sum) / quarter) + (sum / dime));
    }
    else if ( sum != 0 && sum_two != 0 && sum_three == 0)
    {
        printf("%i\n", ((cents_change - sum) / quarter) + (sum / dime) + (sum_two / nickel));
    }
    else if ( sum != 0 && sum_two != 0 && sum_three != 0 && sum_four == 0)
    {
        printf("%i\n", ((cents_change - sum) / quarter) + (sum / dime) + (sum_two / nickel) + (sum_three / penny));
    }
}

1 Answer 1

1

That is way too complicated for cash, not that mine is even close to being the best in terms of design, but its definitely prettier looking, all i did was get an int dollars from the user, round and store it in an int called cents and then initialized another int coins to 0 to keep track of change. Now i simply create 4 while loops, one for quarters, dimes, nickels and pennies each. The first loop checks if the change(cents) is 25 or more and if it is, cents is decremented by 25 and number of coins to be returned(coins) is incremented by one, then the next while loops do the same for dimes, nickels, and pennies and finally print out the minimum number of coins required to be given as change.

Here is my code, feel free to use it:

// Making the "Greedy Algorithm"

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)

{
    // Getting user input for change owed
    float dollars;
    do
    {
        dollars = get_float("Change owed (in Dollars): ");
    }
    while (dollars < 0);

    int cents = round(dollars * 100);
    int coins = 0;

    // Using quarters
    while (cents >= 25)
    {
        cents -= 25;
        coins++;
    }

    // Using dimes
    while (cents >= 10)
    {
        cents -= 10;
        coins++;
    }

    // Using nickels
    while (cents >= 5)
    {
        cents -= 5;
        coins ++;
    }

    // Using pennies
    while (cents >= 1)
    {
        cents -= 1;
        coins++;
    }

    // Final answer for coins used to pay off change owed
    printf("%i\n", coins);
}
2
  • 1
    Yea, too much stuff going on in my code. The thing is that I dont see it the same way you do, I just cant write it that way. Gotta practice more how I think while coding. Thanks for the reply, it really opened my eyes. Commented Jun 3, 2020 at 3:28
  • no problem at all, we all learn overtime, try sitting down with a paper and pen to map out the problem, i used to think that using papers to learn cs was stupid turns out it changes your whole perspective, at least it did change mine:)
    – ammar79797
    Commented Jun 3, 2020 at 21:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .