Like Tree8Likes
  • 2 Post By AndrewHunter
  • 2 Post By AndrewHunter
  • 3 Post By CommonTater
  • 1 Post By CommonTater

How should I go about solving this problem?

This is a discussion on How should I go about solving this problem? within the C Programming forums, part of the General Programming Boards category; Write a C function named change() that accepts a floating point number of total coins and the addresses of the ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    17

    How should I go about solving this problem?

    Write a C function named change() that accepts a floating point
    number of total coins and the addresses of the integer variables named
    quarters, dimes, nickels, and pennies. The function should
    determine the number of quarters, dimes, nickels, and pennies in the total
    coins number passed to it and write these values directly into the
    respective variables declared in its calling function using pointers.
    Call the function change() from main() three times and print out
    the contents of the variables quarters, dimes, nickels, and
    pennies after each function return.

    First Call—pass in the total value $1.88 and on return print the contents of
    the variables.

    Second Call—pass in the total value .32 and on return print the contents
    of the variables.

    Third Call— ask for a total value input from the keyboard and on return
    print the contents of the variables.
    Output should look like:

    TOTAL VALUE ENTERED: 1.88
    7 quarters
    1 dime
    0 nickels
    3 pennies

    and not:

    TOTAL VALUE ENTERED: 1.88
    7 quarters
    18 dimes
    37 nickels
    188 pennies


    The question itself is confusing to me... create a function that accepts floating point numbers and first call, second call and third call... I am not sure on how to go about this

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by ARod609 View Post
    Write a C function named change() that accepts a floating point number of total coins and the addresses of the integer variables named quarters, dimes, nickels, and pennies.
    This is telling you what your function prototype should look like. The function should take 5 arguments, a float (for total coin amount), and 4 pointers to integers (for quarters, dimes, nickels, and pennies).

    Quote Originally Posted by ARod609 View Post
    The function should determine the number of quarters, dimes, nickels, and pennies in the total coins number passed to it and write these values directly into the respective variables declared in its calling function using pointers.
    This tells you what the function should do and how it should do it.

    Quote Originally Posted by ARod609 View Post
    Call the function change() from main() three times and print out the contents of the variables quarters, dimes, nickels, and pennies after each function return.

    First Call—pass in the total value $1.88 and on return print the contents of
    the variables.

    Second Call—pass in the total value .32 and on return print the contents
    of the variables.

    Third Call— ask for a total value input from the keyboard and on return
    print the contents of the variables.
    This is telling you how to lay out your program, e.g. what your main should look like. Not worded the best, however they want you to make a program that looks something like:
    Code:
    //your include files
    
    int main(void){
         //the variables you need to make
    
         call your function with total coin = 1.88
         print your output
         
         call your function with total coin = .32
         print your output
    
         obtain user input for total coin amount
         call your function with total coin = user inputted value
         print your output
    
         return (0);
    }
    Salem and ARod609 like this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    1) Math header not sure if i need it...
    2) Function I am not sure on how to use it correctly...
    3) Output did exactly what I was not supposed to do...

    The book I am using has a bunch of exercises but no examples of the way I should format it or use the code. The only way I know if I am wrong is if the compiler tells me... lol
    Code:
    #include <stdio.h>
    #include <math.h>
    int main (int argc, const char * argv[])
    
    {
        int change(float);
        
        float  total;
        int quarters, dimes, nickels, pennies;
        printf ("Please enter your total value:");
        scanf ("%f",&total);
        
        quarters = total/.25;
        dimes = total/.10;
        nickels = total/.05;
        pennies = total/.01;
        
        
        printf ("Total Value Entered: %f \n",total);
        printf ("%9d Quarters\n",quaters);
        printf ("%9d Dimes\n",dimes);
        printf ("%9d Nickels\n",nickels);
        printf ("%9d pennies\n",pennies);
        
        return 0;
    }
    Output
    Code:
    Total Value Entered: 4.000000
           16 Quaters
           40 Dimes
           80 Nickels
          400 pennies

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by ARod609 View Post
    1) Math header not sure if i need it...
    You do not need it for this problem,

    Quote Originally Posted by ARod609 View Post
    2) Function I am not sure on how to use it correctly...
    They want a function. So the prototype would be something like:

    void change(float totalcoin, int* quarters, int* dimes, int* nickels, int* pennies);

    Quote Originally Posted by ARod609 View Post
    3) Output did exactly what I was not supposed to do...
    A hint for this: most banking software actually stores the amount in a large integer of pennies. So if I were to do something like this I would take the total input and convert it to pennies before I started, e.g. 1.10 would become 110 pennies. After that the operations become pretty simple. Look at what '/' does and what '%' does for math operations.
    beatbox32 and ARod609 like this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ARod609 View Post
    1) Math header not sure if i need it...
    2) Function I am not sure on how to use it correctly...
    3) Output did exactly what I was not supposed to do...
    No offense is intended... but, I'm betting you went straight from reading the assignment to your keyboard and started tapping out code... hence, failure.

    Every program, no matter how small, involves the 4 steps in my signature...
    1) Analyse the problem until you have a good understanding of what is required.
    2) Then and only then, begin planning a solution to the problem... researching as required.
    3) Now that you know what your writing... you can sit down and create code.
    4) Once the code is in a state where it's at least partially functional you should begin testing.

    But take hope... Thomas Edison, inventor of the lightbulb was once in a position to say: "No. I don't know how to make a lightbulb but I have discovered several ways not to make one." (Or very close) ... You are now in the forunate position of having disovered one of a great many ways not to write a computer program.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,590
    @ARod609:

    Your problem is to write a function on how to make change; your function does NOT do that.
    Do you know how to make change?
    If not, find someone to tell you how to do it.

    Then think about it and write down the steps to do it.
    Change the steps into a C program.

    TOTAL VALUE ENTERED: 1.88
    7 quarters
    1 dime
    0 nickels
    3 pennies
    The above coins add up to 1.88 you answer does NOT add up to the value inputted.

    Tim S.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    @Stahta01 I know I just dont know how to apply it in "C" lol... so I am lost using the call functions with the modular division (%) to get the remainder to break it down in cents. I thought it would be hot to be able to enter any number and get back the change...

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,590
    Quote Originally Posted by ARod609 View Post
    @Stahta01 I know I just dont know how to apply it in "C" lol... so I am lost using the call functions with the modular division (%) to get the remainder to break it down in cents. I thought it would be hot to be able to enter any number and get back the change...
    You C code implies you have no idea on how to make change!

    We are not a place that writes code for others; try to do some work.
    Write out the steps; post the steps.

    Tim S.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    @Stahta01
    I am just trying to learn "C" with the help of the board, I only post a question when I am stumped... Your jokes are funny haha, now I appreciate your advice on writing out the steps and thank you. Everything else is really un called for!
    Last edited by ARod609; 08-06-2011 at 08:37 PM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ARod609 View Post
    @Stahta01 I know I just dont know how to apply it in "C" lol... so I am lost using the call functions with the modular division (%) to get the remainder to break it down in cents. I thought it would be hot to be able to enter any number and get back the change...
    Screw the code... think about the problem... How does making change work?

    Lets say you owe someone $0.41 in change...
    How much of that can you make up in quarters? (1)
    Ok, so you give them a quarter.
    How much do you still owe them? ($0.16)
    How much of that amount can you make up in dimes? (1)
    Ok so you give them a dime.
    How much do you still owe them? ($0.06)
    How much of that can you make up in nickles? (1)
    Give them a nickel, how much is left? ($0.01)
    So you give them a penny....

    See the problem is you're trying to think about C code, when you haven't even understood the problem itself...
    Last edited by CommonTater; 08-06-2011 at 09:04 PM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ARod609 View Post
    @Stahta01
    I am just trying to learn "C" with the help of the board, I only post a question when I am stumped... Your jokes are funny haha, now I appreciate your advice on writing out the steps and thank you. Everything else is really un called for!
    You need to stop defending yourself from your friends and start listening to their advice...
    ARod609 likes this.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    Code:
    #include <stdio.h>
    
    void change(float, int *, int*, int*, int*);
    
    int main()
    {
    	float total;
    	int quarters, dimes, nickels, pennies;
        
        {
            total = 1.88;
            
            change(total, &quarters, &dimes, &nickels, &pennies);
            
            printf("TOTAL VALUE ENTERED: %.2f\n", total);
            printf("%9d quarters\n", quarters);
            printf("%9d dimes\n", dimes);
            printf("%9d nickels\n", nickels);
            printf("%9d pennies\n", pennies);
        
            total = 0.32;
            
            change(total, &quarters, &dimes, &nickels, &pennies);
            
            printf("TOTAL VALUE ENTERED: %.2f\n", total);
            printf("%9d quarters\n", quarters);
            printf("%9d dimes\n", dimes);
            printf("%9d nickels\n", nickels);
            printf("%9d pennies\n", pennies);
        
            printf("Enter the amount of money in your pocket:  \n");
            scanf("%f", &total);
            
            change(total, &quarters, &dimes, &nickels, &pennies);
            
            printf("TOTAL VALUE ENTERED: %.2f\n", total);
            printf("%9d quarters\n", quarters);
            printf("%9d dimes\n", dimes);
            printf("%9d nickels\n", nickels);
            printf("%9d pennies\n", pennies);
        }
        return 0;
    }
    void change(float total, int *quarters, int *dimes, int *nickels, int *pennies)
    {		 
        if(total >= 0.25)
            *quarters = (total/0.25);
        
        if(total >= 0.10)
            *dimes = (total - (*quarters * 0.25))/0.10;
        
        if(total >= 0.05)
            *nickels = (total - (*quarters * 0.25) - (*dimes * 0.10))/0.05;
        
        if(total >= 0.05)
            *pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05))/0.01;
        
    	return ;
    }
    this is just one of the ways I am trying it now i am going to try a "for" and a "while" statement with "%" division... wish me luck...
    Last edited by ARod609; 08-07-2011 at 11:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with solving this problem
    By Neox in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2010, 02:40 PM
  2. Need help in solving this problem
    By lokachari in forum C Programming
    Replies: 7
    Last Post: 10-27-2009, 07:32 PM
  3. Help solving this problem
    By marcelomdsc in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2008, 08:32 AM
  4. C++ Problem Solving
    By scojan in forum C++ Programming
    Replies: 16
    Last Post: 10-16-2007, 10:41 AM
  5. C++ problem solving, HELP....PLEASE
    By Dana in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2001, 04:05 PM

Tags for this Thread


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