Skip to main content
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)...
Cliff B's user avatar
  • 69k
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 ...
DinoCoderSaurus's user avatar
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 ...
robert_x44's user avatar
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 ...
Cliff B's user avatar
  • 69k
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 ...
curiouskiwi's user avatar
  • 18.7k
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 ...
Cliff B's user avatar
  • 69k
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 ...
curiouskiwi's user avatar
  • 18.7k
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 ...
Blauelf's user avatar
  • 21k
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 ...
Cliff B's user avatar
  • 69k
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 ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

Still Need Help. Pset1 Geedy with round()

It looks like you have a couple of misconceptions in your code. Mostly, you need to understand how integer math works compared to normal math. For instance, when the content of a float is stored in an ...
Cliff B's user avatar
  • 69k
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 ...
Anil Khosla's user avatar
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 ...
DinoCoderSaurus's user avatar
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 ...
ChrisG's user avatar
  • 7,396
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: ...
Cliff B's user avatar
  • 69k
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?
Steve Bunting's user avatar
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;
MARS's user avatar
  • 5,179
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 ...
ChrisG's user avatar
  • 7,396
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.
Blauelf's user avatar
  • 21k
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.
curiouskiwi's user avatar
  • 18.7k
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 ...
Cliff B's user avatar
  • 69k
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 ...
Cliff B's user avatar
  • 69k
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 ...
Cliff B's user avatar
  • 69k
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.
kluvin's user avatar
  • 1,223
1 vote

is this loggically wrong?

The problem lies in your code: while (c>=1){ l=c/1; c=c-1; } The last should have been a modulo operation, not a subtraction. But then, any x%1 = 0. If you think about it, you can ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

pset1 - Problems with "greedy" - I've written my code, but there is no output

You've created infinite loops. Look at the first version: do { p = 0; i = q - p * quarter; p = p + 1; } while (i > 0); p is set to 0 at the beginning of each pass through the loop, ...
Cliff B's user avatar
  • 69k
1 vote

Use of functions and variable declarations outside main() on pset1 greedy

The difference between where a variable is declared is scope. Strictly for this discussion, consider main to be a function, even though it is a special one. A local variable is a variable declared ...
Cliff B's user avatar
  • 69k
1 vote

Getting started on greedy.c

Start with this paragraph from the pset specs: How few? Well, you tell us. Write, in a file called greedy.c in your ~/workspace/pset1 directory, a program that first asks the user how much ...
Peter's user avatar
  • 1,410
1 vote

I'm stressed, someone pls help

your problem is rounding, greedy can work, sometimes without using rounding, be sure to follow the specifications and you must use the function round () int cents = round(change*100)
MARS's user avatar
  • 5,179
1 vote
Accepted

What's wrong in my greedy code?

your problem is rounding, greedy can work, sometimes without using rounding, but checking 0.41, is not a random choice, be sure to follow the specifications and you must use the function round () n = ...
MARS's user avatar
  • 5,179

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