# Questions tagged [greedy]

greedy.c, one of the assignments in Problem Set 1, asks you to develop a "greedy algorithm" to calculate the minimum number of coins needed to make change, while demonstrating an understanding of floating point number storage in a computer.

281 questions
Filter by
Sorted by
Tagged with
43 views

### Cash Program not returning correct value for .41

I'm working on the Cash program in Week 2 and I think I have it all finished up the only problem is when I run check50 I get everything right except for .41 instead of returning 4 coins it gives an ...
33 views

### Problem probably related to floating numbers from CS50 pset1 cash

how you doing? I can't figure out where is the bug from my code :/ It's really strange and the results are not the same if you do the same test with "0.01", for instance... #include <...
38 views

35 views

### Cash 50 - Less Comfortable. Trying to nest so the algorithm will consider all money levels. Not working

coder newbie here. As far as I understand, this should work. This is the logic of my code... I get the float input from the user, and keep asking until I receive a positive number. I change this into ...
71 views

### Having trouble with Cash but does not know what is wrong

I am having trouble with my code for pset1/cash. When I check the code, this is my output: :) cash.c exists :) cash.c compiles :( input of 0.41 yields output of 4' expected "4\n", not "...
19 views

### My program doesn't run when i close the condition's range. When i leave the range open it does't give the answer i want

#include <cs50.h> #include <stdio.h> #include <math.h> int main(void) { float change; int cents = 0, coin_number = 0, quart = 25, dime = 10, nickel = 5, penny = 1; //...
41 views

### pset1 less greedy - keep on getting errors

I have added a first version of my code, however it is unable to compile and I just cannot figure out why. INPUT #include <cs50.h> #include <stdio.h> #include <math.h> int main(void)...
60 views

### Greedy Algorithms PSET1. I wrote this code but at some cases it fails to give the correct output. Can someone please help me.?

#include <stdio.h> #include <math.h> int main(void) { float dollar; do //Loop to prompt user to type positive value { printf("...
42 views

### CS50 Greedy, Failing 2 Tests

I have searched online but can't seem to wrap my head around what is wrong. My code passes all the tests except 2 of them, which I am having difficulty understanding. Error 1: :( input of 1.6 yields ...
141 views

### pset1 cash: greedy algorithm coin change round off

The output is not being rounded off: It doesn't count the cents value if you input a dollar in decimal. if it's \$45.67, it only counts change for \$45. Also, output comes back 0 if the input is ...
53 views

### Coin counter help

I'm stuck and I don't know how to make it count the amount of coins minimum. I would like some help on my code please. Thanks. #include <stdio.h> #include <cs50.h> #include <math....
3k views

### How to return a value from a loop?

I have been trying to return value "a" to print it from while loop but can't find any ways so far. #include <cs50.h> #include <stdio.h> #include <math.h> int main(void) { float ...
76 views

### Why removing a printf function messes up with my code? CS50 pset1 cash greedy challenge

I finished the challenge and my first C code worked apparently well, returning each time the correct minimum number of coins needed for change. Then when I tried to "clean it up" a bit and remove a ...
272 views

### cs50 pset 1 greedy algorithm

#include <cs50.h> #include <stdio.h> #include <math.h> int main(void) { float y; do { y = get_float("Change: "); } while (y < 0); printf("%f\n", y * 100); ...
132 views

### PSet1 cash, modulo problem

Why does this give 19 instead of 20 when change = 4.2? Thanks.
64 views

### Why is my program not entering the while loop in greedy? Pset1

While I was trying to solve the greedy less comfortable problem in pset1, I ran into a slight problem on the last loop in my program. Here is my code: (the reason why I have some random print ...
71 views

### set1/cash (or greedy)/ Floating point imprecision and rounding requirement help

I think I have figured out a way to convert the users input from dollars to cents. I used the eprintf function and it works. However, when re-watching Zamyla's walk through she mentions towards the ...
261 views

### Error: greedy cash input of 0.41 yields output of 4

I'm getting an error and I can't figure out the solution to this problem. I get an error at check cs50: "input of 0.41 yields output of 4; expected "4\n", not "You typed: 0.41..." Here is my code: #...
172 views

### Week 1 cash.c assignment: How can I get a variable to mutate as it gets operated on?

the way my code works is to reduce c as we count out coin-types. But I'm getting errors. #include <cs50.h> #include <stdio.h> int main(void) { float c; do { ...
218 views

### Problem Set 1: Greedy and While Loops

My program for pset1 (greedy) compiles fine and runs. However, it just prompts the user for the amount of change. After that, a result to show the number of coins never gets printed out, and the ...
64 views

### cs50 pset1 greedy (expect 4 coins for 0.41)

I thought I had cracked pset 1 (greedy) but when I submitted the code it was finding errors with some of my calculations. The main one that i struggle with is where it states that it is expecting an ...
381 views

### :( cash compiles expected exit code 0, not 1

I have tried everything to solve this. I have searched googled, use help50 and even debugger50. My program runs fine, the problem consist when I use check50. i get " :( cash compiles expected exit ...
92 views

### Cash-Is my logic correct?

plz take a look at my code ``` #include <stdio.h> #include<cs50.h> #include<math.h> int main(void) { int coins=0; float change=0; int counter=0; int qr=25; int ...
421 views

I am working on pset1 cash. I'm taking it step by step trying to work out the issues so I know the logic isn't complete but I would really like someone's input explaining why my script only works ...
123 views

### imprecision problem cs50 greedy

#include < cs50.h > #include < stdio.h > int main(void) { float change; int quarter = 25; int dime = 10; int nickel = 5; int penny = 1; int counter = 0; int c ...
127 views

### Help me with my greedy.c problem code cs50 pset1

#include < cs50.h > #include < stdio.h > int main(void) { float change; int quarter = 25; int dime = 10; int nickel = 5; int penny = 1; int counter = 0; int c; //user input do { printf(...
96 views

### Refactoring Pset1 Greedy Modulo

I've got Pset 1, 'Greedy' working, I just think I can refactor it better. How can I get rid of ints a, b, c and d? a = amount /25; amount = amount % 25; b = amount /10; amount = amount % 10; c = ...
48 views

### code works strangely in greedy without modulo

I have been trying to help my friend with greedy without modulo (mine I did with modulo). We used the if conditions and for loops. The code works but counts coins strangely. When I give an input of 0....
98 views

### Problem Set 1 greedy

My program works for any value except 4.2 and I realised that the occurring problem is due to same kind of a mistake as this one: #include <stdio.h> int main(void) { float x = 4.2; x = ...
144 views

### Greedy 4.2 input issure

Can anyone explain to me why this is the result im getting? Checking........ :( input of 4.2 yields output of 18 expected "18\n", not "22\n" everything else is correct. this is my code #...
235 views

### greedy with modulo

I've been working on my greedy with modulo for a while now and I'm not sure what I'm doing wrong.(For the moment I'm focusing on the part where I use integers to count the number of coins, skipping ...
70 views

### Please I need help with greedy

I understand the logic but I keep getting these errors #include <stdio.h> #include <cs50.h> #include <math.h> int main(void) float n; do { printf("how much do I owe you\n"...
151 views

### Problem with CS50 greedy.c. All numbers work except \$23 and \$4.2

I'm having some trouble de-bugging my code for greedy.c of CS50 Pset1. When I input 4.2, I get 12 instead of 18. When I input 0.2, I get 2 (the correct answer). When I input 4, I get 10 (so here is ...
153 views

### Greedy. Help? I have no idea what to do

If anyone could give me any pointers on what is wrong with my code, it would be greatly appreciated! Also, any tips on how to be a better coder? Should I read more books / articles? Honestly, I am ...
93 views

### Greedy - Do While Negative Float

I'm testing the very first requirement of the Greedy assignment (ensure the input is not negative). I'm running into an issue where if I use < 0 in the while statement, -0.1, -0.2, -0.3 etc. is ...
74 views

### pset1_greedy: weird output when input=4.2

guys My code functioned correctly in all other numbers I input, except when I input 4.2. It should just used up 18 coins, which is 16 quarters and 2 dimes, but instead it used: 25(quarter): 16 10 (...
286 views

### Greedy Compiling issue

I am trying to complete the "Greedy" problem on CS50 and am running into some issues when attempting to compile my code so far. Please see my code below: #include <stdio.h> #include <cs50.h&...
41 views

### invalid output in check50

My code gets the wrong output for .41 and .01 when run through check50. why? #include <cs50.h> #include <stdio.h> #include <math.h> int main(void) { float cents; int quarter ...
103 views

### pset1 - greedy not working for all values

So I'm having issues with this producing the right answer for a some money values like .53. It gives me 4 coins when it should be 5. #include <stdio.h> #include <cs50.h> int main(void) {...
213 views

### Pset1 greedy.c fails check50 [duplicate]

CS 50 Check50 link This is my code for greedy.c of the pset1. I seem to be getting the correct output for all input queries but my code fails to pass check50 withgiving errors i'm not able to exactly ...
248 views

### Greedy(less comfortable) questions/thoughts/input/advice on my programm

Hello I am relatively new to programming and C in general. I have left the comments next to the points I have questions to, please don't give me any solutions, as I really want to learn and ...
139 views

### Could someone explain the significance of double round in pset1 “greedy” (less comfortable)?

I've wrote the following which passes check50 and operates as intended: #include <stdio.h> #include <cs50.h> #include <math.h> ... int cents = round(change * 100); ... I've ...
98 views

### Why greedy.c shows unexpected result due to float variable?

The below code is for the problem greedy in pset1. The code is working fine but in the end part variable 'pzeroone' shows an unexpected result. #include <stdio.h> #include <stdlib.h> int ...
52 views

### greedy problem is only printing 1

I can not figure out why my code is only printing out 1 when I type in .41. Any help would be appreciated. int main(void){ /* make change when given a number */ float change; //promt ...
169 views

### greedy.py returning integer variable ending with .0 even w/ conversion from float to int() & round()

I am converting my greedy.py function and since python does not have datatypes, i'm getting really confused as to why my code does not convert to an int w/out a .0 ending. Code: import cs50 class ...
54 views

### Can someone tell why a float variable also accepts an integer value when i use the cast option in my code for pset1?

The following is my code : float f; int counter = 0; float amount; do { printf("O Hai! How much money do I owe?\n "); amount = get_float(); } while(amount <= 0); /* rounds off the input ...
80 views

### Can someone please tell me the errors?

#include <cs50.h> #include <stdio.h> int main(void) { float dollar; int penny = 1; int nickel =5; int dime = 10; int quarter = 25; int cent, change; int a, ...
222 views

### Here's my code for “Greedy” . It works perfectly. Yet I got a score of 69. Could someone please tell me what's wrong with it?

#include<cs50.h> #include<stdio.h> int main () { **float f; do** { **printf("O hai! How much change is owed?\n"); f = get_float();** } **while(f<0); ...