Lately I wanted some help about this vending machine code though I decided to make some changes to it.
The code does the following
First input is how many customers need to be served
Then second input arethe customers money and number of water bottles they wants.
The vending machine takes in only 50,100,200 coins
At first the machine has zero money and enough bottles to sell.
The program should then decide if the change is less than the money inside the vending machine and there is actual coins to give as change
If yes then it prints yes
If no then prints no
My code passed all the test cases in my assignment but the time limit is not met.
I was asking if someone can make it run faster.
I didnt find the right algorithm so I just cooked up the algorithm for only the test cases which I was given
I know that is so stupid of me but I had to do that because of time.
heh, many a demo has been hacked out in a hurry using hard coded known to work values or the user types in known to work values when showing it off. Time happens.
the first thing I ask of a student who has a time limit issue is whether they compiled the code in release mode. Debug mode can run more than twice as slow, though the actual amount depends on many things.
its clearly too late now but you can kill off a lot of logic with a skip test. If you have a certain amount of coins on hand already, you can always make change, and the answer is known to be yes. You only need to be concerned about making change when you are below that magic amount (I did not try to figure out this number, but if you care to revisit the problem, you can). You should be skipping the logic most of the time, if the input data is somewhat realistic: everyone is going to buy something, and the act of buying should pump up your available money, so after just a few sales you should begin to have the funds on hand for change no matter what they do, most of the time. Does that make sense?
Look at the loop from lines 70-83. If Change < 0 then the look subtracts something from change, ensuring that Change will still be less than zero. The loop will continue until you underflow Change and it becomes positive again.