As you learned in Chapter 1, the purpose of Visual C# 2005, and indeed programming languages generally, is to enable you, as the programmer, to give instructions to the computer to carry out. Before you can formulate those instructions in code, you first need to be able to articulate those instructions in English or whatever other language you think in.
To write the Change Machine project, you need to come up with a step-by-step logical procedure to convert the pile of pennies into neater stacks of dollars, quarters, dimes, nickels, and pennies. A step-by-step logical procedure for solving a problem is called an algorithm, pronounced "Al Gore rhythm."
One algorithm for converting the pile of pennies into dollars, quarters, dimes, nickels, and pennies is to first determine how many stacks of 100 pennies you can make from the pile. Each stack of 100 pennies would then represent one dollar. You then would work with the number of pennies left over to determine the number of quarters, dimes, nickels, and pennies.
For example, assume there are 392 pennies in the pile. You might use the following steps to determine the number of quarters, dimes, nickels, and pennies in 392 pennies:
There are 100 pennies in a dollar. You can make three stacks of 100 pennies from 392 pennies. That means there are three dollars, with 92 pennies left over, from which you will determine the number of quarters, dimes, nickels, and pennies.
There are 25 pennies in a quarter. You can make three stacks of 25 pennies from 92 pennies. That means there are three quarters, with 17 pennies left over, from which you will determine the number of dimes, nickels, and pennies.
There are ten pennies in a dime. You can make one stack of ten pennies from 17 pennies. That means there is one dime, with seven pennies left over, from which you will determine the number of nickels and pennies.
There are five pennies in a nickel. You can make one stack of five pennies from seven pennies. That means there is one nickel, with two pennies left over, which is the number of pennies.
Let's now convert this algorithm from English to code.
txtPennies into the int variable intLeftover. The following code does this, first using the Parse method of the Int32 structure (discussed in the earlier section "The Parse Method") to convert the string representation of an integer (the Text property of txtPennies) to the actual integer value before that value is assigned to the int variable (intLeftover):
intLeftover = Int32.Parse(txtPennies.Text);
When you divide the number of pennies (stored in intLeftover) by 100 (the number of pennies in a dollar), the quotient is the number of dollars in the pennies, and the remainder is the number of pennies left over. This division is integer division, because both intLeftover and 100 are integers, so it provides you with the quotient but no remainder. The % operator provides you with the remainder:
lblDollars.Text = (intLeftover / 100).ToString(); intLeftover = intLeftover % 100;
Note As explained in Chapter 3, the ToString method converts a number
(intLeftover / 100) into the string representation of that number so it can be displayed as text in the Label control.
The quotient, representing the number of dollars in the pile of pennies, is displayed in lblDollars. The remainder is stored in intLeftover, which will be used in the code to determine the number of quarters, dimes, nickels, and pennies.
Next, you follow the same procedure, with two differences. First, you are not dividing the total number of pennies, but instead the number of pennies left over, represented by the current value of the variable intLeftover. Second, you are not dividing by 100, but instead 25, the number of pennies in a quarter. We already have determined the number of dollars in the pile of pennies. Now we want to determine the number of quarters in the remaining pennies. Accordingly, the code reads as follows:
lblQuarters.Text = (intLeftover / 25).ToString(); intLeftover = intLeftover % 25
The remainder of the code follows the same process, except that next the divisor is 10, the number of pennies in a dime, then 5, the number of pennies in a nickel:
lblDimes.Text = (intLeftover / 10).ToString(); intLeftover = intLeftover % 10;
lblNickels.Text = (intLeftover / 5).ToString(); intLeftover = intLeftover % 5;
change, so there is no need for further division:
lblPennies.Text = intLeftover.ToString();
You frequently will need to create and implement algorithms in writing a computer program. Creating algorithms is a skill that can be developed from any field that requires analytical thinking, including but not limited to mathematics as well as computer programming.
Conclusion
Computers, in addition to being able to store vast amounts of data, can calculate far faster and more accurately than we can. You harness the computer's calculating ability using arithmetic operators. Most of the arithmetic operators, such as those for addition and multiplication, work the same as the arithmetic operators you use with pencil and paper. However, there are two division operators: one the familiar / operator, the other the modulus operator, %. The / operator reports the decimal equivalent of the quotient and remainder (as does the / operator you use with pencil and paper or a calculator) unless both operands (dividend and divisor) are a whole number data type such as an int, in which case it's called integer division and reports only the quotient. The % operator reports only the remainder.
In the next chapter, you will learn about relational and logical operators, which enable your program to take different actions depending on choices the user makes while the program is running.
Quiz
1. Which arithmetic operator works with string as well as numeric variables?
2. What is the significance of operator precedence?
3. How can you override default operator precedence?
4. Which operator increases the value of a numeric variable by one?
5. What is integer division?
6. Which operator provides only the remainder resulting from division?
7. Which operator has precedence, an arithmetic operator or the assignment operator?
8. What is the purpose of the Parse method of the Int32 class?
9. What is the purpose of the ToString method of the Int32 class?
10. What is a method of a class?
Answers
1. The addition operator works with string as well as numeric variables.
2. Operator precedence determines, when there are two or more arithmetic operators, which arithmetic operation is done first.
3. You can override default operator precedence with parentheses.
4. The increment (++) operator increases the value of a numeric variable by one.
5. Integer division is when both operands of division are a whole number data type when only the quotient is reported and any remainder is dropped.
6. The % (modulus) operator provides only the remainder resulting from division. 7. All arithmetic operators have precedence over the assignment operator.
8. The Parse method of the Int32 class converts the string representation of an integer into actual integer values.
9. The ToString method of the Int32 class converts an integer into its string representation. 10. A method is something an object of a class does.