Hi guys,

im trying to get this code to work, im basically trying to write a programme that will find the smallest possible amount of coins needed to make up the amount of pence entered into a text box.

ive managed to get it working with a very long If elce statement block, but im trying to make it as efficient as possible, the code below compiles but returns blank values in the coinsamount array, when i attemot to output them to a list box.

any help would be appriciated :-)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Task_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            
            InitializeComponent();
        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            int i, ipt, pence;

            ipt = Convert.ToInt32(txtIpt.Text);

            i = 0;
            
            pence = ipt;

            int[] coins;
            coins = new int[8];
            
            coins[0] = 200;
            coins[1] = 100;
            coins[2] = 50;
            coins[3] = 20;
            coins[4] = 10;
            coins[5] = 5;
            coins[6] = 2;
            coins[7] = 1;

            int[] coinsamount;
            coinsamount = new int[8];

            coinsamount[0] = 0;
            coinsamount[1] = 0;
            coinsamount[2] = 0;
            coinsamount[3] = 0;
            coinsamount[4] = 0;
            coinsamount[5] = 0;
            coinsamount[6] = 0;
            coinsamount[7] = 0;

            for (i = 0; i == 8; ++i)
            {
                coinsamount[i] = pence / coins[i];

                pence = pence % coins[i];
                
            }

                        

                lstOutput.Items.Add("You need " + coinsamount[0] + " x £2 Coins");
                lstOutput.Items.Add("You need " + coinsamount[1] + " x £1 Coins");
                lstOutput.Items.Add("You need " + coinsamount[2] + " x 50p Coins");
                lstOutput.Items.Add("You need " + coinsamount[3] + " x 20p Coins");
                lstOutput.Items.Add("You need " + coinsamount[4] + " x 10p Coins");
                lstOutput.Items.Add("You need " + coinsamount[5] + " x 5p Coins");
                lstOutput.Items.Add("You need " + coinsamount[6] + " x 2p Coins");
                lstOutput.Items.Add("You need " + coinsamount[7] + " x 1p Coins");
                
            
         }
    }
}
for (i = 0; i == 8; ++i)
            {
                coinsamount[i] = pence / coins[i];

                pence = pence % coins[i];
                
            }

Think of the for loop as a while loop

it starts as i=0
then while i==8
it i++

so

now you should see where it goes wrong

Thanks for your help lizr :-) makes very good sence and i have changed the code to below - the programme compiles, but nothing is displayed in the list box (allmost seems to crash) and i get a exception notification that the "index was outside the bounds of the array" - i dont understand this as (as far as i understand!) i have declared the array size to be up to 8 and the loop should stop on 8?

for (i = 0; i <= 8; ++i)
            {
                coinsamount[i] = pence / coins[i];

                pence = pence % coins[i];
                
            }

wait i got it!! i forgot to take into account that the loop starts at 0 not 1!
your a star thank you so much!

No worries. Im glad you fixed it. Plus, you worked out why it wasnt working I didnt tell you why not, good for you! :)

This question has already been answered. Start a new discussion instead.