Question

In: Computer Science

I want to design an algorithm decides on the notes to give a customer at a...

I want to design an algorithm decides on the notes to give a customer at a cash dispensing machine. The machine only has $20 and $50 notes and I want my algorithm to dispense the minimum number of notes to the customer. This means that if a customer wants $120 then the machine will dispense two $50 notes and one $20 note rather than six $20 notes.

I propose an algorithm to solve this problem.

My algorithm is:

input: amount  (the amount of cash that is needed)
output: notes (set of notes)
while  amount is greater than $50
   subtract $50 from amount
   add $50 note to notes
end while

while amount is greater than zero dollars
   subtract $20 from amount
   add $20 note to notes
end while

if amount is less than $0 
    print error message "can't give change for that amount"
else
    print out the content of notes
end else

This algorithm is incorrect in the sense that, for some values it will print out the error message:

"can't change amount"

for some values that can actually be changed with just $20 and $50 notes. List three values of amount under $251 for which the algorithm will behave incorrectly.

What is a simple correction you could make to the algorithm to make it distribute change correctly ? (1 mark)

Solutions

Expert Solution

Three values for which the algorithm behaves incorrectly are

  • $60
    • Here, algorithm dispenses $50 first and reaches $10. Since there is no way to dispense $10, it prints the error message. But the right way to dispense cash was [$20, $20, $20]
  • $80
    • Here, algorithm dispenses $50 first and reaches $30. It dispenses a $20 and reaches $10. Since there is no way to dispense $10, it prints the error message. But the right way to dispense cash was [$20, $20, $20, $20]
  • $110
    • Here, algorithm dispenses $50 two times to reach $10. Since there is no way to dispense $10, it prints the error message. But the right way to dispense cash was [$50, $20, $20, $20]

If we look carefully, problem arises when we reach the amount $60 or $80. Though both the amounts are greater than $50, we need not dispense $50 at that point but dispense a $20. This will ensure algorithm distributes change correctly. So a simple change can be introduced in the algorithm as follows -

while amount is greater than $50 but not equal to $60 or $80
   subtract $50 from amount
   add $50 note to notes
end while

while amount is greater than zero dollars
   subtract $20 from amount
   add $20 note to notes
end while

if amount is less than $0
   print error message "can't give change for that amount"
else
   print out the content of notes
end else


Related Solutions

I want this program written in JAVA with the algorithm(The step by step process for the...
I want this program written in JAVA with the algorithm(The step by step process for the problem) . Please help it is due in a couple of hours. I don't want the C++ program, I want it in JAVA please #20 Theater Ticket Sales Create a TicketManager class and a program that uses it to sell tickets for a single performance theater production. Here are the specifications: • The theater's auditorium has 15 rows, with 30 seats in each row....
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design in place sorting algorithm? How this in place term is related to the time complexity and space complexity of the program?
I want answers in 60 minutes Design a CV for the job of a Physiotherapist at...
I want answers in 60 minutes Design a CV for the job of a Physiotherapist at any hospital supposing an individual has completed DPT with an A grade and have 2 years of professional experience in the related field. Your answer Define the terms: Cohesion, Coherence, Skimming, Scanning Your answer You are the CEO of a company write a memorandum to your staff explaining the new software the company has purchased. Your answer What five major details should a person...
I want letter about appreciation,to appreciate agency give me scholarship . please I want typing because...
I want letter about appreciation,to appreciate agency give me scholarship . please I want typing because I don't understand handwriting thanks
I want to design a project for fun, since I am planning to take electromagnetics next...
I want to design a project for fun, since I am planning to take electromagnetics next semester. I need help with this design Design Construct and demonstrate An arc generator solely driven by electrostatics, magnetostatics, and/or electromagnetics Techniques using only household materials and a 9V battery.
C programming problem I have to design an efficient algorithm which solves this problem. Also, it...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm. There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order. A sorted matrix and integer M will be given and we need to find the position(row,column) of M in this matrix. it's okay to report only one position if there are more than one answers. when...
Computing the Proceeds from the Sale of Notes Receivable Below are several customer notes receivable that...
Computing the Proceeds from the Sale of Notes Receivable Below are several customer notes receivable that were sold without recourse. An $8,000, 60-day, non-interest-bearing note sold after 15 days at 12%. A $10,000, 12%, 60-day note sold after 30 days at 14%. A $4,000, 10%, 90-day note sold after 30 days at 12%. A $10,000, 12%, 120-day note sold after 45 days at 15%. Required: Determine the proceeds from each of the preceding sales of customer notes receivable. (Assume a...
7 within the marketing and message design context, explain what is "Give your customer the facts"...
7 within the marketing and message design context, explain what is "Give your customer the facts" and "For desire read detail" mean? 8 a- An old Chinese proverb points out: "I hear and forget. I see and I remember, I do and I understand." What is the I relation of this proverb to creating a desire to a commodity? Explain b- Academic research bears this out. Studies show that attitudes toward products based on direct experience are held with more...
I want important notes and equations in chapter 8 physics 1 Related topics: work done by...
I want important notes and equations in chapter 8 physics 1 Related topics: work done by constant forces chapter 9: 1. kinetic energy 2. gravitational potential energy chapter 10: 1. elastic potential energy 2. power
Hi, I want to design a LAN infrustrure for a building that has 5 Levels OR...
Hi, I want to design a LAN infrustrure for a building that has 5 Levels OR floors. The 1st Level is called ground floor. Can you please help?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT