Help, home work code provided. Totally Lost.

This is a discussion on Help, home work code provided. Totally Lost. within the C Programming forums, part of the General Programming Boards category; Okay first of all thank you for looking at this I know HW questions can be pain staking. Here is ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    Help, home work code provided. Totally Lost.

    Okay first of all thank you for looking at this I know HW questions can be pain staking.

    Here is the question posted and i did searches in the forum and with no results to help.

    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


    Here is what I have.
    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
        float quarters, nickels, dimes, pennies, total;
        int final;
        char stopprogram;
        
        void change (quarters, nickels, dimes, pennies, total);
        total=1.88;
        printf("The Total Entered: %f\n",total);
        printf("%d quarters\n", quarters);
        printf("%d nickels\n", nickels);
        printf("%d dimes\n", dimes);
        printf("%d pennies\n\n", pennies);
        
        void change (quarters, nickels, dimes, pennies, total);
        total=.32;
        printf("The Total Entered: %f\n",total);
        printf("%d quarters\n", quarters);
        printf("%d nickels\n", nickels);
        printf("%d dimes\n", dimes);
        printf("%d pennies\n\n", pennies);
        
        
        printf("Please enter a monetary value");
        scanf("%f", &total);
        void change (quarters, nickels, dimes, pennies, total);
        printf("The Total Entered: %f\n",total);
        printf("%d quarters\n", quarters);
        printf("%d nickels\n", nickels);
        printf("%d dimes\n", dimes);
        printf("%d pennies\n", pennies);
        scanf("%c", &stopprogram);
        
        return 0;
    }
    float change (*quarters, *nickels, *dimes, *pennies, *total);
    {
          valuequarter=.25
          valuedime=.10
          valuenickel=.05
          valuepenny=.01
          
          *quarters= truncf(*total/valuequarter);
          *total=*total-*quarters;
          *dimes=truncf(*total/valuedime);
          *total=*total-*dimes;
          *nickels=truncf(*total/valuenickel);
          *total=*total-*nickels;
          *pennies=truncf(*total/valuepenny);
          *total=*total-*pennies;
          
          return 0;
          
    }
    Last edited by 1BadRbt; 11-30-2006 at 09:03 PM.

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    More Information

    compiler errors.

    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp: In function `int main()':
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:9: error: variable or field `change' declared void
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:9: error: initializer expression list treated as compound expression
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:9: warning: converting to `int' from `float'

    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:17: error: variable or field `change' declared void
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:17: error: redeclaration of `int change'
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:9: error: `int change' previously declared here
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:28: error: variable or field `change' declared void
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:28: error: redeclaration of `int change'
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:9: error: `int change' previously declared here
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp: At global scope:
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: `quarters' was not declared in this scope
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: `nickels' was not declared in this scope
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: `dimes' was not declared in this scope
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: `pennies' was not declared in this scope
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: `total' was not declared in this scope
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:38: error: initializer expression list treated as compound expression
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:39: error: expected unqualified-id before '{' token
    C:\Documents and Settings\Todd\Desktop\School Assignments\C programming\Change.cpp:39: error: expected `,' or `;' before '{' token

    Execution terminated

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You got a lot of nasty stuff in there, dude. Here's what I found.

    1) You prototyped your function saying that it returns void, but then you make it return float. Bad news.
    2) You tell the compiler that change should accept pennies, quarters and such without explaining what these things are. (You probably actually want something like :
    float change(float *pennies, float* quarters, etc...)
    3) You prototype your change function a whopping 3 times ! Only one is necessary, and it is sufficient to declare it outside main (which is good coding practice as well)
    4) You don't send total to your change function. So how is it supposed to know what to do when you say *total ?
    5) You forgot semi-colons after all those valuepennies = 0.01, valuequarters = 0.25 and such.

    There is more. But fix those first.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    You also pass the objects rather than the addresses to the function change(). Oh, and you defined change() twice... Oh, and you have an extra semi-colon after the second declaration of change(). While we're at it, your change() function *always* returns 0... you can make that a void function and never return anything. There's still a lot more to correct but fix all these for now.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    Quote Originally Posted by Happy_Reaper
    You got a lot of nasty stuff in there, dude. Here's what I found.

    1) You prototyped your function saying that it returns void, but then you make it return float. Bad news.
    2) You tell the compiler that change should accept pennies, quarters and such without explaining what these things are. (You probably actually want something like :
    float change(float *pennies, float* quarters, etc...)
    3) You prototype your change function a whopping 3 times ! Only one is necessary, and it is sufficient to declare it outside main (which is good coding practice as well)
    4) You don't send total to your change function. So how is it supposed to know what to do when you say *total ?
    5) You forgot semi-colons after all those valuepennies = 0.01, valuequarters = 0.25 and such.

    There is more. But fix those first.
    #1 Changed them all to float
    #2 changed to your example
    #3 I thought by it making a first call second call third call i had to direct to the function redeclaring the total values
    #4 not sure what you are saying the *total was to be a pointer to the total value
    #5 fixed that please let me know what else here is updated code I changed the names of the *quarters *dimes and so on.

    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
        float quarters, nickels, dimes, pennies, total;
        char stopprogram;
        
        float change (quarters, dimes, nickels, pennies, total);
        total=1.88;
        printf("The Total Entered: %f\n",total);
        printf("%d quarters\n", quarters);
        printf("%d nickels\n", nickels);
        printf("%d dimes\n", dimes);
        printf("%d pennies\n\n", pennies);
        
        float change (quarters, dimes, nickels, pennies, total);
        total=.32;
        printf("The Total Entered: %f\n",total);
        printf("%d quarters\n", quarters);
        printf("%d nickels\n", nickels);
        printf("%d dimes\n", dimes);
        printf("%d pennies\n\n", pennies);
        
        
        printf("Please enter a monetary value");
        scanf("%f", &total);
        float change (quarters, dimes, nickels, pennies, total);
        printf("The Total Entered: %f\n",total);
        printf("%d quarters\n", quarters);
        printf("%d nickels\n", nickels);
        printf("%d dimes\n", dimes);
        printf("%d pennies\n", pennies);
        scanf("%c", &stopprogram);
        
        return 0;
    }
    float change (float *aq,float *bd,float *cn,float *dp,float *total);
    {
          valuequarter=.25;
          valuedime=.10;
          valuenickel=.05;
          valuepenny=.01;
          
          *aq= truncf(*total/valuequarter);
          *total=*total-*aq;
          *bd=truncf(*total/valuedime);
          *total=*total-*bd;
          *cn=truncf(*total/valuenickel);
          *total=*total-*cn;
          *dp=truncf(*total/valuepenny);
          *total=*total-*dp;
                   
    }

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, now you want to send the adresses of your variables to your functions.

    And again, you don't have to prototype 3 times. One time will suffice.

    ex :

    Code:
    float foo(float * bar);
    
    int main(void)
    {
         float b;
         b = 0.6;
         foo(&b);
         return 0;
    }
    After you've prototyped it, you can just call it by its name any time you want.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    Okay so I updated it :)

    Here is a new program more organized.

    Let me know where I went wrong I may not be sending the info to the change function I am unsure where my information breakdown is.

    Code:
    #include <stdio.h>
    #include <math.h>
    #define quarter 25
    #define dime 10
    #define nickel 5
    #define penny 1
    
    float change(float *total);
    
    int main()
    {
       float total;
       total=1.88;
       printf("The Total Entered: %4.2f\n\n",total);
       total=total*100;
       change(&total);
       
       total=0.32;
       printf("The Total Entered: %4.2f\n\n",total);
       total=total*100;
       change(&total);
       
       printf("Please enter a monetary value: ");
       scanf("%f",&total);
       printf("The Total Entered: %8.2f\n\n",total);
       total=total*100;
       change(&total);
       
       return 0;
    }
    float change(float *total)
    {
         float quarters, dimes, nickels, pennies;
         float amt;
         quarters=truncf(amt/quarter);
         amt=amt-(quarters*quarter);
         dimes=truncf(amt/dime);
         amt=amt-(dimes*dime);
         nickels=truncf(amt/nickel);
         amt=amt-(nickels*nickel);
         pennies=truncf(amt/penny);
         amt=amt-(pennies*penny);
         
         printf("%d quarters\n",quarters);
         printf("%d dimes\n",dimes);
         printf("%d nickels\n",nickels);
         printf("%d pennies\n\n",pennies);
         
         }
    Last edited by 1BadRbt; 12-03-2006 at 01:17 PM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    truncf() is C99. Could this be the problem.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    the question wants the values to be sent to this function by address
    Code:
    float change(float *total);
    should be

    Code:
    float change(int *quarters, int *nickels, int *dimes, int *pennies, float total);
    total doesn't have to be a pointer...we're not changing it in change

    now in main

    Code:
       int quarters, nickels, dimes, pennies;
    
    ...
    ...
    ...
       change(&quarters, &nickels, &dimes, &pennies, total);
    
       printf("%d quarters\n", quarters);
       printf("%d dimes\n", dimes);
       printf("%d nickels\n", nickels);
       printf("%d pennies\n\n", pennies);

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    Mabe this will help to geter done.
    Code:
    void change(float *total)
    {
    	int quarters, dimes, nickels, pennies;
    	int amt = (int) *total;
    	quarters = amt / quarter;
    	amt -= quarters * quarter;
    	dimes = amt / dime;
    	amt -= dimes * dime;
    	nickels = amt / nickel;
    	amt -= nickels * nickel;
    	pennies =  amt / penny;
    	amt -= pennies * penny;
    	
    	printf("%d quarters\n",quarters);
    	printf("%d dimes\n",dimes);
    	printf("%d nickels\n",nickels);
    	printf("%d pennies\n\n",pennies);
    }

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    here is the final code that worked. I appreciate the help. You guys got a good place to learn pointers?

    if i understand in the prototype of the function and the actual header line i am pointing the *total to total and sending it to the change program to be manipulated.
    if i am correct maybe an explanation i got kind of lost on whree info was going as I wrote it.

    I do PLC and programming and Robotics and C is a lot different than what i use .

    Code:
    #include <stdio.h>
    #include <math.h>
    #define quarter 25
    #define dime 10
    #define nickel 5
    #define penny 1
    
    void change(float *total);
    
    int main()
    {
       float total;
       total=1.88;
       printf("The Total Entered: %4.2f\n\n",total);
       total=total*100;
       change(&total);
       
       total=0.32;
       printf("The Total Entered: %4.2f\n\n",total);
       total=total*100;
       change(&total);
       
       printf("Please enter a monetary value: ");
       scanf("%f",&total);
       printf("The Total Entered: %8.2f\n\n",total);
       total=total*100;
       change(&total);
       
       return 0;
    }
    void change(float *total)
    {
    	int quarters, dimes, nickels, pennies;
    	int amt = (int) *total;
    	quarters = amt / quarter;
    	amt -= quarters * quarter;
    	dimes = amt / dime;
    	amt -= dimes * dime;
    	nickels = amt / nickel;
    	amt -= nickels * nickel;
    	pennies =  amt / penny;
    	amt -= pennies * penny;
    	
    	printf("%d quarters\n",quarters);
    	printf("%d dimes\n",dimes);
    	printf("%d nickels\n",nickels);
    	printf("%d pennies\n\n",pennies);
    }

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    total does not have to be sent as a pointer since it is not being changed by the change function, then used by the main function with the new value...if you read my post you'll see the correct way to impliment the change function, based on the specifications you provided.

    when implimented this way, the variable amt will also not be needed...since total can just be used.

    [edit]
    looking at it again, it should be void change, not float change, since your not returning and don't need to return a value
    Last edited by sl4nted; 12-03-2006 at 04:02 PM.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by 1BadRbt
    here is the final code that worked.
    Since you're done, here is something similar to look at.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    I think when you copy and paste that you don't learn one single thing.

  15. #15
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Which is why Dave provided it after he was done.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

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

Similar Threads

  1. home work
    By manav in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-29-2008, 03:39 PM
  2. Using wrapping code icon doesn't work?
    By Aia in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-03-2008, 06:15 AM
  3. Replies: 6
    Last Post: 10-11-2004, 11:43 AM
  4. Reference Books at Work and Home
    By kuphryn in forum Tech Board
    Replies: 2
    Last Post: 05-20-2004, 06:59 AM

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