# Tag Info

43

First of all, kudos for debugging your program and identifying the problem as having to do with multiplying your input. Let's take a look at that part of your code step by step, to illustrate what's going wrong. change = GetFloat(); At this point, we're going to enter 4.2; let's make sure we see very precisely what number gets stored by printing out the ...

6

You are given a value of dollars and cents. You must calculate the least number of coins that can be used to make that value. The coins available are worth 25 cents (quarter), 10 cents (dime), 5 cents (nickel), and 1 cent (penny). Let's say you are asked for 1.32 1.32 is 132 cents. So how many coins would you need? To get the least number of coins, you ...

6

Your code isn't working for the same reason that this code: int x = 5; x + 5; printf("%i", x); does not print the number 10. In C, (int) is a type casting operator; much like the + operator, it does not modify a variable; it creates an expression, which is evaluated and replaced by its result. So the above code is equivalent to: int x = 5; 10; printf("%...

6

This is a very common problem and the whole point of greedy.c, so let me try and give the definitive answer. Float values with a decimal part (to the right of the decimal, in case you weren't certain) rarely store with exact precision. They have to be converted from base 10 to base 2. Since there is a limit on the number of digits that can be used, it is ...

5

You should use the round() function to round your float to the nearest int! Casting a float to an int truncates all the digits after the decimal point. Also, 4.2 isn't necessarily stored as 4.2 because floats are not precise.

5

Google search of expression result unused will lead to 2 links : This It seems that you have written the number in a wrong way. Have a look at it, you are not supposed to write commas in the numbers. i.e. int i = 50,000 is wrong way and should be written as int i = 50000. Remove any such commas from your program. This Although the link is of for loop but ...

5

True that check50 is strict about the output. Here is what the instructions say Incidentally, so that we can automate some tests of your code, we ask that your program’s last line of output be only the minimum number of coins possible: an integer followed by \n. The problem in your output is not the newline, which is correct. It is the space between ...

5

You have multiple issues. Firstly, your linker errors undefined reference to ... means the linker can't find the functions you are using. In order to tell clang to link with the cs50 library for get_float you need the -lcs50 option. In order to use the math library functions (such as round) you need the -lm option. Instead of calling clang -lcs50 -lm ...

4

to track how many times a loop has run, you would create a variable outside the loop (e.g. called counter, then inside the loop, after it is done its other operations, increment the counter with counter++ so that every time it goes through the loop, it also increments the counter.

4

The Problem Greedy is a coin change making problem, in which we are supposed to tell the minimum number of coins that add to make a certain value. The available coins are quarters(25), dimes(10), nickels(5), and pennies(1). For example, if a change of 30 is to be made, then 30 = 25 + 5 // this is what we need as minimum number of coins are required for ...

4

You are running greedy1 but checking greedy.c Make sure your greedy.c is the most up to date version of your code. It appears from this that it is not.

4

One of the goals of this pset is to teach some of the issues with computer storage of numbers. One of the biggest issues is that when a number is stored as a float, it may not be stored exactly as what you expect. For instance, 4.2 may be stored as 4.1999999978 or something similar. Another number, say 3.31 may be stored as 3.310000000002. (These are, of ...

4

It's because you did a calculation and didn't do anything with it. You have cents - 25; Where are you putting the result? All of these would work: int change = cents - 25; cents = cents - 25; cents -= 25; The infinite loop is another story. To fix that loop, you'd use either of the latter two. If this answers your question, please accept this answer to ...

4

The main problem is how you are storing the initial retrieved value. You are calling GetFloat() and storing the result in owed, a variable of type int. GetFloat() returns a floating point number, which will be implicitly cast as an int when you try to store it in owed. In the process, the decimal point portion will be truncated. That means that you could ...

4

In programming, there are often several ways to do the same task. If they work, they are "correct". But now, you need to start thinking about efficiency. In these assignments, the efficiency is generally unimportant because they're simple tasks that run quickly no matter how they're programmed. What happens when you write a program that has to execute ...

3

No need to have a condition before the loop since the loop itself has a continuation condition // the loop is enough while (owed < 0) { // the code here is executed while owed < 0 } Use %d or %i to print ints not %a printf("%d\n", amountInCents);

3

The Greedy pset says: Using GetFloat alone will ensure that the user’s input is indeed a floating-point (or integral) value but not that it is non-negative. If the user fails to provide a non-negative value, your program should re-prompt the user for a valid amount again and again until the user complies. [my emphasis] The check50 error you are receiving:...

3

As it says in the Problem Set 1 spec: Incidentally, so that we can automate some tests of your code, we ask that your program’s last line of output be only the minimum number of coins possible: an integer followed by \n. Consider the below representative of how your own program should behave; highlighted in bold is some user’s input. jharvard@appliance (~...

3

check50 is a program like any other program. Computers are not smart enough to understand things (unless we, programmers, teach them to do so). Unfortunately, check50 is not yet taught to understand every single possible different output. That's why it's required per the pset specification page that your output must match a specific sample in order for ...

3

Why do you think n should be 23? if x = 9.72 then n = 972 so 972 % 25 = 22 edit to add given that the original question is now edited When you are dealing in floats, you run into imprecision, because a floating point number is only an approximation. 9.73 cannot be stored in binary exactly. It's more like 9.729999... so when you multiply by 100, you ...

3

The correct answer for 1.6 is 7. 1.6 is \$1.60 which is 160 cents. Given the available coins of 25c, 10c, 5c, and 1c, the smallest number is 6 quarters (150 cents) and 1 dime (10 cents) to equal 160 cents. Looks like you are ignoring the dollars? You need to include them. Brenda.

3

You should only use quarters, dimes, nickles and pennies. If the input is 0.5, your program should output 2 since 50 cents contain 2 quarters.

3

Be sure you have put a new line at the end of your answer, so it matches the staff version exactly. As it says in the spec: "Incidentally, so that we can automate some tests of your code, we ask that your program’s last line of output be only the minimum number of coins possible: an integer followed by \n. "

3

I think your program has a lot more problems than you realize. It is unnecessarily more complicated than it should be. First of all, I couldn't compile the code you've posted in my appliance until I initialized a,b,c,d to 0. It took me a lot of time to understand the code, and I'm still not sure if I understood it correctly. The comments would've helped me,...

3

The function GetFloat from the CS50 library takes no arguments. The way you call it is as follows GetFloat(); // NOTICE the empty parentheses If you wanna assign the result returned by GetFloat to a float variable, you may do it that way float f = GetFloat(); If you're curious about why the compiler gives you this error message, this is because a name of ...

3

I keep getting hung up right after the prompt for user input. This is a clear indication that there is an infinite loop (at least once). The problem is that you have 4 infinite loops, the four while loops. You write that while (cents >= some_constant) { //your loop's inner stuff } But you don't change the value of cents, so this condition always ...

3

In general, a good software engineering tip is to have your software doing exactly what it is supposed to do (no more and no less). This tip is enforced by check50. That is, if your program execution does not exactly match the one in the pset specification page, it will fail the tests.

3

Modulo Operator ( % ) requires both the operands to be of type int . In your code change is a floating point variable. Use an integer variable to store the rounded value.

3

If you tried typecasting to int, still the error would have existed because you are still storing the value in a float. So its basically like, original value = 12.27 round()'s output = 1227.00 Typecasted result = 1227 Storing the typecasted value back in a float variable = 1227.00 And modulo operations don't work with floating point numbers. So, its ...

3

Well, you're not exactly, precisely following the spec. Your program is really working correctly, but there's one minor problem. CHECK50 is looking for very exact responses. For instance, if you include, oh, let's say, an extra space in your output between the number of coins and the \n, it will choke on it. If this solves your problem, please mark your ...

Only top voted, non community-wiki answers of a minimum length are eligible