0

I hope you can help me with the PSET1 task - cash - less comfortable. Here is my code but I cannot figure out why - when testing the code - only 0.41 and 0.15 deliver wrong outputs (8 instead of 4 and 6 instead of 2). I hope someone can help me! Thanks so much and stay safe!

// Print number of minimum coins needed to return to customers
#include <stdio.h>
#include <math.h>`


int main(void)
{
    float n;
    //Prompt user for positive float (change) n
    do
    {
        n = get_float("How many $ are owed?: ");
    }
    while (n <= 0);
    
    //Convert $ into cents, i.e. float into integer
    int cents = round(n * 100);
   
    //Keep count on how much change is owed
    //q = quarters 25C, d = dimes 10C, p=pennies C, n=nickels 5C
   
    int q = 0; 
    int remainder = 0;
    int d = 0;
    int p = 0; 
    int m = 0;

    //Calculate number of quarters, then dimes, then nickels, then pennies = remainder

    q = cents / 25;
        
    remainder = cents % 25;
   
   if (cents < 25 || remainder < 25)
    {
        d = remainder / 10;
   
        remainder = remainder % 10;
    }
   
    else if (cents < 10 || remainder < 10)
    {
        m = cents / 5;
        
        remainder = remainder % 5;
    }
   
    else if (cents < 5 || remainder < 5)
    {
        p = remainder;
    }
   
    //add all number of coins used
   
    int coins = q + d + m + remainder;
   
    //Show results
        
    printf("%i\n", coins);
}
0
if (cents < 25 || remainder < 25)
{
    d = remainder / 10;
   
    remainder = remainder % 10;
}
   
else if (cents < 10 || remainder < 10)
{
    m = cents / 5;
        
    remainder = remainder % 5;
}
   
else if (cents < 5 || remainder < 5)
{
    p = remainder;
}

For if / else if / else blocks, the code only goes down one path. So in this code, if cents = 15, then it'll go into the first block (dimes calculation), and not into the rest of them.

If you want all of this code to run for all inputs, it shouldn't be inside if / else if / else blocks. It should all be in the main block instead.

If you want each block to maybe run, they should all be if blocks, so that any of them could run.

3
  • I changed the "else if" blocks to only "if" blocks but now all the results are wrong :-(
    – SirenLu
    Jul 27 '20 at 21:23
  • Inside some of the blocks, you're using cents, and inside some, you're using remainder where you set quarters/dimes/nickels/pennies. You should probably be the same variable the whole time. It might also help you while you're fixing things to put more printf statements inside the code, so you know what different variables are at different points, and know what's wrong. You could put printf("Quarters: %i, Dimes: %i, Nickels: %i, Pennies: %i", q, d, m, p); at the end, and see which values are right and which are wrong
    – Llenk
    Jul 27 '20 at 21:55
  • Great thanks :)
    – SirenLu
    Jul 28 '20 at 16:41

You must log in to answer this question.

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