Skip to main content
added 5 characters in body; edited title
Source Link
ssan
  • 3
  • 2

after running this program the output i am getting is not rightsupposed to be greedy algorithms that meant to count the number of coins to make the change owed = 0 , what is the problem?output is always less by 1

after running this program the output i am getting is not rightsupposed to be greedy algorithms that meant to count the number of coins to make the change owed = 0 , what is the problem?output is always less by 1

after running this program the output i am getting is not right , what is the problem?

this program is supposed to be greedy algorithms that meant to count the number of coins to make the change owed = 0 , the output is always less by 1

added 365 characters in body
Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

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

int get_cents(void); int calculate_quarters(int cents); int calculate_dimes(int cents); int calculate_nickels(int cents); int calculate_pennies(int cents);

int main(void) { // Ask how many cents the customer is owed int cents = get_cents();

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

int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);

int main(void)
{
    // Ask how many cents the customer is owed
    int cents = get_cents();

    // Calculate the number of quarters to give the customer
    int quarters = calculate_quarters(cents);
    cents = cents - quarters * 25;

    // Calculate the number of dimes to give the customer
    int dimes = calculate_dimes(cents);
    cents = cents - dimes * 10;

    // Calculate the number of nickels to give the customer
    int nickels = calculate_nickels(cents);
    cents = cents - nickels * 5;

    // Calculate the number of pennies to give the customer
    int pennies = calculate_pennies(cents);
    cents = cents - pennies * 1;

    // Sum coins
    int coins = quarters + dimes + nickels + pennies;

    // Print total number of coins to give the customer
    printf("%i\n", coins);

}

int get_cents(void) { int cents; do { cents = get_int("Change owe: "); } while (cents < 1); return cents; } int calculate_quarters(int cents) { int quarters = 0; while(cents > 25) { cents = (cents - 25 ); quarters++; }

}

int get_cents(void)
{
    int cents;
    do
    {
       cents = get_int("Change owe: ");
    }
    while (cents < 1);
    return cents;
}
int calculate_quarters(int cents)
{
    int quarters = 0;
    while(cents > 25)
    {
    cents = (cents - 25 );
    quarters++;
    }

    return quarters;

}

int calculate_dimes(int cents) { // TODO int dimes = 0; while(cents > 10) { cents =(cents - 10); dimes++; } return dimes; }

int calculate_nickels(int cents) { // TODO int nickels = 0; while( cents > 5) { cents = (cents - 5); nickels++; } return nickels; }

int calculate_pennies(int cents) { // TODO int pennies = 0; while(cents > 1) { cents = (cents - 1); pennies++; }

}

int calculate_dimes(int cents)
{
    // TODO
    int dimes = 0;
    while(cents > 10)
    {
        cents =(cents - 10);
        dimes++;
    }
    return dimes;
}

int calculate_nickels(int cents)
{
    // TODO
    int nickels = 0;
    while( cents > 5)
    {
        cents = (cents - 5);
        nickels++;
    }
    return nickels;
}

int calculate_pennies(int cents)
{
    // TODO
    int pennies = 0;
    while(cents > 1)
    {
        cents = (cents - 1);
        pennies++;
    }

    return pennies;
}

}

emphasized text

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

int get_cents(void); int calculate_quarters(int cents); int calculate_dimes(int cents); int calculate_nickels(int cents); int calculate_pennies(int cents);

int main(void) { // Ask how many cents the customer is owed int cents = get_cents();

// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;

// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;

// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;

// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;

// Sum coins
int coins = quarters + dimes + nickels + pennies;

// Print total number of coins to give the customer
printf("%i\n", coins);

}

int get_cents(void) { int cents; do { cents = get_int("Change owe: "); } while (cents < 1); return cents; } int calculate_quarters(int cents) { int quarters = 0; while(cents > 25) { cents = (cents - 25 ); quarters++; }

return quarters;

}

int calculate_dimes(int cents) { // TODO int dimes = 0; while(cents > 10) { cents =(cents - 10); dimes++; } return dimes; }

int calculate_nickels(int cents) { // TODO int nickels = 0; while( cents > 5) { cents = (cents - 5); nickels++; } return nickels; }

int calculate_pennies(int cents) { // TODO int pennies = 0; while(cents > 1) { cents = (cents - 1); pennies++; }

return pennies;

}

emphasized text

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

int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);

int main(void)
{
    // Ask how many cents the customer is owed
    int cents = get_cents();

    // Calculate the number of quarters to give the customer
    int quarters = calculate_quarters(cents);
    cents = cents - quarters * 25;

    // Calculate the number of dimes to give the customer
    int dimes = calculate_dimes(cents);
    cents = cents - dimes * 10;

    // Calculate the number of nickels to give the customer
    int nickels = calculate_nickels(cents);
    cents = cents - nickels * 5;

    // Calculate the number of pennies to give the customer
    int pennies = calculate_pennies(cents);
    cents = cents - pennies * 1;

    // Sum coins
    int coins = quarters + dimes + nickels + pennies;

    // Print total number of coins to give the customer
    printf("%i\n", coins);
}

int get_cents(void)
{
    int cents;
    do
    {
       cents = get_int("Change owe: ");
    }
    while (cents < 1);
    return cents;
}
int calculate_quarters(int cents)
{
    int quarters = 0;
    while(cents > 25)
    {
    cents = (cents - 25 );
    quarters++;
    }

    return quarters;
}

int calculate_dimes(int cents)
{
    // TODO
    int dimes = 0;
    while(cents > 10)
    {
        cents =(cents - 10);
        dimes++;
    }
    return dimes;
}

int calculate_nickels(int cents)
{
    // TODO
    int nickels = 0;
    while( cents > 5)
    {
        cents = (cents - 5);
        nickels++;
    }
    return nickels;
}

int calculate_pennies(int cents)
{
    // TODO
    int pennies = 0;
    while(cents > 1)
    {
        cents = (cents - 1);
        pennies++;
    }

    return pennies;
}
Source Link
ssan
  • 3
  • 2

after running this program the output i am getting is not right , what is the problem?

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

int get_cents(void); int calculate_quarters(int cents); int calculate_dimes(int cents); int calculate_nickels(int cents); int calculate_pennies(int cents);

int main(void) { // Ask how many cents the customer is owed int cents = get_cents();

// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;

// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;

// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;

// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;

// Sum coins
int coins = quarters + dimes + nickels + pennies;

// Print total number of coins to give the customer
printf("%i\n", coins);

}

int get_cents(void) { int cents; do { cents = get_int("Change owe: "); } while (cents < 1); return cents; } int calculate_quarters(int cents) { int quarters = 0; while(cents > 25) { cents = (cents - 25 ); quarters++; }

return quarters;

}

int calculate_dimes(int cents) { // TODO int dimes = 0; while(cents > 10) { cents =(cents - 10); dimes++; } return dimes; }

int calculate_nickels(int cents) { // TODO int nickels = 0; while( cents > 5) { cents = (cents - 5); nickels++; } return nickels; }

int calculate_pennies(int cents) { // TODO int pennies = 0; while(cents > 1) { cents = (cents - 1); pennies++; }

return pennies;

}

emphasized text