Thread: problems w/my simple function

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4

    problems w/my simple function

    Code:
    /*
    
    CS50 Problem Set 2
    
    This program asks for the user to specify how much change is due and then returns the smallest number of coins that could be combined to give the correct change.  Implements the "greedy algorithm."
    
    Project Started: 5/7/2009 10:20pm
    V1 Completed:
    Last Modified:
    
    */
    
    #include <stdio.h>
    
    int
    main(int argc, char *argv[]){
    
            int countCoints();
    
            float change;
            int coinsDue;
    
            printf("How much change is due?\n");
            scanf("%f", &change);
    
            coinsDue = countCoins(change);
    
            printf("%d", coinsDue);
    
    
    }
    
    int
    countCoins(float c){
    
            int input = (float) c;
    
            int numCoins = 0; //numCoins stores the current number of coins needed to give correct change
    
            float penniesLeft = (int) (input*100); //initializes to total change due in pennies to eliminate loss in floating point precision
    
            while(penniesLeft > 0){
    
                    if(penniesLeft >= 25){ //if quarters can be given
    
                            int numQuarters;
    
                            numQuarters = (int)(penniesLeft/25); //finds out how many quarters can be given
                            penniesLeft -= (numQuarters*25); //subtracts change given in quarters from change sti$
                            numCoins += numQuarters;
    
                    }
    
                    if(penniesLeft >= 10){ //if dimes can be given
    
                            int numDimes;
    
                            numDimes = (int)(penniesLeft/10); //finds out how many quarters can be given
                            penniesLeft -= (numQuarters*10); //subtracts change given in dimes from change still $
                            numCoins += numDimes;
    
                    }
    
            }
    
            return numCoins;
    }
    (line 34) error: conflicting types for ‘countCoins’
    (line 34) note: an argument type that has a default promotion can’t match an empty parameter name list declaration
    (line 26)error: previous implicit declaration of ‘countCoins’ was here
    In function ‘countCoins’:
    (line 59) error: ‘numQuarters’ undeclared (first use in this function)
    (line 59) error: (Each undeclared identifier is reported only once for each function it appears in.)

    hmmm I can't seem to find an instance of declaring countCoins() anything other than an integer...

    I don't know why the error on line 26 is an error;

    And numQuarters IS declared in the function!

    I'm new to C, and it's been a while since I've programmed anything other than PHP, so I'm probably missing something basic here... I've looked at tutorials for calling functions but none of them seem to suggest a solution to my errors.

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    You need a prototype declaration for function countCoins(float).
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by generalt View Post
    [code]


    I'm new to C, and it's been a while since I've programmed anything other than PHP, so I'm probably missing something basic here... I've looked at tutorials for calling functions but none of them seem to suggest a solution to my errors.
    You are declaring countCoints to take no arguments but while calling and defining you are actually passing arguments to the function. That's why the conflict in function declaration and definition.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Why even use float variables. You're using pennies to calculate the types of coins. You can't have a fraction of a penny. You'd also be better off not declaring your variables inside of "if" blocks. Declare them outside, before entering any type of block statement.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kcpilot View Post
    Why even use float variables. You're using pennies to calculate the types of coins. You can't have a fraction of a penny.
    Using floats for money is a bad idea -- it only seems like a good one because $5.95 has a decimal in it. However, money is NOT floating point, it's fixed precision (to two decimal places). "Rounding" a float into fixed precision is also a bad idea because of how computers actually store the numbers, qv.
    Floating Point
    Which is about Java, but the exact same logic applies to C. Note the explanation of why "0.1 + 0.1 + 0.1 MAY NOT EQUAL 0.3" (which is why you should not use == with floats either, but always <= or >= ).

    Anyway, with money DO NOT USE FLOATS.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4
    So, if I only read in 2 decimal places that would solve the problem of imprecision? In this program, I don't think I'm in trouble w/imprecision b/c I convert it into an integer... right?
    Last edited by generalt; 05-08-2009 at 08:03 AM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by generalt View Post
    So, if I only read in 2 decimal places that would solve the problem of imprecision? In this program, I don't think I'm in trouble w/imprecision b/c I convert it into an integer... right?
    How did you plan to "read in only 2 decimal places"? Nb. That the problem *is* with converting floats into integers. There is a very long thread here if you want to look at it:
    float13

    If you want a quick illustration, try this:
    Code:
    #include <stdio.h>
    
    int main () {
    	int X=1,i;
    	float x;
    	for (i=0;i<25;i++) {
    		x=(float)X/10;
    		printf("%d %f %d\t",X,x,i);
    		x+=0.1f;
    		X=x*10;
    		printf("%d %f \n",X,x);
    	}
    	return 0;			
    }
    On a 32-bit system, you will notice something strange happen starting at 13. On a 64-bit system, it starts at 21.

    Understand that binary floating point arithmetic is really something almost unique to computers, and not to confuse the concept with decimal numbers. You should deal with money as an integer (the number of cents), because that is what money is. Using a float is 1) inappropriate, 2) prone to error.
    Last edited by MK27; 05-08-2009 at 08:41 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4
    You should deal with money as an integer (the number of cents), because that is what money is.
    But how would I read in $0.41 if I didn't read it in as a float?! I'm guessing double has the same problems.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by generalt View Post
    But how would I read in $0.41 if I didn't read it in as a float?! I'm guessing double has the same problems.
    Ah. That's an issue! And some confusion is added by the fact that string functions like scanf allow you to read in a fixed precison number.

    For the input:
    Code:
    scanf("%d.%d",dollars,change);
    total_cents=dollars*100+change;
    For the output, something like:
    Code:
    void output (int total_cents) {
    	int dollars = total_cents/100; /* C rounds down by default */
    	int change = total_cents%100;  /* modulus */
    	printf("$%d.%d",dollars,change);
    }


    Which ain't hard either. The point is don't use f
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4
    Ah, thanks. I didn't realize that you could split the decimal like that. It makes a lot more sense now.

    And how would you do that with fgets()? I've been messing around with it for a little bit and can't figure out how to store each side in a separate variable (using fgets)

    EDIT: nevermind, it just occurred to me to look up how to parse a string.
    Last edited by generalt; 05-08-2009 at 10:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Big problems with the Text function
    By GaPe in forum C Programming
    Replies: 26
    Last Post: 05-22-2002, 10:42 AM