Like Tree3Likes

Brand New to C; First Program and a Couple of Questions

This is a discussion on Brand New to C; First Program and a Couple of Questions within the C Programming forums, part of the General Programming Boards category; Hey guys! I'm brand new to C and I'm having a hell of a hard time so far. I'm currently ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    16

    Exclamation Brand New to C; First Program and a Couple of Questions

    Hey guys! I'm brand new to C and I'm having a hell of a hard time so far. I'm currently writing one of my first programs for my class and I'm completely suck. We're supposed to write a cash back program where a user enters a dollar amount up to $100 and they get their change back in twenties, tens, fives, ones, quarters, dimes, nickels, and pennies.

    The amount entered has to be a double and I don't understand how I can divide a double by another double, and then take the remainder and use it for another dollar value.

    Any help is MUCH appreciated! My current code is below. Thanks!

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    int main()
    {
        float twenties;
        float twentiesRemainder;
        float tens;
        float fives = 5;
        float ones = 1;
        float quarter = .25;
        float dime = .10;
        float nickel = .05;
        float penny = .01;
        double dollarEnt;
    
    
        /*Get the total input amount*/
        printf("Please enter a dollar amount up to 100:  ");
        scanf("%lf", &dollarEnt);
    
    
        twenties = (dollarEnt/(20));
        if (twenties>=1)
            printf("%.0lf twenties", twenties);
    
    
        tens = (twenties/(10));
        if (tens>=1)
            printf("%.2lf tens", tens);
        if (fives>=1)
            printf("%.2lf", fives);
        if (ones>=1)
            printf("%.2lf", ones);
        if (quarter>=1)
            printf("%.2lf", quarter);
        if (dime>=1)
            printf("%.2lf", dime);
        if (nickel>=1)
            printf("%.2lf", nickel);
        if (penny>=1)
            printf("%.2lf", penny);
    
    
    
    
    
    
    
    
        system("Pause");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    Look up functions in the standard header <math.h>, particularly fmod(), modf(), floor(), and ceil().
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    16
    Thank you for the reply, but I'm not even sure what that means exactly. I looked up fmod() and tried using it but failed.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    You need to persist. fmod() does exactly what you are looking for, in terms of finding a remainder upon division of floating point values. To use fmod(), there needs to be an "#include <math.h>" line in code.

    You may also need to change linker settings to enable floating point support (but the way to do that depends on your development environment, so you will need to read documentation for your compiler and linker or IDE).

    Beyond that, just saying you "failed" is not really enough to get useful help. Forum members are not mindreaders, remember - and there is more than one way you might have failed. The hints I've given above are educated guesses, but you haven't given enough information to get more than that.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    I don't agree with using new never-before-seen functions from the math.h library will be the best approach that a beginner should be taking.

    thepsionicstorm, have you used for or while loops yet? What you could do is

    Code:
    int i;
    
    for (i=1; i<=5; i++) {
        twenties = dollarEnt/20;
        if (twenties == i) {
            printf("%d twenties", i);
        }
    }
    Notice the for loop goes up to i == 5 because that's the max amount of 20's you can have (100/20 = 5).

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Most of the variables that you've declared as floats should probably be ints.
    Code:
    int twenties = 0;
    
    //...
    
    twenties = (int)(dollarEnt / 20);
    if (twenties > 0)
        printf("%d twenties\n", twenties);
    
    // Calculate how much is left.
    dollarEnt -= twenties * 20;
    
    // Now do 10's, 5's, etc.
    PS: If you don't understand Mentallic's post, don't worry, neither do I. Although I do agree that you don't need the math functions.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by grumpy View Post
    You need to persist. fmod() does exactly what you are looking for, in terms of finding a remainder upon division of floating point values. To use fmod(), there needs to be an "#include <math.h>" line in code.

    You may also need to change linker settings to enable floating point support (but the way to do that depends on your development environment, so you will need to read documentation for your compiler and linker or IDE).
    With all due respect, please look at the title - Brand New to C; First Program and a Couple of Questions
    I myself have been studying C for over half a year now and I have no clue of what to make with what you just said, so I really doubt the OP will either.


    Quote Originally Posted by oogabooga View Post
    PS: If you don't understand Mentallic's post, don't worry, neither do I. Although I do agree that you don't need the math functions.
    Agreed. Your code looks much simpler. When I wrote it out myself I used ints instead of floats, and who knows what I was thinking when I came up with the loop
    Last edited by Mentallic; 09-15-2012 at 11:46 PM.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,176
    What you could do is
    Yes. It wouldn't necessarily work because of the use of floating point values, but it could work.

    The question is, why would you do that?

    Most of the variables that you've declared as floats should probably be ints.
    Yeah.

    There is no reason for this to be so complicated as you've made it out to be or involve floating point mathematics in any way.

    You have a $100.00 maximum value; that's 10000 pennies.

    I myself have been studying C for over half a year now and I have no clue of what to make with what you just said, so I really doubt the OP will either.
    *shrug*

    It isn't the best suggestion posted, but the posts grumpy made are the only posts that contains an actual answer to the question.

    If you need the remainder of a floating point operation you need to use `fmod'.

    I suppose you could do the operation "long hand", but that would be kind of weird.

    Soma

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    16
    lol thank you guys. thank you @grumpy, although i didn't understand what you were saying.

    @Mentallic, yes I am just starting to play with loops; I am by no means proficient in them yet though.

    @oogabooga, that worked fantastic! I do have 2 questions though:
    1. How did you know that would work? What was your process for figuring that out, so that I might be able to follow your logic the next time I encounter this problem or am able to help someone myself.
    2. The code you gave me works for 20's, 10's, 5's, and 1's as Ints, but how would I apply this code to work with quarters (.25c), dimes (.10c), nickels (.05c), and pennies (.01c)?

    Thank you all so incredibly much!

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,176
    although i didn't understand what you were saying.
    Really?

    The `fmod' thing is a function just like `printf' is a function.

    To use `fmod' correctly in your code you need the prototype just like you need the prototype for `printf' in order to use it; the prototype for `fmod' can be found in the "math.h" header.

    The environment, IDE or whatever, may be setup by default in such a way that it will not work without changing something. (This has nothing to do with programming. It is like configuring a game to use "2x" or "4x" anti-aliasing. It is just a part of using, and knowing how to use, the tools you have installed to support your programming work.)

    How did you know that would work?
    He has experience with programming and mathematics.

    What was your process for figuring that out, so that I might be able to follow your logic the next time I encounter this problem or am able to help someone myself.
    Continue to do your practice work over a long enough period of time and it will soon come naturally... whether or not you want it to come.

    Soma

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    16
    @phantom, really.
    1. I've been exposed to see an entire 3 weeks or so along with 15 other units at school and a job, trying to learn as much of this as I can when I actually have the time to.
    2. I was not asking for why he knew it would work because of his overall experience in a certain field, but rather for his reasoning regarding the particular piece of code.
    3. Thank you; I will continue to practice and would obviously not be putting in the time I currently am and taking the step of asking other generous people for help if I didn't desire to learn this material.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    Quote Originally Posted by phantomotap View Post
    It isn't the best suggestion posted, but the posts grumpy made are the only posts that contains an actual answer to the question.
    Indeed. I didn't claim my answer was perfect, but it was the OP who claimed he was constrained to using floating point (double), and asked how to compute a remainder. I answered accordingly.

    The suggestion of using loops to compute a floating point remainder is actually a pretty bad one: rounding errors build up in such loops. I would never encourage a beginner to use such a technique.

    Personally, I would never use floating point variables to represent currency, either, since decimal fractions (0.1, 0.01) CANNOT be represented exactly in floating point, but are needed when working with currencies. But, I wouldn't expect a beginner to be able to argue credibly against such requirements in a classroom, so I didn't bother to suggest that.
    phantomotap likes this.
    Right 98% of the time, and don't care about the other 3%.

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,176
    rather for his reasoning regarding the particular piece of code
    O_o

    If you have a bunch of things and sort them into as many groups of `count' as possible, with a remainder group of an unknown quantity strictly less than `count', you have `group_count' groups.

    If you take all the complete groups, the ones with `count', and add them back into one big group you are left with only the remainder group.

    This is remarkably simple mathematics.

    You are clearly too close to the problem to see a solution.

    No, really, there is no intent to offend here; you need to back away from the problem and programming in general and just work the solution out on paper.

    Soma
    rags_to_riches likes this.

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    It doesn't look like anyone gave you the easy way to do this.

    All your results are obviously supposed to be integers (you can't have part of a twenty).
    Your input must be a real number with a maximum of 2 decimals (dollars.cents).

    Therefore, convert your input to an integer by multiplying to remove (without losing) the decimals (19.23 ==> 1923)
    Now, all your other values translate from whole dollars into cents (1 dollar ==> 100 cents)

    Now it's a simple matter to process the data and determine the counts.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    Registered User
    Join Date
    Sep 2012
    Posts
    16
    @WaltP, thank you for your reply. I sort of get what you mean by making dollars into cents, but I'm not sure how I would do that with my current code. I will include the code below; I am able to input a whole number and get the correct results ($44 = 2 twenties, 4 ones), but also what shows up is '0 tens, 0 fives'.

    1. How could I make it so that the values that would be 0 do not show up.
    2. How can I alter my current code to integrate cents from .25, .10, .05, and .01? I added the .25 cents code (quarter) below the ones (ones), but when I put in an input value such as $44.50, the number of quarters never show up.

    Thank you all again!

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. couple questions for very small random number program
    By nick753 in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2010, 08:35 PM
  2. Couple of questions
    By kzar in forum C++ Programming
    Replies: 13
    Last Post: 10-08-2005, 01:13 PM
  3. couple questions...
    By seal in forum C Programming
    Replies: 8
    Last Post: 08-29-2005, 07:14 PM
  4. a couple of questions about c#
    By Rune Hunter in forum C# Programming
    Replies: 16
    Last Post: 10-07-2004, 02:06 AM
  5. A couple of Questions
    By johnnabn in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2003, 09:10 PM

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