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....
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer items from a terminal operator, and display to the screen their sum, difference, product and quotient. Note that the quotient calculation (first integer divided by second integer) is only to be performed if the second integer does not equal zero. 2) Design an algorithm that will read two numbers and an integer code from the screen. The value of the integer code should be...
* Please I want answer for these questions and I will give big thump for it....
* Please I want answer for these questions and I will give big thump for it. Thanks Read the following case study and answer the following question. The Five star Hotel ELV (Extra Low Voltage) project was located in Bahrain and completed in 2011. The purpose of this project was to install, test, and commission the IT, communication infrastructure, and services for the hotel. The ELV project was part of a total program to deliver 11 sub system, including installation...
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.
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...
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...
Please I want answer for this and I will give big thump. Thanks Generally in project...
Please I want answer for this and I will give big thump. Thanks Generally in project management, there are three basic forms of organizational structure that is functional, project and matrix. Suppose you are responsible for advising an health care product company on changes to an organizational structure, to embrace the growing need for effective project management in creating new health care products. Draw the sample organization structure of health care products and conduct an assessment of the advantages and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT