0

What's wrong with my greedy problem code?

#include<stdio.h>
#include<cs50.h>
#include<ctype.h>

int main(void)  
{   
    float x,a,b,c,d;  
    float=get_float("O hai! How much change is owed?\n");  
    a=x%0.25;  
    b=a%0.10;  
    c=b%0.5;  
    d=c%0.1;  
    printf("%.1f",((((x-a)/0.25)+(x-a-b)/0.10)+(x-a-b-c)/0.5)+(x-a-b-c- d)/0.1);  
    }

1 Answer 1

2

Before the answer, a few housekeeping things:

  1. Don't post huge chucks of your solution to any pset on the internet (a few lines may be ok - just kind in mind reasonable vs. unreasonable behaviour). Although it's true you are showing people so they may help you, remember the people who come to find answers.
  2. Follow the guides on how to ask a question. It makes it much easier for people to read parts of the question (e.g. your code), and what your exact issue/problem/question is (e.g. compiling issue, logical error etc).

From what I gather, you're code is as follows:

#include <stdio.h>
#include <cs50.h>

int main(void)
{ 
    float x,a,b,c,d;
    float=get_float("O hai! How much change is owed?\n");
    a=x%0.25;
    b=a%0.10;
    c=b%0.5;
    d=c%0.1;
    printf("%.1f",((((x-a)/0.25)+(x-a-b)/0.10)+(x-a-b-c)/0.5)+(x-a-b-c-d)/0.1);
}

The first issue that you'll run into is when compiling. float x,a,b,c,d; declares 5 variables of type float for you to initialise (set) later. The next line, however, you've tried to initialise a variable called float to the value of get_string().

Remember that float is the variable type, not name, so when you try to set float (which doesn't exist as a variable) to some value, the compiler gives up and throws an error that expected an identifier (fancy way of saying variable name).

To fix this, reflect on what you actually what to set. A variable, right? So since you've already declared that you want a float named x, but haven't yet set its value, you just wanna set that variable. This is what it would be for an integer:

int age;
age = 18;

See if you can adapt that to what you need to do. The next error you'll have when you compile is that the modulo operator % doesn't work with double or float types.

There are two ways to fix this. The first (which in the instance I would most certainly not recommend) is to use some function that applies the modulo operation to floats like fmod(). The more practical method, and the one recommended by the pset's walkthorugh (something I would highly recommend you watch: it hints to all the solutions in giving you) is to represent the dollars in cents, which is an integer.

The other reason to use an integer is floating point imprecision can wreak havoc on the value (well, maybe not havoc, but it's not what you expect).

Another issue with you're code is the statement in printf. Even though it may technically compile, it's illegible to any other human, and probably yourself in 3 days time. I would recommend keeping track of the number of coins throughout the programme instead of calculating it all at once, not only for readability and style, but also functionality.

Finally, you're last major problem is style and readability. You're variables don't have very descriptive names (x, a, etc.) which is fine for a counter variable whose purpose is very clear, but not for what this programme uses. Try things like dollars, cents, coins, that kind of thing.

Another hugely important thing is comments. It communicates to anyone who reads the code what is happening in every block.

CS50 recommends single spaces between operators (=, +, *, != etc) to help readability. So instead of say

int age=(big_brother-14)%7 

try

int age = (big_brother - 14) % 7

With that said, best of luck with the pset! I notice that you call the problem greedy, which is something that was changed in 2017. I would recommend you do the current edx course of problem sets because, in my humble opinion, the David and TFs have made it easier to learn to code well compared to 2016.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .