# problems w/my simple function

• 05-08-2009
generalt
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.
• 05-08-2009
stevesmithx
You need a prototype declaration for function countCoins(float).
• 05-08-2009
BEN10
Quote:

Originally Posted by generalt
[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.
• 05-08-2009
kcpilot
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.
• 05-08-2009
MK27
Quote:

Originally Posted by kcpilot
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.
• 05-08-2009
generalt
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?
• 05-08-2009
MK27
Quote:

Originally Posted by generalt
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:
http://cboard.cprogramming.com/c-pro...float13-2.html

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.
• 05-08-2009
generalt
Quote:

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.
• 05-08-2009
MK27
Quote:

Originally Posted by generalt
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
• 05-08-2009
generalt
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.