0

Im working on greedy.c and realized i have no idea how to organize my do-while functions (more to the point i dont understand how they interact with variables).

For example afterq (integer after quarters have been calculated) cannot be used in the following do-while for Dimes because it sits within the previous do statement. How can i update the afterq value to be used in the next do statement? or am i going about this completely incorrectly all together?

    int main(void)
{
    float change;
    int coins = 0;
    int afterq;
    int afterd;
    int aftern;
    int afterp;

    printf("Change: ");
    change = GetFloat();
    int rounded = round( change * 100 );


    do {
        int afterq = rounded - 25;
        coins = coins + 1;
    }
        while( rounded > 25 );
    do {
        int afterd = afterq - 10;
        coins = coins + 1;
    }
        while( afterq > 10 );
    do {
        int aftern = afterd - 5;
        coins = coins + 1;
    }
        while( afterd > 5 );

    do {
        int afterp = aftern - 1;
        coins = coins + 1;
    }
        while( aftern > 1 );

        printf("%i /n", coins);
}

2 Answers 2

3

There are two major issues here - scope and infinite loops - along with the do/while vs. while issue.

Scope: When you declare a variable, it only exists between the immediate set of curly braces that surround it. Once the code moves past the closing brace, it will no longer exist. In this case, you are creating vars inside the do/while loops, so after the loop exits, the variables no longer exist. If you had declared these variables at the beginning of the program, or outside any internal curly braces (but within the main() function's main curly brace pair), they would be available later.

Infinite loops: Now, look at the following:

do {
    int afterq = rounded - 25;
    coins = coins + 1;
}
    while( rounded > 25 );

If rounded is greater than 25 when this loop starts, it will run forever because the value of rounded is never changed within the loop. The only things that will terminate this loop are a manual intervention or an exit on error, such as an overflow when coins exceeds the maximum value possible for an int.

Instead, you could have simply assigned the updated value back to rounded:

rounded = rounded - 25;

This would eliminate a number of redundant variables as well.

Finally, MARS was correct in pointing out that a do/while loop is not the right choice here. Think about it. What happens if the initial amount is 10 cents? Since each loop MUST execute at least once, it will subtract 25 from 10, producing a negative amount. oops.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you, here i was thinking i had some sort of syntax error when really it was more of a logic/thought process error.
    – nnbb
    Jul 25, 2016 at 19:59
0

In general your idea is correct, but you have not considered a DO-WHILE loop is executed at least once,this form afterq (for example) is initialized incorrectly, it is more appropriate to use a WHILE loop

You must log in to answer this question.

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