Write a program to prompt the user to input an amount in
pence. Your program should then work out what coins are the
least number needed for this amount of money. The possible coins
are £2, £1, 50p, 20p, 10p, 5p, 2p, 1p.
For example, the coins which are the least number for 397 pence
are:
1 x £2, 1 x £1, 1 x 50p, 2 x 20p, 1 x 5p, 1 x 2p
There could be a significant amount of repetition in parts of your
solution. Marks will be awarded for using some sort of data
structure (such as an array) to hold the denominations of coins
available and the number of coins used, to reduce the amount of
repetition.
Your program should repeatedly prompt the user for an amount of
pence. As long as a positive amount is typed in, the program
should calculate the number of coins; when a zero or negative
amount is typed in the program should halt.

I was set this task by a tutor some time ago and I just cannot get my head around it at all. In comparison to others I have completed it is seemingly relatively straightforward, but I am struggling to come up with a working solution. I have only been programming sparingly for around 4 or so months, and whilst I am comfortable with the fundamentals, I struggle with arrays and while loops which is exactly what I need in order to provide a functional solution.

If anybody could give me any pointers in the right direction or some suggestions of how I could tackle the problem it would be much appreciated. Thanks.

Some psuedo code to help you along

Get amount from user
while amount > 0
    for (int i = 0; i < length of coin value array; i++) 
        coinamount[i] = amout / coinvalue[i];
        amount = amount % coinvalue[i];
    end if
    Display the values in cointamount array
    Get new amount from user
end while

coinvalue holds the values of each coin (in pence) in decreasing order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.