Home > front end >  Why does my Loop work in a sub optimal way?
Why does my Loop work in a sub optimal way?

Time:11-25

The code works but its weird , when i run it and give the quantity 375 this is the result:

Quantity: 375 2 notes of 100 2 notes of 50 2 notes of 20 2 notes of 10 2 notes of 5 2 notes of 2 1 notes of 1

It should give me 3 notes of 100 , one note of 50 , one note of 20 and one note of 5. Im really new to coding so this might be really easy.

int main(void)

{
    int quantity = get_int("Quantity: ");
    int hundred = 0;
    int fifty = 0;
    int twenty = 0;
    int ten = 0;
    int five = 0;
    int two = 0;
    int one = 0;
    
    
    while ( quantity > 0 )
    
    {
        if ( quantity >= 100 )
        {
            quantity -= 100; 
            hundred  ;
        }
        if ( quantity >= 50 )
        {
            quantity -= 50;
            fifty  ;
        }
        if ( quantity >= 20 )
        {
            quantity -= 20;
            twenty  ;
        }
        if ( quantity >= 10 )
        {
            quantity -= 10;
            ten  ;
        }
        if ( quantity >= 5)
        {
            quantity -= 5;
            five  ;
        }
        if ( quantity >= 2)
        {
            quantity -= 2;
            two  ;
        }
        if ( quantity >= 1 )
        {
            quantity -= 1;
            one  ;
        }
    }
    
    printf("%d notes of 100\n", hundred);
    printf("%d notes of 50\n", fifty);
    printf("%d notes of 20\n", twenty);
    printf("%d notes of 10\n", ten);
    printf("%d notes of 5\n", five);
    printf("%d notes of 2\n", two);
    printf("%d notes of 1\n", one);

CodePudding user response:

well no lie it's quite ugly to do it like that but well..if it works :) here is how you can do it:

int main(void)

{
     int quantity;
     scanf("%d", &quantity);
int hundred = 0;
int fifty = 0;
int twenty = 0;
int ten = 0;
int five = 0;
int two = 0;
int one = 0;


while ( quantity >= 100 )
{
    quantity -= 100; 
    hundred  ;
}
while ( quantity >= 50 )
{
    quantity -= 50;
    fifty  ;
}
while ( quantity >= 20 )
{
    quantity -= 20;
    twenty  ;
}
while ( quantity >= 10 )
{
    quantity -= 10;
    ten  ;
}
while ( quantity >= 5)
{
    quantity -= 5;
    five  ;
}
while ( quantity >= 2)
{
    quantity -= 2;
    two  ;
}
while ( quantity >= 1 )
{
    quantity -= 1;
    one  ;
}

printf("%d notes of 100\n", hundred);
printf("%d notes of 50\n", fifty);
printf("%d notes of 20\n", twenty);
printf("%d notes of 10\n", ten);
printf("%d notes of 5\n", five);
printf("%d notes of 2\n", two);
printf("%d notes of 1\n", one);
}

CodePudding user response:

You are checking if you can remove a 100, then checking if you can remove a 50, etc, and then start all over again. That's not correct.

You want to subtract 100s until you can't. Only THEN you want to subtract 50s until you can't.

while ( quantity >= 100 ) { quantity -= 100; hundred  ; }
while ( quantity >=  50 ) { quantity -=  50; fifty  ;   }
...

A better way of achieving the same thing is to use modulus.

hundred = quantity / 100;  quantity = quantity % 100;
fifty   = quantity /  50;  quantity = quantity %  50;
...

The next step would be to use array of values.

int bills[] = { 100, 50, ... };
const size_t NUM_BILLS = sizeof(bills) / sizeof(bills[0]);

int counts[NUM_BILLS] = { 0 };

But coding this version is left to you :)

  •  Tags:  
  • c
  • Related