Jump to content


Trouble with Arrays


5 replies to this topic

#1 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 01 March 2010 - 18:13

Trying to make a program that takes a number of pence from the user and then calculates the most efficient number of coins to make up that number.

£2, £1, 50p, 20p, 10p, 5p, 2p, 1p coins.

I tried to use an array but that doesn't seem to be working as the output is wayyyy messed up. Anyone got any ideas? It's C#.

using System;

namespace Calculator
{
	class Program
	{
		public static void Main(string[] args)
		{
			//Declare Variables.
			string value, myChoice;
			int pence;
			int[] change = new int [8] {200, 100, 50, 20, 10, 5, 2, 1};
		do
			{
			//Collect input from user.
			Console.WriteLine("Please input the amount in pence that you want to have calculated:\n");
			value = Console.ReadLine();
			pence = Convert.ToInt32(value);
			Console.Clear();
			Console.WriteLine("You entered {0}", pence);

				//The math.
				do
					{
						if(pence >= 200)
						{
							change[0] = pence / 200;
   							pence = pence % 200;
						}
						else if(pence >= 100)
						{
							change[1] = pence / 100;
   							pence = pence % 100;
						}
						else if(pence >= 50)
						{
							change[2] = pence / 50;
   							pence = pence % 50;
						}
						else if(pence >= 20)
						{
							change[3] = pence / 20;
   							pence = pence % 20;
						}
						else if(pence >= 10)
						{
							change[4] = pence / 10;
   							pence = pence % 10;
						}
						else if(pence >= 5)
						{
							change[5] = pence / 5;
   							pence = pence % 5;
						}
						else if(pence >= 2)
						{
							change[6] = pence / 2;
   							pence = pence % 2;
						}
						else if(pence >= 1)
						{
							change[7] = pence / 1;
   							pence = pence % 1;				 
						}
					} while (pence > 0);
			
				Console.WriteLine("The minimum amount of change possible for that sum of money");
				Console.WriteLine("would be the following combination of coins: \n");
			
				//Output end values only if greater than 0.
				if(change[0] > 0)
				{
					Console.WriteLine("{0} x £2 coins.", change[0]);
				}
				if(change[1] > 0)
				{
					Console.WriteLine("{0} x £1 coins.", change[1]);
				}
				if(change[2] > 0)
				{
					Console.WriteLine("{0} x 50p coins.", change[2]);
				}
			
				if(change[3] > 0)
				{
					Console.WriteLine("{0} x 20p coins.", change[3]);
				}
				if(change[4] > 0)
				{
					Console.WriteLine("{0} x 10p coins.", change[4]);
				}
				if(change[5] > 0)
				{
					Console.WriteLine("{0} x 5p coins.", change[5]);
				}
				if(change[6] > 0)
				{
					Console.WriteLine("{0} x 2p coins.", change[6]);
				}
				if(change[7] >0)
				{
					Console.WriteLine("{0} x 1p coins.", change[7]);
				}
				
			Console.ReadLine();
			Console.Clear();
			Console.WriteLine("Would you like to quit?\n(Y/N)\n");
			myChoice = Console.ReadLine();
			
			//Does user want to quit?
			switch(myChoice)
			{
				case "Y":
				case "y":
					Console.WriteLine("\nProgram will close.");
					Console.ReadLine();
					break;
				case "N":
				case "n":
					Console.WriteLine("\nProgram will restart.");
					Console.ReadLine();
					Console.Clear();
					break;
			}
			
			} while (myChoice != "Y" && myChoice != "y");

		}
	}
}


EDIT:

If it helps, here is the working program, before I tried to implement the array.

using System;

