Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 2 Array Split Amount Question

Tags:

c#

My question is i have a certain money amount, lets say 552. I wish to split that in its coins/bills => So the result would for example be 1x 500 1x 50 1x 2

I have made 2 arrays for this:

double[] CoinValue = {500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02,  0.01};
  uint[] CoinAmount = new uint[CoinValue.Length];

My problem is how exactly do i "tell" the array the value for 500 should be 1 in the countAmount array.=> 1. So if i have 1000 the array CoinAmount array would know it would need to hold 2 as value(2x500 = 1000).

So my end result would be something like this, giving the amount of coin/bills: 1 x 500 1 x 50 1 x 2 .......

Thanks in advance.

like image 508
Sefran Avatar asked Mar 18 '26 14:03

Sefran


1 Answers

Don't use doubles if you want exact answers. Use either decimals or integer arithmetic (by converting to cents).

I'm not going to provide full source code as this looks like homework or a learning exercise, so instead I'll just give a few hints.

To find out how many notes of a certain denomination you need, use division:

int number = (int)(total / sizeOfBill);

Start with the largest bills and work downwards to the smallest to get a small number of notes/coins, otherwise you could end up with thousands of cent coins instead of a few bills.

like image 194
Mark Byers Avatar answered Mar 20 '26 04:03

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!