# Simplifying “Greedy” Code

My code for "greedy" works, but it's annoyingly repetetive and long. I know there must be some way to simplify it, but I'm at a complete loss as to how to do it. This is my code:

``````int main(void)
{
printf("O hai! ");
float n;

do
{
printf("How much change is owed?\n");
n = GetFloat();
}
while(n<0);

int x = roundf(n*100);
int c1;
int m;

if(x >= 25)
{
m = x % 25;
c1 = (x - m) / 25;
}
else if(x < 25 && x >= 10)
{
m = x % 10;
c1 = (x - m) / 10;
}
else if(x < 10 && x >= 5)
{
m = x % 5;
c1 = (x - m) / 5;
}
else
{
m = x % 1;
c1 = (x - m) / 1;
}

int c2;
int m2;

if(m < 25 && m >= 10)
{
m2 = m % 10;
c2 = (m - m2) / 10;
}
else if(m < 10 && m >= 5)
{
m2 = m % 5;
c2 = (m - m2) / 5;
}
else
{
m2 = m % 1;
c2 = (m - m2) / 1;
}

int c3;
int m3;

if(m2 < 10 && m2 >= 5)
{
m3 = m2 % 5;
c3 = (m2 - m3) / 5;
}
else
{
m3 = m2 % 1;
c3 = (m2 - m3) / 1;
}

int c4;
int m4;

if(m3<5)
{
m4 = m3 % 1;
c4 = (m3 - m4) / 1;
}
else
{
m4 = 0;
c4 = 0;
}

int total = c1 + c2 + c3 + c4;
printf("%d\n", total);
}
``````

Your instinct that it's too long and not efficient is correct. Think about what the code is doing. There is a series of if/else if conditions. Most, perhaps all, are unnecessary.

Consider this. You start with a number x to convert to coins. Instead of testing with an if condition, just apply the calculation to it. If it is >= 25, then determine how many quarters are there with the modulo operation. Next, subtract the value of the quarters from x. (btw, your formula for this is incorrect) and store the result back in x. If x is less than 25, do the same thing, except that it will leave x unchanged and produce a count of 0 quarters, which you want anyways. The point is that you run the same operations, no matter what the value is in x - there's no need to test whether it's >= 25.

This leaves the remaining change in x. Just execute the same process for dimes and then nickels. You could also do it for pennies, but the penny count will simply be whatever is left.

As a side note, some suggestions to improve your coding style.

You have very simple variable names - x, c1, c2, m, etc. Single letter and very short variable names should be avoided, except for simple counters in for loops (usually, i, j, k). They make code very difficult to read and understand, particularly when there are no comments in the code. Instead, use short but descriptive names, like change, quarters, dimes, nickels, pennies, coins, etc.

Comments are a good thing. It would be appropriate to add comments like `// calculate quarters` and so on to describe what a given section of code is supposed to do. Don't make people guess what the code is doing in a particular section. (Especially yourself when you come back to read it in 6 months!)

Whitespace is good, but too much can be as bad as too little. Avoid extra blank lines (i.e., 2 or 3 blank lines where 1 is sufficient) and keep your indents at a standard size, like 4 spaces.

There's a tool called style50. It checks spacing, blank lines, indents, variable names, etc. Use it until your code produces very few suggestions from style50. It will be well worth getting it right while you're in the early stages of learning to program.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)