Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
what's wrong with this code? I am trying greedy algorithsm

What I have tried:

C
#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 %i\n", coins, quarters);
}

int get_cents(void)
{
    // Get the number of cents
    int cents;
    do
    {
        cents = get_int("Change owed: ");
    }
    while (cents < 0);
    return cents;
}

int calculate_quarters(int cents)
{
    // Calculate quarters
    for (int quarters = 0; cents > 24; quarters++)
    {
       cents -= 25;
    }
    return cents / 25;
}

int calculate_dimes(int cents)
{
    // Calculate dimes
    for (int dimes = 0; cents > 9; dimes++)
    {
       cents -= 10;
    }
    return cents / 10;
}

int calculate_nickels(int cents)
{
    // Calculate nickels
    for (int nickels = 0; nickels > 4; nickels++)
    {
       cents -= 5;
    }
    return cents / 5;
}

int calculate_pennies(int cents)
{
    // Calculate pennies
    for (int pennies = 0; pennies > 0; pennies++)
    {
       cents --;
    }
     return cents;
}
Posted
Updated 18-Mar-23 7:51am
v4
Comments
Richard MacCutchan 18-Mar-23 5:05am    
Just dumping an unformatted block of code and expecting us to correct it is not the best way to get help here. So please explain exactly what is wrong with the program and why.
OriginalGriff 18-Mar-23 8:20am    
This is the second time I have formatted your code for you: it's not difficult but it is annoying when you do it the first time, and the OP ignores that and throws away the format codes to dump unformatted code on us again.

Formatting both activates the appropriate syntax highlighter, and preserves indentation - and those two things make a huge difference to the readability of code.
In future, either paste your code using the "Code Block" option on the pop-up, or by using the "code" button on the toolbar above the textbox. Make it easy for people to help you and you get better help! Make it harder, and some will ignore it and go away instead of answering.
Eyobed Demissie 18-Mar-23 8:58am    
Thanks but I am new to programming I didn't quite understand what you meant
OriginalGriff 18-Mar-23 9:21am    
When you paste code, it is treated as text unless you tell the system otherwise:
int calculate_pennies(int cents)
{
// Calculate pennies
for (int pennies = 0; pennies > 0; pennies++)
{
cents --;
}
return cents;
}
That's not particularly readable, and the bigger the code the worse it gets. By using the "Code block" option or the "code" button, you get <pre> tags around your ocde which activates the syntax highlighter:
int calculate_pennies(int cents)
{
    // Calculate pennies
    for (int pennies = 0; pennies > 0; pennies++)
    {
       cents --;
    }
     return cents;
}
See the difference?
Eyobed Demissie 18-Mar-23 13:46pm    
ok, I got it.

Quote:
What's wrong with this C code?
Well, apart from the comments being pretty much useless ... Never comment what the code is doing, people can read that - comment why it's doing it if it needs commenting:
// Ask how many cents the customer is owed
int cents = get_cents();
The comment is longer than teh code, and isn't even accurate - it presumably prompts the user for the cents owed, and gets his response. The comment doesn't reflect that so it's both unnecessary and misleading!
All your comments are like that ... but you don't comment the functions themselves, where the comment could be useful if it was accurate:
// Prompt the user to enter change owed, gets his response, 
// and validates it.
//
// Returns the number of cents owed.
int get_cents(void)
{
    int cents;
    do
    {
        cents = get_int("Change owed: ");
    }
    while (cents < 0);
    return cents;
}

Apart from that, we have no idea what you consider "Wrong": we don't know what data you are testign it with, or what the actual purpose of the code is (the assignment you were given covers what exactly your tutor wants you to submit, and we can't see that).

So assuming that cs50.h holds the definition of get_int so your code compiles (and that's another thing we can't see) I'm going to assume that it runs, but gives you unexpected results of some kind.

Which means it's time to find out why - and we can't do that because we can't run your code, and don't have your test data if we could!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
As OriginalGriff stated you missed the return statement which should get your code working.
But I would add that you work with some constants and integer division and modulo division.

