Adam 'Sacki' Sackfield - 7 months ago 74

C Question

I have been Googling this for days now and I am lost. So doing CS50 online and can't seem to get a handle on this rounding of numbers. My program is messing up multiplying floats like **2.10** with integers like **100** it would output **209.xxxxxxxx**

Now like I say I have read countless posts on that I should use **ceilf** and include but I am getting an error

greedy.c:(.text+0x74): undefined reference to `ceilf'

collect2: error: ld returned 1 exit status make: *** [greedy] Error 1

adam@beethoven:~/projects/atom/edx/pSet1/greedy$

I have seen the posts about -lm and a certain file but if I am honest I don't understand what it means.

I am in no way looking for an outright solution, just guidance in improving.

Here is my code, probably not as streamlined as some would like but I am back to basics here ;)

`#include <stdio.h>`

#include <math.h>

int main() {

// Initialize Variables

int coinsTotal = 0,

quarter = 25,

dime = 10,

nickel = 5,

penny = 1,

cents;

float changeDue;

do {

printf("How much change are you owed? (Format = 0.00)($): ");

scanf("%f", &changeDue );

// Convert to cents

cents = changeDue * 100;

} while(cents <= 0);

while (cents >= quarter) {

cents = cents - quarter;

coinsTotal = coinsTotal + 1;

} if (cents == 0) {

printf("The miminum number of coins is: %d\n", coinsTotal);

} else {

while (cents >= dime) {

cents - dime;

coinsTotal = coinsTotal + 1;

} if (cents == 0) {

printf("The minimum number of coins is: %d\n", coinsTotal);

} else {

while (cents >= nickel) {

cents = cents - nickel;

coinsTotal = coinsTotal + 1;

} if (cents == 0) {

printf("The minimum number of coins is: %d\n", coinsTotal);

} else {

while (cents >= penny) {

cents = cents - penny;

coinsTotal = coinsTotal + 1;

} if (cents == 0) {

printf("The minimum number of coins is: %d\n", coinsTotal);

}

}

}

}

}

Basically it should work out the minimum number of coins needed to make a given amount. It works in most cases until the floats mess up. Excuse the notes I like to write what I did so I learn better.

Update Tried to compile with GCC using -lm but still failed.

adam@beethoven:~/projects/atom/edx/pSet1/greedy$ gcc -o foo -lm

greedy.c /tmp/cc3qHAK7.o: In functionceilf' collect2: error: ld returned 1 exit`main': greedy.c:(.text+0x6e):`

undefined reference to

status adam@beethoven:~/projects/atom/edx/pSet1/greedy$

SOLUTION Instead of using themakecommand I used gcc and added

the -lm flagAt the end of the command`gcc -o foo greedy.c -lm`

Answer

I have seen the posts about -lm and a certain file but if I am honest I don't understand what it means.

You have to link to the math library to fix the error. Math functions implementations are usually put as a separate library, the math library. If you use `gcc`

add `-lm`

to the linker command.