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.

3 comments:

  1. Hi,Denis
    I may not understand very well this HW's spec and your thought because of my poor English skill...:p
    Anyway, I tried to write code using array.
    What do you think of this?
    -------------------------------------------------------
    int[] coins = { 50, 20, 10, 5 };
    int[] numbers = { 0, 0, 0, 0 };

    int amount = int.Parse(amountTextBox.Text);
    amount = amount % 100;

    for (int i = 0; i < coins.Length; i++)
    {
    numbers[i] = amount / coins[i];
    amount = amount % coins[i];
    resultTableLayoutPanel.Controls[i].Text =
    coins[i] + " cent \n" + numbers[i];
    }
    -------------------------------------------------------
    and, your design is beautiful! I like it :)
    Have a nice weekend.

    ReplyDelete
  2. Hi Misa,
    thanks for the comment. This makes sense now :) The code is clear and concise and works perfectly.
    However, some students might not be aware of how resultTableLayoutPanel.Controls[i].Text works.
    I will write another post about your code, if that is alright with you.

    ReplyDelete
  3. Of course that is alright! It is good idea. Thanks for help:D

    ReplyDelete