0

Here is my code for the pset1 greedy. Now it all works from what I can tell and tested and used cs50 check aswell...

Problem is it was hinted in the walk through and me having to look up how to use round properly, that I maybe should of used modular somewhere? I get what it does. eg 10 % 3 = 1. Is it worth doing it again using modular %. Also any advice on my method? Thanks in advance.

  • Ye sorry, meant 10! Thanks for pointing that out. – Jamie Evans Sep 26 '17 at 16:44
  • 1
    The number of quarter coins is `num_quarters = change_int / quart;` leaving a new balance of `change_int -= num_quarters * quart;` You could even put the four coin values in an array and do the thing in a loop. – Weather Vane Sep 26 '17 at 17:10

1 Answers1

1

To calculate how much coins are needed you can divide change_int by the current coin value. To calculate how much change is left you can the do the same calculation, but only with % instead of /.

This will speed up the programm for large change values because you don't have to use the while loop anymore.

Also as Weather Vane wrote in a comment you can put the four coin values in an array and do the calculation in a loop.

Sister Fister
  • 368
  • 1
  • 10
  • 1
    Well darn me I missed the `change_int %= quart;` – Weather Vane Sep 26 '17 at 17:45
  • @WeatherVane Yes, such things are easy to miss. This seems to be a common excercise (I already saw a similar question yesterday), so it might be good to have a well documented solution which we could then just post a link to... – Sister Fister Sep 26 '17 at 17:56
  • @WeatherVane I just saw that you also commented there. https://stackoverflow.com/questions/46411952/why-my-program-works-correctly-for-some-tests-and-not-for-others – Sister Fister Sep 26 '17 at 18:22
  • 1
    oooh mybad I said I missed it, not that I didn't know about it. – Weather Vane Sep 26 '17 at 18:25