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 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
Accepted
Why greedy.c shows unexpected result due to float variable?
I'd suggest you review the Week 1 lecture, particularly the part about float imprecision. (you can see the notes for week 1. Also, the problem walkthrough (and hints) mention that should convert the ...
3
votes
Accepted
Pset1 Greedy Help Please
It's exactly the issue of lack of rounding.
If x is 4.2, for example, x*100 is 419 because 4.2 can't be expressed exactly in binary. It's actually 4.1999999nnnn. When you then multiply by 100 and ...
3
votes
Accepted
Problem Set 1: Greedy and While Loops
while (change >= 25)
;
means "while change is greater than or equal to 25, do nothing". So your programme sits there repeatedly doing nothing, in an infinite loop.
The semicolon ; is the ...
2
votes
pset1.greedy did not use modulo
I think it's fine, there's often lots of ways to accomplish the same task in code!
As a note, is your big do...while loop necessary? When would it ever run more than once?
2
votes
Accepted
pset1 Greedy Algorithm
Well, you have a couple of issues. I'm assunimg this is incomplete code and that you're just testing for quarters at this point, which is a good approach.
First look at the following:
...
2
votes
Accepted
How to fix Error: unused variable (Pset 1 - Greedy)
This is a visibility problem, amount only exists inside the loop, the solution is to declare amount hole of the loop:
float amount;
And then inside the loop:
amount = get_float;
2
votes
Accepted
greedy.py returning integer variable ending with .0 even w/ conversion from float to int() & round()
First a couple of stylistic problems I noticed. Per PEP8, which is the style guide for Python, and more specifically the BlankLines part, you should:
Surround top-level function and class definitions ...
2
votes
Accepted
Problem with CS50 greedy.c. All numbers work except $23 and $4.2
printf("%x\n", count);
prints count in hexadecimal. 0x12 is 18 in decimal. Use %i or %d instead of %x.
2
votes
Accepted
imprecision problem cs50 greedy
You need to round the result of change * 100. You can do that simply with the round function found in math.h.
For example, round(4.2*100) would return 420, where 4.2*100 gives 419.
2
votes
cs50 pset1 greedy (expect 4 coins for 0.41)
One of the hidden lessons in the first few psets is to write the code EXACTLY to the specification, not close, and not what you think is better. This is extremely important in team programming when ...
2
votes
Accepted
How to return a value from a loop?
I think you have a misconception of how return works. A return statement acts like a short-circuit. It causes the currently executing function, or main, to immediately terminate processing and return ...
1
vote
Accepted
Greedy - Do While Negative Float
Well, let's think about what's happening. The code is changeOwed = get_float(); which is supposed to get a float and store it in changeOwed. However, changeOwed is declared as an int, not a float, ...
1
vote
Accepted
pset1 - greedy not working for all values
You have to round off your money after storing and multiplying with 100. See the walkthroughs carefully. Add following line to your code after the first do - while loop.
this round function comes with ...
1
vote
Accepted
Could someone explain the significance of double round in pset1 "greedy" (less comfortable)?
double is one of the types in C, like int and char. It's a double-precision floating point number. So
#include <math.h>
double round(double x);
means that the function takes a double and ...
1
vote
Can someone tell why a float variable also accepts an integer value when i use the cast option in my code for pset1?
If you are asking why it's mathematically logical to do that, that's because the set of integer numbers is a subset of the set of real numbers in mathematics. If you mean how it's possible in code ...
1
vote
Accepted
Not sure what happened with Pset1, Greedy
There are a couple of problems. First, the print statement is inside the while loop, as stated by MARS, and needs to be moved outside the loop.
More importantly, this code will only count quarters. ...
1
vote
Accepted
Stuck on Problem set 1! (greedy.c)
First of all, the problem with killed by the system is due to your program being stuck in an infinite loop.
When I get into such an issue it helps me to take out a pen and paper and follow the ...
1
vote
Pset1 Greedy; problem with do-while loop
Your while loop does not end up
EDIT:
(Thanks to SuperNovaCoder for your intervention)
The problem is that equality must be achieved ie the conditions must be >=
EDIT1:
while(cent_amount>=0)
...
1
vote
Accepted
My pset1 greedy compiles and runs successfully but is marked 0. Why?
Contacting Glenn Holloway via [email protected] can help give you an idea of what went wrong. It could be an issue with check50 and/or the grading system. Remember - the grading system is ...
1
vote
Accepted
Pset 1 Greedy - only wrong values at 1.6 and 0.15
I don't understand the line double round(double change);.
Don't use floating point numbers when dealing with money, as a floating point number cannot exactly represent numbers that are not multiples ...
1
vote
Compilation error on function round in greedy.c, pset1
Did you use make? Or did you manually type clang [flags]? If it is the latter you must link the math.h library with the -lm flag.
1
vote
Accepted
pset1, greedy, not quite sure what to fix but I think it has to do with typecast.
You have a couple of issues going on here, but you're on the right track.Yes, it has a lot to do with order of operations. Look at the line:
change_due = round ((int)GetFloat()*100));
The first ...
1
vote
Accepted
CS50 Pset1 - Schizofrenic Greedy Test
One of the hidden lessons in the first few exercises is that programs need to be designed and written to exactly satisfy a program specification. This is particularly important in team programming ...
1
vote
Use of round in the greedy problem
roundf and other functions in the "round" family should be used wherever a float or double must be rounded, that includes both input (before any calculation is performed using it) or output (values ...
1
vote
pset1 greedy error
The compile error messages are quite literal. The code is not doing anything with the values returned from the function calls. For example, look at the followinig:
do
{
change25;
...
This code ...
1
vote
Pset1 Greedy Help Please
The point of the exercise has been missed - floats are frequently not precisely stored.
If you want to see the problem, try printing cents to a precision of 20 digits after the decimal. Also, for ...
1
vote
Accepted
pset6 greedy - python decimal points driving me crazy!
If you cast to an integer using int(), just like any other operation, you need to assign it to another variable. There's no reason you can't get the amt variable from get_float, and then in one line, ...
1
vote
Accepted
What does "using" a variable entail and why does this inconsistency come up? (pset1, greedy, general question)
The root of this problem lies in the order of operations. In C, postfix increment (++) binds more tightly than dereference (*). Thus, what the code *coins++; does is not to dereference coins and then ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
greedy × 281pset1 × 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
compile × 2