C++
int DIME = 10;
countDimes = cents / DIME;
int remaining = cents % DIME

If you really want to more, than use reference operator. Try your luck with
C++
int calculateDimes(int &change);
 
Share this answer
 
int calculate_quarters(int cents)
{
    // Calculate quarters
    for (int quarters = 0; cents > 24; quarters++)
    {
       cents -= 25;
    }
}

int calculate_dimes(int cents)
{
    // Calculate dimes
    for (int dimes = 0; cents > 9; dimes++)
    {
       cents -= 10;

    }

}

int calculate_nickels(int cents)
{
    // Calculate nickels
    for (int nickels = 0; nickels > 4; nickels++)
    {
       cents -= 5;

    }

}

int calculate_pennies(int cents)
{
    // Calculate pennies
    for (int pennies = 0; pennies > 0; pennies++)
    {
       cents --;
       return cents;
    }

}


In each function you are not returning a value or correct value

Modify calculate_quarters to be

int calculate_quarters(int cents)
{
return cents / 25;
}

Do the same for each function

Regards
C Coder
 
Share this answer
 
Comments
Eyobed Demissie 18-Mar-23 8:09am    
what about now?
Or you could simplify this to:
C++
#include <stdio.h>
#include <stdlib.h>

int get_coins(int cents, int coin)
{
    int number_of_coins = 0;
    if (cents > coin)
    {
        number_of_coins = cents / coin;
    }
    return number_of_coins;
}

int main()
{
    // replace the following line with the code to get the number from the user 
    int cents = 113;

    printf("%d cents equals: ", cents);
    int quarters = get_coins(cents, 25);
    cents -= quarters * 25;
    int dimes = get_coins(cents, 10);
    cents -= dimes * 10;
    int nickels = get_coins(cents, 5);
    cents -= nickels * 5;
    printf("%d quarters, %d dimes, %d nickels, %d pennies\n", quarters, dimes, nickels, cents);

    return 0;
}
 
Share this answer
 
v5
Comments
Rick York 18-Mar-23 12:30pm    
I would change get_coins slightly to :
int get_coins( int * pcents, int coin )
{
    int number_of_coins = 0;
    if( * pcents > coin )
    {
        number_of_coins = * pcents / coin;
        *pcents -= number_of_coins * coin;
    }
    return number_of_coins;
}
this will cut down the code a bit :
    int quarters = get_coins( & cents, 25 );
    int dimes = get_coins( & cents, 10 );
    int nickels = get_coins( & cents, 5 );
Eyobed Demissie 18-Mar-23 13:51pm    
Thank you all, I am not allowed to modify my code that much but follow the comments. I still haven't got the hang of it yet but thanks.
Richard MacCutchan 19-Mar-23 4:11am    
All you need to do is modify each of your methods to do what I have shown above. The only difference being each method uses a constant value for coin.
The suggestions of Richard and Rick are already great, it would be a bit more comfortable to use if you put all this into the original functions.
C
int get_coins(int* pcents, int coin);

int calculate_quarters(int* pcents)
{
   return get_coins(pcents, 25);
}

The call would then look like this:
C
// Calculate the number of quarters to give the customer
// int quarters = get_coins(&cents, 25);
int quarters = calculate_quarters(&cents);


// edit:
Quote:
Thank you all, I am not allowed to modify my code that much ...

Even then it would not be a problem to use the suggested solution:
C
int calculate_quarters(int cents)
{
   return get_coins(&cents, 25);
}

int calculate_dimes(int cents)
{
    get_coins(&cents, 25);
    return get_coins(&cents, 10);
}
 
Share this answer
 
v3
Comments
Eyobed Demissie 20-Mar-23 3:51am    
Thank you all! I got it.
merano99 20-Mar-23 13:52pm    
If my post was helpful, I would appreciate rating it and marking it as a solution.
Eyobed Demissie 21-Mar-23 3:34am    
I really didn't use it but sure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900