Thread: [HELP] Changing cents into individual denominations - Programme for Uni

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    7

    [HELP] Changing cents into individual denominations - Programme for Uni

    Hi guys,
    I'm new to this forum and to programming. I started this semester and I need a bit of help with my second assignment. I tried to do something tricky (well, at least for me) and it backfired on me. I haven't actually learned how to use arrays yet but I know the basic principle of it and decided to try and implement one to improve my code.

    I'm sure the thing is a bug ridden mess but I would particularly like to point your attention to the function sortDenomination(). Is what I am trying to do in the loop possible?

    The reason why I want to do it this way and not with if statements for each denomination is because I can then easily apply this to the second part of the assignment which sees us split a double into dollars and cents and then process each separately. All I would have to differ for each is change the money_loop variable to include the last two denominations in the array. That and it would make me feel clever, which I'm not feeling right now.

    Thanks in advance,
    Ryan.

    Code:
    #include <stdio.h>
    
    /*Reads cents input from the user.*/
    void getCents(int &read_cents)
    {
        printf("Please enter the amount of cents between 5-95:\n");
        scanf("%c%*c", &read_cents);
        while ((read_cent%5 != 0) && (read_cents < 5 || read_cents > 95))
        {
            printf("Error - please enter an amount between 5-95 cents that is divisible by 5\n");
            scanf("%c%*c", &read_cents);
        }
        return;
    }
    
    /*Supposed to sort through all cents by denomination (50c, 20c ect.)*/
    void sortDenomination(int sort_cents, int money_loop)
    {
        int loop_count;
        int denomination[6];
        denomination[0] = 50;
        denomination[0] = 20;
        denomination[0] = 10;
        denomination[0] = 5;
        denomination[0] = 2;
        denomination[0] = 1;
        for (loop_count = 0; loop_count >= money_loop; loop_count++)
        {
            if (sort_cents > denomination[loop_count])
            {
                sort_cents = getDenomination(sort_cents, denomination[loop_count]);
            }    
        }
        return;
    }
    
    /*Finds number of coins of a denomination and returns remaining cents to be processed again*/
    int getDenomination(int remaining_cents, int coin_denomination)
    {
        int denomination_amount;
        denomination_amount = remaining_cents / coin_denomination;
        remaining_cents = (coin_denomination * denomination_amount) - remaining_cents;
        printf("Please give the customer %d%*c", denomination_amount);
        printf("of the %d%*c coins\n", coin_denomination);
        return(remaining_cents);
    }
    
    int main()
    {
        int cents;
        /*This is so I can adjust the length of the for loop for both dollars and cents*/
        int cents_loop = 4;
        getCents(cents);
        sortDenomination(cents, cents_loop);
        return(0);
    }

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    7
    Wait, I'm an idiot. I'm scanning for chars. brb

    EDIT: I fixed it, it complies but it didn't make a single bit of difference. I can enter a number and then the programme ends.
    Last edited by Tape_Trip; 09-16-2013 at 09:08 AM.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I haven't looked through your code closely, but having a quick glance at sortDenomination I see this:
    Code:
        int denomination[6];
        denomination[0] = 50;
        denomination[0] = 20;
        denomination[0] = 10;
        denomination[0] = 5;
        denomination[0] = 2;
        denomination[0] = 1;
    You presumably don't mean to set only the first array location over and over again, but mean to set 0, 1, 2, etc.


    Actually, this is better done like so:
    Code:
        int denomination[6] = {50, 20, 10, 5, 2, 1};
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    7
    Ah thanks oogabooga. Your exactly right.
    I'll give a shot and report my findings.

    EDIT: While certainly that was a problem apparently there are others. Thanks for pointing that out though.
    Last edited by Tape_Trip; 09-16-2013 at 09:53 AM.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I dont think the way you read the values is right. Or atleast not right in C for certaib. As your not aware of pointer ( I assume ). I would suggest the way you read the be like

    Code:
    int getCents( void )
    {
        int read_cents;
    
        printf("Please enter the amount of cents between 5-95:\n");
        scanf("%d", &read_cents);
        while ((read_cent%5 != 0) && (read_cents < 5 || read_cents > 95))
        {
            printf("Error - please enter an amount between 5-95 cents that is divisible by 5\n");
            scanf("%d", &read_cents);
        }
        return read_cents;
    }
    
    int cents = getCents();
    Ask if you dont understand!

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    7
    Hi ssharish,
    You're correct, I was passing by reference (well, at least I was trying to) which I believe is a feature of C++. I'm not sure if I was doing it correctly but that method has work fine in the past. I implemented the change you suggested just in case and still the same result I'm afraid. I can enter the integer but nothing prints, the programme simply ends.

    I'm not sure exactly where everything is going wrong but I assume it's the loop?

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    7
    Just gonna give this a bumpity bump. Back to the top with you!

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Bumping a thread is generally frowned upon.
    And you should post your current code, which would have given you a good reason to post again!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    7
    Ah thanks oogabooga, I didn't know about that. Sorry, I'll keep that in mind for the future.

    Here's what I got:
    Code:
    #include <stdio.h>
    
    /*Reads cents input from the user.*/
    int getCents(void)
    {
        int read_cents;
        printf("Please enter the amount of cents between 5-95:\n");
        scanf("%d%*c", &read_cents);
        while (read_cents < 5 && read_cents > 95)
        {
            printf("Error - please enter an amount between 5-95 cents that is divisible by 5\n");
            scanf("%d%*c", &read_cents);
        }
        return(read_cents);
    }
    
    /*Finds number of coins of a denomination and returns remaining cents to be processed again*/
    int getDenomination(int remaining_cents, int coin_denomination)
    {
        int denomination_amount;
        denomination_amount = remaining_cents / coin_denomination;
        remaining_cents = (coin_denomination * denomination_amount) - remaining_cents;
        printf("Please give the customer %d%*c", denomination_amount);
        printf("of the %d%*c coins\n", coin_denomination);
        return(remaining_cents);
    }
    
    /*Supposed to sort through all cents by denomination (50c, 20c ect.)*/
    void sortDenomination(int sort_cents, int money_loop)
    {
        int loop_count;
        int denomination[6] = {50, 20, 10, 5, 2, 1};
        for (loop_count = 0; loop_count >= money_loop; loop_count++)
        {
            if (sort_cents > denomination[loop_count])
            {
                sort_cents = getDenomination(sort_cents, denomination[loop_count]);
            }    
        }
        return;
    }
    
    int main()
    {
        int cents;
        /*This is so I can adjust the length of the for loop for both dollars and cents*/
        int cents_loop = 4;
        cents = getCents();
        sortDenomination(cents, cents_loop);
        return(0);
    }

    EDIT: One small amendment.
    Code:
    printf("Please give the customer %d", denomination_amount);
        printf("of the %d coins\n", coin_denomination);
    Last edited by Tape_Trip; 09-16-2013 at 07:36 PM.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Code:
    while (read_cents < 5 && read_cents > 95)
    
    /*
        Think carefully about your logic.
        Can an integer be both less than five AND greater than 95?
    */
    Code:
    for (loop_count = 0; loop_count >= money_loop; loop_count++)
    
    /*
        Keep in mind how a for loop works.
    
        1. initialization (first part)
        2. check condition (second part)
        3. if condition is true, execute code/block
        4. update variable(s) (third part)
        5. go back to #2
    
        The important thing to note is that the condition is checked first before
        the body of the loop is executed.  In your case, "money_loop" is 4, and
        "loop_count" is 0, so "loop_count >= money_loop" is false and the loop
        never executes.
    */
    Your "sortDenomination" and "getDenomination" functions appear to be broken. I would advise more planning, step by step on paper, before committing the algorithm to code.

    For instance, add the following line:

    Code:
    int getDenomination(int remaining_cents, int coin_denomination)
    {
        int denomination_amount;
        denomination_amount = remaining_cents / coin_denomination;
        remaining_cents = (coin_denomination * denomination_amount) - remaining_cents;
        printf("remaining cents:  %d\n",remaining_cents); /* <<< add this line */
        printf("Please give the customer %d%*c", denomination_amount);
        printf("of the %d%*c coins\n", coin_denomination);
        return(remaining_cents);
    }
    ... enter "95", and see what your calculations are doing.

  11. #11
    Registered User
    Join Date
    Sep 2013
    Posts
    7
    Thanks Matticus, it's working!
    In my defense I actually wrote out the getDenomintion() function correctly on the algorithm and desk checked it but apparently 3-in-the-morning me decided it was a good idea to swap things around when writing the code! Also, o
    n the for loop my thinking was that the loop continued until that statement was satisfied, I have no idea where I got that from. You got me dead to rights on that error check loop though, that was very silly of me *sheepish grin*.

    Thanks to every one who helped me, hopefully I wont balls up the second part and have to come running back to you all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accesing individual bytes in an int
    By kkk in forum C Programming
    Replies: 16
    Last Post: 05-19-2011, 05:03 PM
  2. How to access individual characters in an array?
    By LanguidLegend in forum C Programming
    Replies: 2
    Last Post: 02-08-2011, 06:29 AM
  3. separate integer into individual units
    By manzoor in forum C++ Programming
    Replies: 9
    Last Post: 05-12-2008, 10:03 AM
  4. Reading individual strings from an array.
    By Baaaah! in forum C Programming
    Replies: 2
    Last Post: 12-20-2005, 03:42 PM
  5. check individual characters on input
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 09:56 PM