Hello,

I am trying to write a C# program that will calculate the minimum number of coins need from X amount of pence entered by the user in the form of £2, £1 etc. I have come up with this so far (Think its right), but how would i print out the results in "resultNumbersOfEachCoin" so the user can see which coins they need?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Coin_Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] britishCoinValues = new int[] { 1, 2, 5, 10, 20, 50, 100, 200 };
            int useramountInPence;
            int indexOfCurrentCoinToCheck;
            int numberOfThisCoin;

            int[] resultNumbersOfEachCoin = new int[8];

            Console.WriteLine("Enter the amount in pence");
            useramountInPence = Convert.ToInt32(Console.ReadLine());

            int remainingAmountToAllocateToCoins = useramountInPence;

            for (indexOfCurrentCoinToCheck = 7; indexOfCurrentCoinToCheck >= 0; indexOfCurrentCoinToCheck--) 
            {
                numberOfThisCoin = remainingAmountToAllocateToCoins / britishCoinValues[indexOfCurrentCoinToCheck];

                resultNumbersOfEachCoin[indexOfCurrentCoinToCheck] = numberOfThisCoin;

                remainingAmountToAllocateToCoins -= (numberOfThisCoin * britishCoinValues[indexOfCurrentCoinToCheck]);

                if (remainingAmountToAllocateToCoins == 0) break;
            }
        }
    }
}

Any help REALLy appreciated.

Recommended Answers

All 10 Replies

Maybe something like this? Correct me if there are any mistakes ;)

for (indexOfCurrentCoinToCheck = 0; indexOfCurrentCoinToCheck < 7; indexOfCurrentCoinToCheck++)
    {
       if (resultNumbersOfEachCoin[indexOfCurrentCoinToCheck] != 0)
           Console.WriteLine("You Will need {0} coins of ammount {1}", resultNumbersOfEachCoin[indexOfCurrentCoinToCheck], britishCoinValues[indexOfCurrentCoinToCheck]);
     }

OMG! Thanks it worked. Thought you had to use a for statement but didnt know how to implement it.

Thanks to the end of the earth :-).

Thanks again, just one thing, although not major. How would i go about formating this so the output for 200p and 100p would appear as £2 and £1 respectivly. For example if i typed in 150 as the input it would out put as needing:

You Will need 1 coins of ammount 50p
You Will need 1 coins of ammount £1 //instead of 100p

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Coin_Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] britishCoinValues = new int[] { 1, 2, 5, 10, 20, 50, 100, 200 };
            int useramountInPence;
            int indexOfCurrentCoinToCheck;
            int numberOfThisCoin;

            int[] resultNumbersOfEachCoin = new int[8];

            Console.WriteLine("Enter the amount in pence");
            useramountInPence = Convert.ToInt32(Console.ReadLine());

            int remainingAmountToAllocateToCoins = useramountInPence;

            for (indexOfCurrentCoinToCheck = 7; indexOfCurrentCoinToCheck >= 0; indexOfCurrentCoinToCheck--) 
            {
                numberOfThisCoin = remainingAmountToAllocateToCoins / britishCoinValues[indexOfCurrentCoinToCheck];

                resultNumbersOfEachCoin[indexOfCurrentCoinToCheck] = numberOfThisCoin;

                remainingAmountToAllocateToCoins -= (numberOfThisCoin * britishCoinValues[indexOfCurrentCoinToCheck]);

                if (remainingAmountToAllocateToCoins == 0) break;
            }

            for (indexOfCurrentCoinToCheck = 0; indexOfCurrentCoinToCheck < 7; indexOfCurrentCoinToCheck++)
            {
                if (resultNumbersOfEachCoin[indexOfCurrentCoinToCheck] != 0)
                    Console.WriteLine("You Will need {0} coins of ammount {1}p", resultNumbersOfEachCoin[indexOfCurrentCoinToCheck], britishCoinValues[indexOfCurrentCoinToCheck]);
            }
            Console.ReadLine();
        }
    }
}

Thanks if you or anyone else can help again.

I have come across a problem when testing it. For some reason it only calculates the input upto 200 after which it just calculates the last two digits of the input.

Anyone got any ideas.

EDIT:: Fixed this problem, as it was only counting upto the max array value i added 32183 to the array so it will be able to count to it.

Hello, sorry I can't help you - In fact I find myself asking you for advice.
This may be obvious but I wondered what 'int indexOfCurrentCoinToCheck' is representing/being used for.

Also wondered if you had come up with any ideas for outputting 100/200 pence as £1/£2 - I suppose separating int[] britishCoinValues, into two variables (for pounds and pence)?

Thanks ;)

Could you possibly post the final script?
Im trying to utilize this type of script to deliver a minimum number of stamps required to reach a total postage cost, given different denominations of stamps (not using the greedy method). Much appreciated if you could

try:

if (resultNumbersOfEachCoin[indexOfCurrentCoinToCheck] >= 100)
{
Console.WriteLine(resultNumbersOfEachCoin[indexOfCurrentCoinToCheck]/100);
}

You only need to divide by 100 if the value is of 100 or greater.. in this case 100 or 200

P.S. if it fixes your problem, please mark this post as SOLVED.

tnx

No, I'm looking for input value of 605 and keep getting 'the index was outside the size of the array'

I do appreciate your help but I've not used C# in 8 years since university. Tried changing all int to long but no joy (Microsoft C# 2010).

I need to replicate the dynamic coin solution shown at http://www.exorithm.com/algorithm/view/coin_change - In my case an example uses change of 605 and denominations 5, 60, 120 and 180 - solution reads 180 x 3, 60 x 1, and 1 x 5.
These are Australian stamp denominations and I'm trying to sort out a solution for determining minimum stamps req. for various postage costs.

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.