0

If I make the code posted below give the final count after 0.25s and 0.10s, the values are always correct, but as the counter loops for 0.05s and 0.01s are added, something happens and the final count becomes 419****, regardless of the input (0 or 100 - the same large number gets returned). I found that the dim_modul = qrt_modul % 10; on the 34th line ruins the whole thing. I feel like I am making the program do some impossible math, but I have no idea where. I should mention that I am compiling this on my normal Ubuntu terminal, not the Appliance.

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

int main(void)
{
float inp;
int final_count, qrt_modul, dim_modul, fiv_modul;
int a, b, c, d = 0;

printf("Enter the amount of money: ");
    inp = GetFloat();
while (inp < 0)
{
    printf("Cannot interpret, retry: ");
    inp = GetFloat();
}
int rnded_inp = round(inp * 100); 
    printf("\nThe rounded input is %i", rnded_inp);

while ((a + 1) * 25 <= rnded_inp)
{
    printf("\n a value is %i", a);  //adding tickers to see where the code starts being a giant baby
    a++;
}
    qrt_modul = rnded_inp % 25; 
while ((b + 1) * 10 <= qrt_modul)
{
    printf("\n b value is %i", b);
    b++;
}  //if the code ended here and went to the final count, it would bring a correct value
    dim_modul = qrt_modul % 10;
while ((c + 1) * 5 <= dim_modul)
{
    printf("\n c value is %i", c);
    c++;
}
    fiv_modul = dim_modul % 5;
while ((d + 1) * 1 <= fiv_modul)
{
    printf("\n d value is %i", d);
    d++;
}
    final_count = a + b + c + d;
    printf("\nThe coin amount is %i\n", final_count);
}

1 Answer 1

3

I think your program has a lot more problems than you realize. It is unnecessarily more complicated than it should be.

First of all, I couldn't compile the code you've posted in my appliance until I initialized a,b,c,d to 0. It took me a lot of time to understand the code, and I'm still not sure if I understood it correctly. The comments would've helped me, but unfortunately there were only a few of them.

The basis of the code are the while loops, and here's how each one of them should look

while (rnded_inp >= 25)          //if the value of change is larger than a 25c coin
 {
 a++;                            //you increase the count of coins used for 25c
 printf("\n a value is %i", a);  //print out the current count
 rnded_inp -= 25;               //and than take out that 25c from the value of change
 }

And then comes the next while loop, done in the same manner. I tried to fix your code, while not deviating from the steps that you've intended for this program.

Here is an example of what happens if I enter that the change is 1. That 1 is converted to 100c, of course. So, 4 25c coins are used.

Enter the amount of money: 1

The rounded input is 100
a value is 1
a value is 2
a value is 3
a value is 4
The coin amount is 4
2
  • Your while loop condition is that the value is larger than or equal to 25 cents.
    – Air
    Oct 20, 2014 at 15:27
  • Yeah, I've noticed that after posting, but didn't find it important enough to edit the post. Tnx for the upvote
    – Ice32
    Oct 20, 2014 at 16:07

You must log in to answer this question.

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