Thread: Change function

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    6

    Change function

    Below is the the assignment. Now I wrote some code I thought would work, but recieving so many errors that I can't understand, I reduced the code to just find the amount of quarters needed. Once I can clear that up, I should able to finalize the assignment. I think I am not adressing quarters properly. I have searched the forums and read the text required for the class and other tuturiols.

    1. 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

    Code:
    #include <stdio.h>
    int change (float, int*);  /* function prototype */
    int main()
    {
    	int *quarters;
    	float total;
    	
            total = 1.88;
            
            change(total, quarters);
            
            printf("TOTAL VALUE ENTERED: $%6.2f\n", total);
            printf("%d quarters", quarters);
            
       
        return 0;
    }
    int change(float total, int *quarters)
    {		 
            if( total >= 0.25 )
            	*quarters = (total/0.25);
            	
    	return;
    }
    Here is the window pop-up error I get.
    Run-Time Check Failure #3 - The variable 'quarters' is being used without being initialized.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There are surprisingly many problems with this code.

    1) Don't use int main() in C use either, int main(void) or int main(int argc, char *argv[]) if you need to use command line arguments.

    2) Function change is supposed to return an int value yet it doesn't.

    3) Avoid doing floating point arithmetic division and assigning the result to an integer.

    4) I don't understand why you are complicating this for no reason and using pointers, when you clearly don't know to use them, and don't design your function change to return a value that you can grab in another variable in main:

    chg = change(total);

    5) If you DO use pointers don't printf the address the pointer change is pointing to but rather the value contained at that address.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    To elaborate on what claudiu said about the pointers, your pointer is not pointing to anything, so when you try to change its value you are resulting in undefined behavior. That is what your compiler is warning you about, as you asked.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    6

    Updated code/ with one problem

    Ok so I finally got my head almost wrapped around this. The last computation for the pennies doesn't produce the correct results. I know the problem has to do with the function but not sure why. Any ideas?
    Code:
    #include <stdio.h>
    int change (float total, int *quarters, int *dimes, int *nickels, int *pennies);  /* function prototype */
    int main()
    {
     int quarters, dimes, nickels, pennies;
     float total;
     {
    	total = 1.88;
            
        change (total, &quarters, &dimes, &nickels, &pennies);
            printf("TOTAL VALUE ENTERED: $%6.2f", total);
            printf("\n%d quarters\n", quarters);
            printf("\n%3d dimes\n", dimes);
            printf("\n%3d nickels\n", nickels);
            printf("\n%3d pennies\n", pennies);
     }
     {
    	total=0.32; 
               
        change(total, &quarters, &dimes, &nickels, &pennies); 
    		printf("\nTOTAL VALUE ENTERED: %6.2f",total); 
            printf("\n%d quarters\n",quarters); 
            printf("\n%d dimes\n", dimes); 
            printf("\n%d nickles\n", nickels); 
            printf("\n%d pennies\n",pennies); 
     }      
     {           
        printf("\nPlease enter an amount of money: \n"); 
        scanf("%f", &total); 
               
        change(total, &quarters, &dimes, &nickels, &pennies); 
            printf("TOTAL VALUE ENTERED: %6.2f",total); 
    		printf("\n%d quarters\n",quarters); 
            printf("\n%d dimes\n",dimes); 
            printf("\n%d nickles\n",nickels); 
            printf("\n%d pennies\n",pennies); 
     }    
    
       return 0;
    }
    int 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.04 >0)
          *pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05))/0.01;
    		
    	return;

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What is the meaning of "if( total <= 0.04 >0)"?

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by cody.carter View Post
    Ok so I finally got my head almost wrapped around this. The last computation for the pennies doesn't produce the correct results. I know the problem has to do with the function but not sure why. Any ideas?
    Code:
    #include <stdio.h>
    int change (float total, int *quarters, int *dimes, int *nickels, int *pennies);  /* function prototype */
    int main()
    {
     int quarters, dimes, nickels, pennies;
     float total;
     {
    	total = 1.88;
            
        change (total, &quarters, &dimes, &nickels, &pennies);
            printf("TOTAL VALUE ENTERED: $%6.2f", total);
            printf("\n%d quarters\n", quarters);
            printf("\n%3d dimes\n", dimes);
            printf("\n%3d nickels\n", nickels);
            printf("\n%3d pennies\n", pennies);
     }
     {
    	total=0.32; 
               
        change(total, &quarters, &dimes, &nickels, &pennies); 
    		printf("\nTOTAL VALUE ENTERED: %6.2f",total); 
            printf("\n%d quarters\n",quarters); 
            printf("\n%d dimes\n", dimes); 
            printf("\n%d nickles\n", nickels); 
            printf("\n%d pennies\n",pennies); 
     }      
     {           
        printf("\nPlease enter an amount of money: \n"); 
        scanf("%f", &total); 
               
        change(total, &quarters, &dimes, &nickels, &pennies); 
            printf("TOTAL VALUE ENTERED: %6.2f",total); 
    		printf("\n%d quarters\n",quarters); 
            printf("\n%d dimes\n",dimes); 
            printf("\n%d nickles\n",nickels); 
            printf("\n%d pennies\n",pennies); 
     }    
    
       return 0;
    }
    int 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.04 >0)
          *pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05))/0.01;
    		
    	return;
    If you don't follow the advice posted here why do you insist asking for answers to your questions?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    6
    Quote Originally Posted by claudiu View Post
    If you don't follow the advice posted here why do you insist asking for answers to your questions?
    If you don't want to answers why do you insist on instigating conflict.

    I am student just trying to follow the assignment I was given. While your previous advice was somewhat helpful, most of it was and still is over my head right now. So maybe you could bring you advice down to a beginners level.

    Thank you.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by claudiu View Post
    There are surprisingly many problems with this code.

    1) Don't use int main() in C use either, int main(void) or int main(int argc, char *argv[]) if you need to use command line arguments.

    2) Function change is supposed to return an int value yet it doesn't.

    3) Avoid doing floating point arithmetic division and assigning the result to an integer.

    4) I don't understand why you are complicating this for no reason and using pointers, when you clearly don't know to use them, and don't design your function change to return a value that you can grab in another variable in main:

    chg = change(total);

    5) If you DO use pointers don't printf the address the pointer change is pointing to but rather the value contained at that address.
    What is over your level in this? Perhaps you can elaborate.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    6
    Quote Originally Posted by tabstop View Post
    What is the meaning of "if( total <= 0.04 >0)"?
    Is the total less than or equal to 4 or greater than 0. But as I was typing I realized it should say greater than or equal 4 like below.

    "if (total >= 0.04)", but this returned one penny less than what the results should be. Back to the drawing board.

    Thanks for the prompt.

  10. #10
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Its funny how from time to time we get 40 people in here all asking about how to create a function to generate change, do airline ticketing/routing, etc and all at the same time. I just left a thread in the C++ forum asking the same exact question and I swear there was someone else asking this about two days ago...and Cody if you feel put-upon, remember that we are human too and sometimes we have bad weeks or whatever and get tired of answering the same exact questions over and over. If you need some simpler logic to solving this, look at what I posted in the C++ forum within the past 24 hours. The solution is basic C even though it was asked in that area...not that it is a superior solution, just that it is simpler than what you posted here (and thus easier to understand) and it has the benefit of working...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    6
    Quote Originally Posted by claudiu View Post
    What is over your level in this? Perhaps you can elaborate.
    I'm still not quite familar will all the terms and functions, yet.

    Also the assignment call for pointers and you suggested not to use them.

    How do you avoid doing floating point arithmetic division and assigning the result to an integer, when the assignment calls for it?

    Just trying to learn, dude.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cody.carter View Post
    I'm still not quite familar will all the terms and functions, yet.

    Also the assignment call for pointers and you suggested not to use them.

    How do you avoid doing floating point arithmetic division and assigning the result to an integer, when the assignment calls for it?

    Just trying to learn, dude.
    There is no floating point arithmetic division involved in monetary transactions, as we have tried (and apparently failed) to point out. There are only whole numbers of pennies.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    6
    Quote Originally Posted by jeffcobb View Post
    Its funny how from time to time we get 40 people in here all asking about how to create a function to generate change, do airline ticketing/routing, etc and all at the same time. I just left a thread in the C++ forum asking the same exact question and I swear there was someone else asking this about two days ago...and Cody if you feel put-upon, remember that we are human too and sometimes we have bad weeks or whatever and get tired of answering the same exact questions over and over. If you need some simpler logic to solving this, look at what I posted in the C++ forum within the past 24 hours. The solution is basic C even though it was asked in that area...not that it is a superior solution, just that it is simpler than what you posted here (and thus easier to understand) and it has the benefit of working...
    Thanks, its just a struggle to learn the terminology, plus it seems the book is written greek. It's phenominal what you guys do with this stuff. Once, I make it through this class, I'll leave this stuff for the pros. I rather be back in Afghanistan than to be doing this, but its a degree requirement. Thanks for putting up with students like myself.

  14. #14
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I would do this something like this, to avoid floating points. Not sure if you still need help, but this might make things clearer if you are still stuck.

    First, convert the change you are given to pennies (ie, an integer) by multiplying by 100 and just casting it to integer. Ie, if you are given 1.88, change it to 188.

    To get the number of quarters, do the amount / 25

    ie, 188/25 = 7

    to get the remaining amount of change, do 188%25 = 13

    to get the number of dimes, take the remainder and div by 10

    13/10 = 1

    to get the remainder, 13%10 = 3

    repeat for nickels and pennies.

    This should be a 10 line program, really, I think you are just overcomplicating the problem by using pointers and such.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    13
    Quote Originally Posted by KBriggs View Post
    This should be a 10 line program, really, I think you are just overcomplicating the problem by using pointers and such.
    It's not necessarily overcomplicated if he has to use a function to calculate the change. In that case, it would best to use a function that returns all four numbers at once rather than four functions to return the number of each coin separately.

    So, Cody, do you have to use functions (other than main) in your program?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM