Hot answers tagged

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
  • 68.1k
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, ...
Cliff B's user avatar
  • 68.1k
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
  • 68.1k
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. ...
Cliff B's user avatar
  • 68.1k
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 "...
ChrisG's user avatar
  • 7,366
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 ...
ChrisG's user avatar
  • 7,366
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);
ChrisG's user avatar
  • 7,366
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 ...
Irene's user avatar
  • 1,677
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 ...
Cliff B's user avatar
  • 68.1k
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
  • 68.1k
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.6k
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.6k
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
  • 20.9k
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 ...
Cliff B's user avatar
  • 68.1k
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 ...
Cliff B's user avatar
  • 68.1k
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 ...
Cliff B's user avatar
  • 68.1k
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 ...
ChrisG's user avatar
  • 7,366
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: ...
Cliff B's user avatar
  • 68.1k
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 ...
Cliff B's user avatar
  • 68.1k
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. ;-)
Cliff B's user avatar
  • 68.1k
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 ...
Cliff B's user avatar
  • 68.1k
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, ...
kzidane's user avatar
  • 17.6k
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 ...
Cliff B's user avatar
  • 68.1k
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
  • 68.1k
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
  • 68.1k
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,366
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

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

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