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
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 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
greedy counter equals to 1 with while loop
The problem is straightforward. Look carefully at this line:
cents =-25;
You have just done an assignment that sets cents to -25.
=- is an equals assignment operator followed by a negative sign.
...
3
votes
Accepted
What is wrong with this code for greedy (pset1)?
You are not learning what this pset is trying to teach you. That the floats in your computer are stored imprecisely. Take a look at the following program:
float_test.c
#include "stdio.h"
#include "...
3
votes
Accepted
How to check for valid input in pset1 greedy
You are missing the whole forest for the tree. You didn't learn what this exact lesson was written to teach you, that the floats in your computer are stored with imprecision, and there is nothing you ...
3
votes
problem set 1 and check50 question
You have to also print a newline after the number of coins.
Change this:
printf("%d", totalCoins);
to this:
printf("%d\n", totalCoins);
3
votes
Accepted
Problem Set 1 - greedy.c - Program hangs when GetFloat() used
Your do/while loop executes while amountInt >=25 (and other values). But you never update its value, so it's always going to be >= than the value it had when it entered the loop. That's why you get an ...
3
votes
Accepted
Modulo operator on greedy?
When you code for greedy, you'll need to do two things as you're counting the coins. You need to figure out how many coins of a given denomination (quarters, dimes, etc.) are needed and then how much ...
3
votes
Accepted
Organizing do while
There are two major issues here - scope and infinite loops - along with the do/while vs. while issue.
Scope: When you declare a variable, it only exists between the immediate set of curly braces that ...
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
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
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 PROBLEM
The purpose of this exercise is to understand the issues with inexact storage of floats. You've run into exactly this problem. In your code, a will never get all the way to zero. To see what's ...
2
votes
getting error message for greedy expeted identifier
You have a series of issues here, all to do with correct program structure. Some of them are hiding others.
First, int main (void); has a semicolon. This is causing problems and masking others. main ...
2
votes
Accepted
pset1 trouble in understanding greedy concept
Good question in an international teaching environment. U.S. currency is based on the Dollar, as you have probably learned at some point. We also have coins that represent a fraction of $1 (one ...
2
votes
Pset 1 - Greedy
You are doing things that are not requested by the specification. You should do exactly what you are asked and nothing more, as the programs are checked by computers and not real humans, and thus they ...
2
votes
Typecasting doesn't work as expected in pset1
It looks like you don't have a clear understanding of how typecasting works. It is a temporary change, not a permanent one, and is only active for the current line of code. You had the following code:
...
2
votes
Accepted
I don't know why it doesn't work, help please? greedy.c code below pset1
You have a "divide by zero" exceptiion.
In your code, you've defined QUARTER as 25. You also declare quarter and initialize it to 0. Then you have the following line:
quarter = cent / quarter;
C ...
2
votes
Accepted
Typecasting with Modulo in Greedy
Modulo only works with integers. You are attempting to apply it to a float.
If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)
2
votes
Accepted
PSET1 Greedy Algorithm critique?
Yes, there are more efficient ways to do this. Your way isn't wrong, it gets the job done, but there can be better ways to do a given programming task.
Let's look at the efficiency of your code. To ...
2
votes
Accepted
Inappropriate behaviour of greedy.c program!
as denoted by the lectures and other resources, floats are not precise. that is, 4.2 may not necessarily get stored as 4.2. instead, it may be stored as 4.1999999. when subtracting the integer part, ...
2
votes
Accepted
greedy 4.2 error
That's a good question. Respectfully, your result of 4.20000016 is highly suspicious to me because in every environment I've seen this program tested in, 4.2 is always stored as something slightly ...
2
votes
Accepted
cs50 Problem Set 1: Greedy. float 0.01 problem
Most of the problem is that no matter what you do, you keep putting the result back into change, a float. Floats have this nasty little problem that they don't store fractional parts of a number, the ...
2
votes
Accepted
pset1 - Problems with "greedy" - "undeclared identifier"
Well, you did, but you didn't declare the identifiers. The problem is scope. Numerous variables are being used outside of the scope where they exist. When a variable is declared, it is only valid ...
2
votes
Accepted
Why my code for "Greedy" does not pass the test
Let's debug your program by hand.
We are given an input of 1.
m is assigned the value 100.
since m >= 25, sum_coin25 = 100 / 25 so sum_coin25 == 4
By the way, all of those ifs after the divisions ...
2
votes
My output for greedy.c is correct when I check it manually but not when I check it using check50
An unintended benefit of CS50 is learning how to read and adhere to specifications, an invaluable skill if one intends to code for pay :)
From the spec:
Incidentally, so that we can automate some ...
2
votes
PSet1 greedy.c: Trouble with nickles and pennies?
Have you considered using division and modulo functios instead of subtraction only?
integer division carries only the quotient and modulo only the remainder.
(5 / 3) = 1
(5 % 3) = 2
Will save you a ...
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