C program

This is a discussion on C program within the C Programming forums, part of the General Programming Boards category; Hello, I am learning C right now...anyway I do try to understand it but I got a problem with the ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    72

    C program

    Hello,

    I am learning C right now...anyway I do try to understand it but I got a problem with the following program. It's a C program that calculates the exact change. So, since only int arithmetic is exact, I l need to break up your double into two ints, the one before the decimal point and the one after the decimal point. It gotta look like this here

    Enter the sales price: 5.66
    Enter the cash paid: 10.75
    Here's your change: 5.09
    5 dollars, 1 nickel and 4 pennies
    Want to try again? [y/n] n

    I have like no idea now how do do this one. Can someone help me, please? Very much appreciated!!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    You have no need to do that in the first place. Use double or float instead of int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by Elysia View Post
    You have no need to do that in the first place. Use double or float instead of int.
    Yes, I thought the same. But I am still not able to do it

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    char buf[50];
    double price;
    fgets(buf, sizeof(buf), stdin);
    price = strtod(buf, NULL, 10);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you insist upon using the two int approach, this is a somewhat elaborate way to do it:

    Create a struct to represent an amount, containing two ints, one for dollars and one for cents. Create functions to do the necessary arithmetic on such structs. When you want to read an amount, you can use scanf() or fgets()/sscanf() with the format specificer of something along the lines of "%d.%d".

    If you do that, the rest of the program might be trivial.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Another way to think of it is to change all your amounts into pennies by multiplying them X 100.

    Now 10.57 becomes 1057, and 5.95 becomes 595. Everything becomes an int since there are no tenths or hundred's of a penny.

    To change it back to normal change, use a while loop:

    Code:
    dollars = quarters = dimes = nickels = pennies = 0;
    
    while(amount > 0)   {
       if(amount > 100)
          amount = amount - 100;
          dollars++;
       }
       else if(amount > 24)   {
          amount = amount - 25;
          quarters++;
       }
       //continue with this logic with dimes, nickels, and pennies, until you have diminished    
       //amount to zero.
    
    }  //end of while loop

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Note that if you want the highest change, you'd want to use something like this:
    Code:
    while(amount >= 100) {
        amount -= 100;
        dollars ++;
    }
    
    /* ... */
    (Note the >= instead of >, as well.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by Adak View Post
    Another way to think of it is to change all your amounts into pennies by multiplying them X 100.

    Now 10.57 becomes 1057, and 5.95 becomes 595. Everything becomes an int since there are no tenths or hundred's of a penny.

    To change it back to normal change, use a while loop:

    Code:
    dollars = quarters = dimes = nickels = pennies = 0;
    
    while(amount > 0)   {
       if(amount > 100)
          amount = amount - 100;
          dollars++;
       }
       else if(amount > 24)   {
          amount = amount - 25;
          quarters++;
       }
       //continue with this logic with dimes, nickels, and pennies, until you have diminished    
       //amount to zero.
    
    }  //end of while loop
    So basically now I have to add the fields for the input and output and Im done?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Do you know how to write a number in binary? You start with the highest (most significant) digit. For example, to write 10 in binary, you'd see that there was an 8, and so put a 1 in the 8s column.
    Code:
    1???
    Now you're left with 10-8 = 2. Consider the next column, the fours column: there is no 4 in 2.
    Code:
    10??
    But there is a 2 in 2.
    Code:
    101?
    Which leaves you with 0. There aren't any ones in 0.
    Code:
    1010
    It's the same basic idea to convert a numerical amount into change. Start with the largest denomination -- say, whole dollars. Use as many of those as you can -- you'll eventually end up with a number less than a dollar. Now go on to the next smallest change -- say, quarters. Use as many quarters as you can, until you have less than 25 cents, and so on.

    It's just like the code I posted. I'll extend it a little.
    Code:
    while(amount >= 100) {
        amount -= 100;
        dollars ++;
    }
    
    while(amount >= 25) {
        amount -= 25;
        quarters ++;
    }
    
    /* ... */
    Of course, you could make your life easier and use / and %.
    Code:
    dollars = amount / 100;
    amount %= 100;
    quarters = amount / 25;
    amount %= 25;
    /* ... */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Little tidbit to consider... If you need to worry about printing plural or not, you should try the tertiary command.

    Code:
    :
    :
    if(dollars)        
                (dollars > 1) ? printf("%d dollars ", dollars) : printf("%d dollar ", dollars);
    :
    :
    if(quarter)
    ...
    if(dime)
    ...
    And so on till you reach your pennies.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    And many such examples can be contrived:

    Code:
    printf("%d dollar%s", dollars, dollars == 1 ? "" : "s");

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by XodoX View Post
    So basically now I have to add the fields for the input and output and Im done?
    No, you're a student, and I wouldn't let you go through this and be dumbed down.

    You need to add your input block of code, extend the coin logic I showed before, all the way down to pennies, and add your output block of code.

    As is mentioned above, and shown most eloquently by MacGyver, the tertiary operator is a great little addition to your code. That way "1 pennies" is never printed, becoming "1 penny", and "1 dollars" becomes "1 dollar", etc.

    As much as anything else though, understand how the logic works here - especially WHY you have to start with the dollars in the while loop, instead of starting with the dimes, or quarters, etc. Also, why you need to use else if in the if statement, instead of a simpler else.

    Notice also, that there are a myriad of ways to solve this problem. Some are simple and easy for nearly anyone to understand, others are complex and leave your imagination panting to catch it's breath. Others are somewhere in between.

    Any of the solutions might be "the best", depending on the exact situation. Your goal imo is to find the solution that is most clear when you look at the problem to be solved, and at the code.

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    All right then...thank you.

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    okay at least I think I got a part of it now...

    Code:
      double SalesPrice = 5.66;
    double CashPaid = 10.75;
    
    int SPDollars; // Sales price dollars
    int SPCents;   // Sales price cents
    int CPDollars; // Cash paid dollars
    int CPCents;   // Cash paid cents
    
    int Dollars;   // Number of $1 bills
    int Quarters;  // Number of Quarters
    int Dimes;     // Number of Dimes
    int Nickels;   // Number of Nickels
    int Pennies;   // Number of Pennies
    int Remaining;
    
    
    SPDollars = (int)SalesPrice;                         // SPDollars = 5
    SPCents = (int)(SalesPrice * 100.00) % 100; // 5.66 * 100.00 = 566.00; 566 % 100 = 66 cents
    CPDollars = (int)CashPaid;
    CPCents = (int)(CashPaid * 100.00) % 100;
    
    
    Dollars = CPDollars - SPDollars;             // 5
    Remaining = CPCents - SPCents;           // 9
    
    if (Remaining < 0) {
       // We had to borrow a dollar
       Dollars--;
       Remaining += 100;
    };
    
    Quarters = Remaining / 25; // How many quarters?  0
    Remaining = Remaining % 25; // How much left?  9
    Dimes = Remaining / 10; // How many dimes?  0
    Remaining = Remaining % 10; // How much left? 9
    Nickels = Remaining / 5; // How many nickels?   1
    Pennies = Remaining % 5; // How many pennies? 4
    Does that look good so far ? I know I still need to add the other part.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Overall it looks - a little over complicated (what do you care about sales price dollars and cents? Or cash price dollars and cash price cents?). You seem to have the math all set - good!

    Half those variables should wing their way to the variable graveyard. They just muck up the program with unneeded junk, imo.

    Run-time and Clarity are the clarion call for programs, and your program has no problem with run-time.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21