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.

Filter by
Sorted by
Tagged with
0
votes
1answer
24 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 ...
0
votes
1answer
21 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 ...
0
votes
1answer
38 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); ...
0
votes
1answer
72 views

PSet1 cash, modulo problem

Why does this give 19 instead of 20 when change = 4.2? Thanks.
0
votes
1answer
46 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 ...
0
votes
1answer
38 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 ...
0
votes
1answer
63 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: #...
0
votes
1answer
46 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 { ...
1
vote
1answer
132 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 ...
0
votes
1answer
40 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 ...
-2
votes
2answers
245 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 ...
0
votes
1answer
76 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 ...
0
votes
1answer
349 views

Pset1 cash. Please help me understand my script. Only works for 35 cents

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 ...
0
votes
1answer
105 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 ...
0
votes
1answer
116 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(...
1
vote
2answers
72 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 = ...
0
votes
1answer
41 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....
0
votes
1answer
90 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 = ...
0
votes
1answer
138 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 #...
0
votes
1answer
170 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 ...
0
votes
1answer
67 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"...
1
vote
1answer
126 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 ...
0
votes
1answer
133 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 ...
0
votes
1answer
41 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 ...
0
votes
1answer
63 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 (...
0
votes
1answer
134 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&...
0
votes
1answer
39 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 ...
0
votes
1answer
91 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) {...
0
votes
0answers
163 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 ...
0
votes
1answer
132 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 ...
0
votes
1answer
106 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 ...
0
votes
1answer
61 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 ...
0
votes
2answers
50 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 ...
1
vote
1answer
109 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 ...
0
votes
1answer
46 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 ...
0
votes
2answers
72 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, ...
0
votes
1answer
186 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); ...
0
votes
1answer
188 views

IDE not working properly

I am having an issue with the CS50 IDE. I'm trying to run greedy.c from pset1 and the output is always my input as a float instead of the number of coins as it should be. I thought the issue was my ...
0
votes
1answer
67 views

Not sure what happened with Pset1, Greedy

The following is my last attempt at this project but my result was way off! It is multiplying the user input by 4 and then counting up to the total ie; 1,2,3,4, etc. I've been going over it and I ...
0
votes
1answer
162 views

My Pset1 greedy code produces no errors and the wrong answer

I changed all the "remaining" to cents and its didn't work. I still can't see the logic why I made the made the change. I want to understand this. Here is my code: #include <cs50.h> include ...
0
votes
1answer
486 views

Stuck on Problem set 1! (greedy.c)

I've been trying to solve this problem for a while but I am completely stuck, so thought I'd ask for help. This is what I have so far: #include <stdio.h> #include <cs50.h> int main(void)...
0
votes
1answer
44 views

PSet1 Getting Weird Numbers in Greedy

My code at least appears to similar to others' I have seen on here, but I can't seem to get the right answers. If I enter anything less than "1" I get "0" for the number of coins, and if I enter "1.25"...
0
votes
1answer
47 views

This is my code for greedy how do i convert it to decimal. I want to take the final value of coins_4 / 100. But I get an error

Or I could use Getfloat then times by 100. But I get an error. I think this makes more sense. #include <stdio.h> #include <cs50.h> int main(void) { int amount; int change = 0, ...
0
votes
1answer
149 views

Pset1 Greedy; problem with do-while loop

When I try to run my code, I get prompted for the change amount, as desired, but when I enter an amount, nothing else appears in the terminal and the program does not calculate the minimum number of ...
0
votes
2answers
37 views

why does this not allow coins =0.01 and greater?

{ float coins; do { printf ("please pick an positive number\n"); coins = GetFloat(); } while ( coins < 0.01 ); printf("%f", coins ); }
0
votes
1answer
344 views

How to fix Error: unused variable (Pset 1 - Greedy)

I don't understand why I keep getting an error message regarding the variable "amount." Can anyone identify/explain the problem and how to fix it? The relevant part of my code is below. Thanks in ...
0
votes
1answer
19 views

I Have used define for Quarters . Quarters = .25. The line that is an error is change = coins % Quarters

I can't seem to get modf to work. Can someone help? It might be called fmod. change = coins % Quarters; if (change >= Quarters) { change = coins - Quarters; ...
0
votes
1answer
64 views

Greedy numbers between 0 and 1 … and 4.2

I have good number of coins for number over 1 with decimal (ex: 2.7, 5.1). And wrong number of coins only with numbers between 0 to 1 (0.99 or 0.41): always 0. plus the check50 says that it doesn't ...
0
votes
1answer
68 views

pset1 greedy.c check 50 programm error

I am wandering around for answers. But I end up that my code is correct ( I'm 90% sure :P ) #include <cs50.h> #include <stdio.h> int main(void) { double change; do { ...
0
votes
2answers
80 views

Greedy algorithm1

#include<cs50.h> #include<stdio.h> #include<math.h> int main() { float f=0; long int a=0,c=0; printf("Enter the float value:"); do { f = get_float(); }while(f <= 0); a = (...