Re: Delphi task

Posted by Maxwell

In Reply to: Delphi task posted by stephen

: Hi there,
: I need help ... I have to solve the following task, a person buys goods and everytime the person receives change. The aim is that the person has as few as possible coins in its wallet. The person has an unlimited value of one dollar coins. And at the beginning 5 1 cent coint, 5 2 cent coins, 5 5 cent coins, 5 10 cent coins, 5 20 cent coins and 5 fifty cent coins in his wallet. I shall simulate as many times as the programs user wants an amount the person has to pay (between 0.05$ and 30.00$). The program shall return which coins I shall give to the cashier (for each amount). The cashier always gives as few small change as possible. The person has everytime enough 1$-coins (or notes) and only wants to have as few 1cent, 2cent, 5cent, 10cent, 20cent, 50cent coins as possible in his wallet. I don't really know how I shall solve that I would be very nice if you try to help me!
: It should be programmed with Delphi (5 or 7)

If I understand you well:

* the program should tell you which coins to use, each time that a "price" is given by the user;
* program should minimize the wallet's number of coins smaller than $1;
* with each new question, using the current "small" coins in the wallet?

Correct me if I'm wrong :)

I would start by defining a TCoins record type:

record TCoin
  Value: integer; // coin value in cents
  Number: integer; // number of coins

Next, there has to be an array of TCoin that are in the wallet:

Wallet: array [1..6] of TCoin;

Fill that array with values:

Wallet[1].Value := 1;
Wallet[1].Number := 5;
Wallet[2].Value := 2;
Wallet[2].Number := 5;
and so on...
Wallet[6].Value := 50;
Wallet[6].Number := 5;
When a "price" is given by the user (value between 0.05 and 30.00), convert it to cents.

Test a scenario; it's valid if at the end the price is payed. If it's not valid, try another scenario, and so on... If no scenario fits, pay with only $1 notes.

Next, receive the change and add it to the wallet.

... ;) nice puzzle, but it needs some very smart coding, and LOTS of debugging... ;)

Good luck!

DelphiLand Discussion Forum