# Tag Info

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)...
• 69k
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 ...
• 28.5k

### 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 ...
• 812
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 ...
• 69k
Accepted

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 ...
• 18.7k
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 ...
• 69k
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 ...
• 18.7k
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 ...
• 21k
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 ...
• 69k
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 ...
• 69k
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 ...
• 69k

### 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 ...

### 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 ...
• 28.5k
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 ...
• 7,396
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: ...
• 69k

### 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?
• 1,122
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;
• 5,179
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 ...
• 7,396
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.
• 21k
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.
• 18.7k

### 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 ...
• 69k
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 ...
• 69k
1 vote

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 ...
• 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.
• 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 ...
• 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, ...
• 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 ...
• 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 ...
• 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)
• 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 = ...
• 5,179

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