pset1.greedy did not use modulo

I'm answering pset1 greedy I did not use any modulo but still got the right answers. Is that okay? Here's my code.

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

int main (void)
{
float change;
int count = 0, nchange;

//check if change is not negative
do{
printf("O hai! How much change is owed?\n");
change = get_float();
}while( change < 0);

nchange = round(change * 100);

do{
//check if chashier can give 25c
while( nchange / 25 > 0 )
{
count++;
nchange -= 25;
}

//check if chashier can give 10c
while( nchange / 10 > 0 )
{
count++;
nchange -= 10;
}

//check if chashier can give 5c
while( nchange / 5  > 0)
{
count++;
nchange -= 5;
}

//check if chashier can give 1c
while (nchange / 1 > 0 )
{
count++;
nchange -= 1;
}
}while (nchange > 0 );

printf("%i\n", count);
}
``````

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 generally unimportant because they're simple tasks that run quickly no matter how they're programmed.

What happens when you write a program that has to execute over and over, perhaps millions of times in a production environment? Think about your algorithm. It runs 4 loops that could run more than once. Let's look at that. For pennies, it could run up to 5 times. (At 5, it's a nickel. Also, it will "test" one extra time to exit the loop.) Nickels will test 2 times max. Dimes will test 3 times max. In any environment, it would have some impact, but not so much. So far, there's a maximum of 10 while loop tests, leaving quarters. Here's where the fun starts. For quarters, there's potentially no limit on how many passes through the while loop are required. It will depend on the value entered. If the number entered were \$1000.00, that would mean about 4000 passes.

On the other hand, let's look at the execution of a program that used the modulo algorithm. Each coin would require one calculation to be executed exactly once for each type of coin. That means that the worst case execution is 4 calculations. (Actually, it can be done in 3, but I'll leave that for the readers to figure out. ;-) ). Also there will be less processing because there's no loop mechanism.

So now, think about a production environment where this has to execute 1 million times. Let's also say that the average amount is \$25. In the while loop algorithm, that's 1M x 100 loop executions. In the modulo algorighm, it's 1M x 4 calculations. That means that the loop algorithm will run more than 25 times longer than the modulo algorithm. Multiply by a million and you could be looking at a program that runs in hours vs. seconds. That doesn't consider loop overhead (or any other statements in both algorithms.)

But you can relax for now. J ust learn the material being presented for now. Once you have the tools you need, the course will cover efficiency ( in a couple weeks.) Just try and master the current topics as they are presented. But do try to learn all of them. It's great to try different methods, but learn to use them all as you go. Don't skip any because you already know something. You may need it later. Best to master them all!

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

• To be honest I haven't figured out how to use modulo in this pset. But I get what you're saying about efficiency and I'll try to figure it out. Thank you! Feb 27, 2017 at 11:35

I think it's fine, there's often lots of ways to accomplish the same task in code!

As a note, is your big do...while loop necessary? When would it ever run more than once?

• Absolutely right. There are almost always multiple ways to write a program. Feb 26, 2017 at 22:49
• I forgot to remove the big do..while i had it in my first version of greedy which had different code. Thanks for pointing it out! Feb 27, 2017 at 11:32