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");
}
}
}