namespace Calculator
{
	class Program
	{
		public static void Main(string[] args)
		{
			//Declare Variables.
			string value, myChoice;
			int change, pound2 = 0, pound1 = 0, p50 = 0, p20 = 0, p10 = 0, p5 = 0, p2 = 0, p1 = 0;
		do
			{
			//Collect input from user.
			Console.WriteLine("Please input the amount in pence that you want to have calculated:\n");
			value = Console.ReadLine();
			change = Convert.ToInt32(value);
			Console.Clear();
			Console.WriteLine("You entered {0}", change);

				//The math.
				do
					{
						if(change >= 200)
						{
   							pound2 = change / 200;
   							change = change % 200;
						}
						else if(change >= 100)
						{
   							pound1 = change / 100;
   							change = change % 100;
						}
						else if(change >= 50)
						{
   							p50 = change / 50;
   							change = change % 50;
						}
						else if(change >= 20)
						{
							  p20 = change / 20;
   							change = change % 20;
						}
						else if(change >= 10)
						{
   							p10 = change / 10;
   							change = change % 10;
						}
						else if(change >= 5)
						{
   							p5 = change / 5;
   							change = change % 5;
						}
						else if(change >= 2)
						{
   							p2 = change / 2;
   							change = change % 2;
						}
						else if(change >= 1)
						{
   							p1 = change / 1;
   							change = change % 1;				 
						}
					} while (change > 0);
			
				Console.WriteLine("The minimum amount of change possible for that sum of money");
				Console.WriteLine("would be the following combination of coins: \n");
			
				//Output end values only if greater than 0.
				if(pound2 > 0)
				{
					Console.WriteLine("{0} x £2 coins.", pound2);
				}
				if(pound1 > 0)
				{
					Console.WriteLine("{0} x £1 coins.", pound1);
				}
				if(p50 > 0)
				{
					Console.WriteLine("{0} x 50p coins.", p50);
				}
			
				if(p20 > 0)
				{
					Console.WriteLine("{0} x 20p coins.", p20);
				}
				if(p10 > 0)
				{
					Console.WriteLine("{0} x 10p coins.", p10);
				}
				if(p5 > 0)
				{
					Console.WriteLine("{0} x 5p coins.", p5);
				}
				if(p2 > 0)
				{
					Console.WriteLine("{0} x 2p coins.", p2);
				}
				if(p1 >0)
				{
					Console.WriteLine("{0} x 1p coins.", p1);
				}
				
			Console.ReadLine();
			Console.Clear();
			Console.WriteLine("Would you like to quit?\n(Y/N)\n");
			myChoice = Console.ReadLine();
			
			//Does user want to quit?
			switch(myChoice)
			{
				case "Y":
				case "y":
					Console.WriteLine("\nProgram will close.");
					Console.ReadLine();
					break;
				case "N":
				case "n":
					Console.WriteLine("\nProgram will restart.");
					Console.ReadLine();
					Console.Clear();
					break;
			}
			
			} while (myChoice != "Y" && myChoice != "y");

		}
	}
}

Edited by W!, 01 March 2010 - 18:18.


Posted Image

#2 Dauth

    <Custom title available>

  • Gold Member
  • 11193 posts

Posted 01 March 2010 - 18:18

Do you need to End your if statements? Using Endif or something similar?

BTW I expect one of the moos to shift this to the code block.

#3 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 01 March 2010 - 18:24

Sorted now, sorry.

int&#91;&#93; change = new int &#91;8&#93; {200, 100, 50, 20, 10, 5, 2, 1};


Should have been

int&#91;&#93; change = new int &#91;8&#93; {0, 0, 0, 0, 0, 0, 0, 0};


Didn't realise it assigns values, thought it was some kind of naming method.

Posted Image

#4 Libains

    Light up life.

  • Gold Member
  • 4950 posts

Posted 01 March 2010 - 18:29

Yer bloody numpty. Topic moved btw.
For there can be no death without life.

#5 Warbz

    IRC is just a multiplayer notepad.

  • Project Team
  • 4646 posts

Posted 01 March 2010 - 18:31

View PostAJ, on 1 Mar 2010, 18:29, said:

Yer bloody numpty. Topic moved btw.


Now I have the problem of the array keeping the previous values each time the program loops. |:

EDIT: Fixed again. Added:
Array.Clear&#40;change, 0, Math.Min&#40;8, change.Length&#41;&#41;;


To the end.

Edited by W!, 01 March 2010 - 18:46.


Posted Image

#6 BeefJeRKy

    Formerly known as Scopejim

  • Gold Member
  • 5114 posts
  • Projects: Life

Posted 01 March 2010 - 19:06

Try assigning the values inside one of the do while loops. Also this is the program I've seen that makes full use of a 'do while' loop. I normally use either 'while' or 'for' loops.
Posted Image



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users