Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

Friday, March 27, 2009

Monetary System - Version 2 - Using arrays & for loops - the proper way

Misa devised a brillant way to use arrays, loops and the TableLayoutPanel control to tackle the problem stated in the previous entry (version 1).  


But first, let's understand one important design concept, the TableLayoutPanel control.
We are used to display informations using labels, but we also found out the limitations of this control. With labels, it is very difficult to predict the display of the string or text being output. For example, a label might contain multiple lines of text (we might use '\n' for new lines, or '\t' for tabs).

With TableLayoutPanel, the organisation of any controls including labels, is made easier.
The TableLayoutPanel is just a series of rows and columns, analogous to a table.

One of the main advantage of using the TableLayoutPanel is that you can refer to each control in  a cell, just like an array. An array of controls, found in the Table. We will use that concept in this code.


For example, you might want to contain a list of Coins you have, and the number of coins, each represented on a different row, with separate columns for each category. You can do it with labels only, but the best design solution is to use the TableLayoutPanel with labels.

 

In this scenario, Coins are on the top row, whereas the numbers are below the respective coins, bottom row.  We create the table TableLayoutPanel1.


In design view, the table is set as follows, with 4 normal labels, one in each cell.

Now the nice part. You can actually refer to the labels in an 'array-like' fashion. For example, 

TableLayoutPanel1.Controls[0] will refer to label2.
TableLayoutPanel1.Controls[1] will refer to label3.
TableLayoutPanel1.Controls[2] will refer to label4.
TableLayoutPanel1.Controls[3] will refer to label5.
So this is why we should be using arrays and loops in this question. 
This is the end of the explanation on why we should use the TableLayoutPanel control. 


This is the final code for the home work.

   1:  int[] coins = { 50, 20, 10, 5 };
   2:  int[] numbers = { 0, 0, 0, 0 };
   3:  int iAmount = int.Parse(txtAmount.Text);
   4:  int iCoinsNum = 0;  //stores the number of coins used
   5:   
   6:  iAmount %= 100; //Get the 2 last digits
   7:   
   8:  for (int i = 0; i < coins.Length; i++)
   9:  {
  10:      numbers[i] = iAmount / coins[i];    //gets the number of particular coin in amount
  11:      iAmount = iAmount % coins[i];       //gets the remainder
  12:      tableLayoutPanel1.Controls[i].Text = coins[i] + " cent \n" + numbers[i];
  13:      //display the coins value and amount, in each label per column in the TableLayoutPanel
  14:      iCoinsNum += numbers[i];            //accumulates the amount of coins
  15:  }
  16:   
  17:  if (iCoinsNum == 0) //check if any coin was required
  18:  {
  19:      btnReset_Click(sender, e);
  20:      lblCoinsNum.Text = "No coins required.";
  21:  }
  22:  else
  23:  {
  24:      lblCoinsNum.Text = "" + iCoinsNum + " coins required.";
  25:  }



Note: The order of the Coins array is important, as you want to divide the 50c coins first before moving to the 20c coins, and so on.


Screenshots of design and runtime application

Thursday, March 26, 2009

Monetary System - Can someone tell me why we should be using arrays for this?


Assume your monetary system only has the following coins: 50 cents, 20 cents, 10 cents,
and 5 cents. One dollar is equal to 100 cents.
Write a program to display the number and type of coins required to make up the amount
of money given by the user. The amount must be in cents, and must be an integer. Print
the number and type of coins required to make up the amount of money using the
smallest possible number of coins. The output lists types of coins only if at least one coin
of that is required.

Example:

If the input is 70
The output is:
2 coins required:
Fifty Cents Twenty Cents Ten Cents Five Cents
1 1 0 0
If the input is a multiple of 100 cents
The output is:
No coins required

If the input is 170
The output is:
2 coins required:
Fifty Cents Twenty Cents Ten Cents Five Cents
1 1 0 0


I have been pondering on this question for a long time, not because it is difficult. I was trying to figure out how arrays can make my life easier, but I couldn't find the answer. Anyone has an idea?

The first version I made (version 1) does not use arrays at all. Check the screenshot below:

Code Discussion
The main points to be addressed in C# in this question are:
  1. We need to get the TENS and UNITS from the value entered by the user. For example, we need to extract '87' from '187', or '6' from '106'.
  2. We need to keep count of 50's, 20's, 10's and 5's.
  3. Display a summary of the number of coins required.

We need to get the TENS and UNITS from the value entered by the user. For example, we need to extract '87' from '187', or '6' from '106'.
The logic is straight-forward. You need to check the number of digits in the string. We make use of the Length() method.
For example, if the length of the string is 2, it means that the value entered may look like "99", which does not need any validation.
However, "1024" needs to be addressed. The length for this string is 4. We need the last two elements. I used the Substring() function to perform the appropriate string manipulation.

iVal (int) will store the tens and units from the value entered by the user.
sVal (string) will store the text value entered by the user.
iLengthAmount is the length of the string value entered by the user.
e.g. int iLengthAmount = txtValueEnteredByUserBox.Length;


if (iLengthAmount > 2)
{
iLengthAmount = iLengthAmount - 2;
iVal = int.Parse(sVal.Substring(iLengthAmount, 2));//Discard Hundreds,
keeping Units and Tens

}


Note: I will continue on this post at a later time in the evening.

Update: 27/03
Misa, thanks for the solution in your comment. Your proposed solution is awesome and I think that is what is required from us by Allan. You definitely made it simpler. I will discontinue on this post, and create a new entry for this question. If anyone needs the complete code or wants to know more about this code algorithm/logic, just post a message.