Im new to Pascal and have to write a program to calculate coinage given as change, ie 1 x20p . I just haven't got a clue how to get Pascal to calculate the coins.

## All 6 Replies

Do you know how to code in Pascal at all?

Have you made an attempt any this code yet?

What are your thoughts on how you might begin to tackle this?
(hint: think about the thought processes you'd go through if you were calculating this in your head; write them down)

Hi,
I have just started my course and this is the first assignment I have done a little coding and understand the basics.
For the assignment I have to create a program for a hypothetical cash register that will calculate change given leaving the answer in coins. The program will use div and mod which Im ok with using and it also has to include a sensible loop, I understand the loops a little. To be honest though I don't think I could get a formulae to work for that output with a calculator and I thought my maths were pretty good !

Many Thanks
;)

Of course you could do it in your head: it's easier than you think. I would approach it this way:

You have a price, p, and an amount of money given, m. I would hope the m > p, but I'd put in a check to ascertain the veracity of this.

Now, to calculate the change, c, it's simply a matter of m-c.

Now, we have a discrete number of coins, say 50p, 20p, 10p, 5p, 2p and 1p.

There are usually a number of ways in which c can be made up from these denominations: 15p can be 15x1p; 3x5p; 1x10p+1x5p; etc.. For the purposes of this exercise, I'll assume that you want as many large denomination coins as possible.

So work through the coins in order: if I need 47p change, start with the largest denomination:

• 50p. How many 50p in 47p? 0.
• now 20p. How many? 2! That's 2x20p and 7p left to get...
• now 10p. How many 10s in 7? 0.
• now 5p. How many 5s in 7? 1! That's 2x20p, 1x5p and 2p left
• now 2p. How many 2s in 2? 1!

So list out the number of coins, and Bob is your Mother's Brother.

Now make sure you understand this method, make any improvements you deem necessary, write it out neatly as a series of steps, then try and fit those constructs you mentioned earlier to the steps in the algorithm.

Finally, and this shouldn't take long now that it's all written out and you know what you're doing, begin coding.

I think Ive got it now, I use to my loop do do the divisions.Can I come back to you for further help if I get stuck?
Thanks for this Ive landed myself in a class were everyone else is slightly more advanced than me ! :eek:
Thanks again for your help x
:)

No problem.

Glad to see you've worked it out. Jumping in at the deep-end might seem daunting at first, but if you have patience and are able to think about things, then it's the quickest way to learn.

I'd be interested to see your code when you've finished.

I was going to ask you to have a quick look at it when it was done anyway, just incase !
Im going to have a good go at it over the weekend and see what happens......watch this space !
Thanks again, will post when it's done.