Skip to main content
edited tags
Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100
deleted 1260 characters in body
Source Link
#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)

{

float quarter = 25, dime = 10, nickle = 5, penny = 1,dollar;

int coins = 0;
int cents;

// prompts the user for the value of change in valid parameters
do
{
    printf("How much change is owed?\n");
    dollar = GetFloat();
    if (dollar == 0)
    {
        return 0;
    }
}
while (dollar <= 0);

cents = (int) roundf(dollar * 100);

// takes as many quarters as possible from the value of change
while( quarter < cents )
{
    cents = cents - quarter;
    coins++; 
}

// takes as many dimes as possible from the value of change
while( dime < cents )
{
    cents = cents - dime;
    coins++; 
}

// takes as many nickles as possible from the value of change
while( nickle < change )
{
    cents = cents - nickle;
    coins++; 
}

 // takes as many pennies as possible from the value of change
while( penny < cents )
{
    cents = cents - penny;
    coins++; 
}

// prints how many coins were used
printf("%d\n", coins);

return 0; 
}

Hey, so this is my code for greedy. When I run it, the amount of necessary coins is slightly imprecise ( when I modify it to a float value, I see just a clean 3.00000 * 10 for example with a roundf for change). For example, when I input 0.41 it shows 3 or 5 to 22 coins or 10 to 42 coins, never actually getting the precise amount of coins needed even if the aforementioned modification not in this topic shows a clean floating value.

Please help !

Removed code, problem solved.

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

int main(void)

{

float quarter = 25, dime = 10, nickle = 5, penny = 1,dollar;

int coins = 0;
int cents;

// prompts the user for the value of change in valid parameters
do
{
    printf("How much change is owed?\n");
    dollar = GetFloat();
    if (dollar == 0)
    {
        return 0;
    }
}
while (dollar <= 0);

cents = (int) roundf(dollar * 100);

// takes as many quarters as possible from the value of change
while( quarter < cents )
{
    cents = cents - quarter;
    coins++; 
}

// takes as many dimes as possible from the value of change
while( dime < cents )
{
    cents = cents - dime;
    coins++; 
}

// takes as many nickles as possible from the value of change
while( nickle < change )
{
    cents = cents - nickle;
    coins++; 
}

 // takes as many pennies as possible from the value of change
while( penny < cents )
{
    cents = cents - penny;
    coins++; 
}

// prints how many coins were used
printf("%d\n", coins);

return 0; 
}

Hey, so this is my code for greedy. When I run it, the amount of necessary coins is slightly imprecise ( when I modify it to a float value, I see just a clean 3.00000 * 10 for example with a roundf for change). For example, when I input 0.41 it shows 3 or 5 to 22 coins or 10 to 42 coins, never actually getting the precise amount of coins needed even if the aforementioned modification not in this topic shows a clean floating value.

Please help !

so this is my code for greedy. When I run it, the amount of necessary coins is slightly imprecise ( when I modify it to a float value, I see just a clean 3.00000 * 10 for example with a roundf for change). For example, when I input 0.41 it shows 3 or 5 to 22 coins or 10 to 42 coins, never actually getting the precise amount of coins needed even if the aforementioned modification not in this topic shows a clean floating value.

Please help !

Removed code, problem solved.

edited body
Source Link
#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)

{

float quarter = 0.25, dime = 0.10, nickle = 0.055, penny = 0.011,dollar;

int coins = 0;
int cents;

// prompts the user for the value of change in valid parameters
do
{
    printf("How much change is owed?\n");
    dollar = GetFloat();
    if (dollar == 0)
    {
        return 0;
    }
}
while (dollar <= 0);

cents = (int) roundf(dollar * 100);

// takes as many quarters as possible from the value of change
while( quarter < cents )
{
    cents = cents - quarter;
    coins++; 
}

// takes as many dimes as possible from the value of change
while( dime < cents )
{
    cents = cents - dime;
    coins++; 
}

// takes as many nickles as possible from the value of change
while( nickle < change )
{
    cents = cents - nickle;
    coins++; 
}

 // takes as many pennies as possible from the value of change
while( penny < cents )
{
    cents = cents - penny;
    coins++; 
}

// prints how many coins were used
printf("%i\n""%d\n", coins);

return 0; 
}

Hey, so this is my code for greedy. When I run it, the amount of necessary coins is slightly imprecise ( when I modify it to a float value, I see just a clean 3.00000 * 10 for example with a roundf for change). For example, when I input 0.41 it shows 3 or 5 to 22 coins or 10 to 42 coins, never actually getting the precise amount of coins needed even if the aforementioned modification not in this topic shows a clean floating value.

Please help !

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

int main(void)

{

float quarter = 0.25, dime = 0.10, nickle = 0.05, penny = 0.01,dollar;

int coins = 0;
int cents;

// prompts the user for the value of change in valid parameters
do
{
    printf("How much change is owed?\n");
    dollar = GetFloat();
    if (dollar == 0)
    {
        return 0;
    }
}
while (dollar <= 0);

cents = (int) roundf(dollar * 100);

// takes as many quarters as possible from the value of change
while( quarter < cents )
{
    cents = cents - quarter;
    coins++; 
}

// takes as many dimes as possible from the value of change
while( dime < cents )
{
    cents = cents - dime;
    coins++; 
}

// takes as many nickles as possible from the value of change
while( nickle < change )
{
    cents = cents - nickle;
    coins++; 
}

 // takes as many pennies as possible from the value of change
while( penny < cents )
{
    cents = cents - penny;
    coins++; 
}

// prints how many coins were used
printf("%i\n", coins);

return 0; 
}

Hey, so this is my code for greedy. When I run it, the amount of necessary coins is slightly imprecise ( when I modify it to a float value, I see just a clean 3.00000 * 10 for example with a roundf for change). For example, when I input 0.41 it shows 3 or 5 to 22 coins or 10 to 42 coins, never actually getting the precise amount of coins needed even if the aforementioned modification not in this topic shows a clean floating value.

Please help !

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

int main(void)

{

float quarter = 25, dime = 10, nickle = 5, penny = 1,dollar;

int coins = 0;
int cents;

// prompts the user for the value of change in valid parameters
do
{
    printf("How much change is owed?\n");
    dollar = GetFloat();
    if (dollar == 0)
    {
        return 0;
    }
}
while (dollar <= 0);

cents = (int) roundf(dollar * 100);

// takes as many quarters as possible from the value of change
while( quarter < cents )
{
    cents = cents - quarter;
    coins++; 
}

// takes as many dimes as possible from the value of change
while( dime < cents )
{
    cents = cents - dime;
    coins++; 
}

// takes as many nickles as possible from the value of change
while( nickle < change )
{
    cents = cents - nickle;
    coins++; 
}

 // takes as many pennies as possible from the value of change
while( penny < cents )
{
    cents = cents - penny;
    coins++; 
}

// prints how many coins were used
printf("%d\n", coins);

return 0; 
}

Hey, so this is my code for greedy. When I run it, the amount of necessary coins is slightly imprecise ( when I modify it to a float value, I see just a clean 3.00000 * 10 for example with a roundf for change). For example, when I input 0.41 it shows 3 or 5 to 22 coins or 10 to 42 coins, never actually getting the precise amount of coins needed even if the aforementioned modification not in this topic shows a clean floating value.

Please help !

added 47 characters in body
Source Link
Loading
added 24 characters in body
Source Link
Loading
improved formatting
Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56
Loading
deleted 18 characters in body
Source Link
Loading
Source Link
Loading