43
votes
Accepted
Greedy.c works for all numbers except 4.2
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 ...
6
votes
How to approach greedy in problem set 1?
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 ...
6
votes
How to Fix the "expression result unused" Error?
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....
6
votes
Accepted
Can't cast a float to an int?
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 ...
6
votes
Accepted
pset1 - "greedy" failing at one value
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)...
5
votes
Accepted
Why does an input of 4.2 yield an output of 22 not 18?
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 ...
5
votes
Accepted
Convert from dollars to cents
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, ...
5
votes
Accepted
pset1 greedy checking problem
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 ...
5
votes
Greedy Compiling issue
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 ...
4
votes
Accepted
Pset1 Greedy: how to get result of how many coins
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 ...
4
votes
Calculating the number of required coins in Greedy
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), ...
4
votes
Accepted
Greedy's output is correct when I run it in the Terminal, but check50 shows different outputs
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
votes
Accepted
pset1 Greedy can't compute pennies
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 ...
4
votes
Accepted
How to fix the "expression result unused" compilation error?
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;
...
4
votes
Accepted
pset1.greedy did not use modulo
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 ...
3
votes
Greedy - what's did I do wrong with round()?
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 ...
3
votes
Accepted
Greedy Check50 Error
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-...
3
votes
Does greedy's Output Have to Match The One Required per the Pset Specification Page?
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 ...
3
votes
Why Does greedy Fail Passing check50 Tests?
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 ...
3
votes
Why don't I get the correct result when dividing numbers for greedy?
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, ...
3
votes
Accepted
Why Do I Get Errors When Checking greedy with check50?
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 ...
3
votes
Should the greedy algorithm recognize a 50 cent coin?
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
votes
Accepted
check50 rejects greedy even if the correct answer is displayed
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 ...
3
votes
Accepted
Greedy correctly calculates only for quarters and dimes
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 ...
3
votes
Accepted
I cannot get my greedy program to compile
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 ...
3
votes
Greedy hangs after prompting for user input
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 ...
3
votes
Accepted
if I have a better code that does more things than the one asked for, should I submit which one?
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 ...
3
votes
Modulo not working in Greedy
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
votes
Accepted
Modulo not working in Greedy
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
...
3
votes
Accepted
greedy check50 peculiarity
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
greedy × 282pset1 × 223
check50 × 18
cs50x × 16
cash × 12
round × 11
c × 9
modulo × 7
error × 6
output × 5
float × 5
compilation-error × 4
problem-set-1 × 4
pset6 × 3
loop × 3
variables × 3
dowhileloops × 3
python × 2
function × 2
for-loop × 2
gradebook × 2
printf × 2
cash.c × 2
do-while × 2
while-loop × 2