ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, 2 0 1 0 R E Z A S H A H I D I
Today s class Constants Assignment statement Parameters and calling functions Expressions Mixed precision arithmetic Type casting
Assignment statements An assignment statement gives a value to a variable. The left-hand side of an assignment statement must consist of only one variable. This basically means we can t have an implicit assignment, e.g. y+2 = 5; 7*x+3=10 General form of variable assignment is: variable = expression;
Assignment statements As we just saw, a general variable assignment is of the form: variable = expression;; This means that we should first evaluate the expression on the right, and then assign it to the variable on the left. The variable on the left of the assignment can appear on the right side of the assignment.
Example of variable on both sides of assignment We will see later that having a variable on both sides of an assignment statement is useful in control structures like for and while loops. If we wish to increment the value of a variable x, we have two options: int x=5; int y; y = x+1; We may not want to allocate a new variable just for this. Also may make the code more complicated than needed.
Example of variable on both sides of assignment Instead we can do the following: int x=5; x = x+1; In a mathematical context, this would be impossible. x =x + 1; has no solution! However in C++, this is valid. It means use the current value of x, add 1 to it, and then store the value back in the variable x. This updates the value of x with a value based on its current value.
Back to the drawer representation of variables For example, x=x+1 would take the value of x out of its drawer, and add 1 to it, and then put it back into its drawer again. 3 1 2 tmp deg x
Parameters as variables The parameters of a function can be thought of as variables. We already saw that functions must be declared before they are used, along with their parameters. These parameters are of a certain type. In order to make calling functions easier for the programmer, we can use different names for the variables that are passed as parameters, or just pass values.
Example Calling Functions different ways The parameters of a function are like variables. Consider a function that converts a height in feet and inches to centimetres. The prototype (declaration) of the function could be something like: double convertheight(int feet, int inches);
Example And the definition of the function could be: /* convertheight ***************************************** * * @params: feet - number of feet in person s height @pre >= 0 * inches number of inches in person s height @pre>= 0 * * @returns: number of centimetres in height. **************************************************** ****/ double convertheight(int feet, int inches) { int numinches; // number of inches in height double numcentimetres; // number of cm. in height numinches = 12*feet+ inches; numcentimetres = numinches*2.54; return numcentimetres; }
Example Calling the function We can call the function in different ways Assume that a person s height is 5 feet, 9 inches. Then to convert this height to centimetres, we could call convertheight in at least four different ways: 1. Call with constant values: double numcentimetres; numcentimetres = convertheight(5,9); This works, but may be tricky if we want to code many calls to convertheight.
Example Calling the Function 2. call with variable names (same as prototype): Notice here that the variables have the same name as in the definition of the function. int feet = 5; int inches = 9; double numcentimetres; numcentimetres = convertheight(feet,inches);
Example Calling the Function 3. Call with variable names (different than prototype): int numfeet = 5; int numinches = 9; double numcentimetres; numcentimetres=convertheight(numfeet,numinches );
Example Calling the Function 4. Call with variable names in the wrong order! This is wrong, but this shows a possible error that can happen if the parameters you call the function with are not descriptive: int feet = 5; int inches = 9; double numcentimetres; numcentimetres = convertheight(inches,feet);
Symbolic Constants A symbolic constant is a value that does not change and cannot change throughout a program. General form: const type VARIABLENAME = expression; Examples (from Lecture Notes): const int MONTHS = 12; const double PI = 4 * atan(1.0); const char YES = 'Y'; const int LONG_CONSTANT_NAME = 17; Good to use symbolic constants for values that are used many times and do not change during the program.
Expressions We have seen in variable declaration statements, where the value of the variable is initialized that the general form of the declaration is: type var = expression; We haven t yet explained what an expression is. They are somewhat similar to math expressions, but more general.
Expressions The definition of an expression is (from the Lecture notes): A combination of variables, constants, operators and functions which is progressively evaluated an operation at a time until it is reduced to a final value. An expression is always reduced to a single value (very important). It does not include C++ keywords!
Arithmetic Expressions An expression which uses arithmetic operators. There are different operators based on whether we are using integers or doubles. Operators can be either unary or binary. A unary operator only takes one argument. A binary operator takes two arguments. If we are using doubles, then the usual arithmetic operators that you have learned in math are valid. These are: + - * /
Arithmetic Expressions The operators we are allowed to use depend on the data type. For arithmetic expressions there are two data types that we consider in this course, int and double. For real numbers, we have the usual operators: + - * / + and can either be unary or binary operators. This is because we can have positive and negative numbers, e.g. +98.6 or -25. The unary + sign is strictly not necessary, however is included for consistency with the sign.
Arithmetic Expressions For integers, we have the usual operators, both unary and binary. We also have an extra operator: the % operator. This is the modulus operator. This is like the case for calculating times, e.g. 9 hours after 5 p.m. is 2 a.m. (5+9) % 12 = 2
Integer operators / and % Look at example in Lecture Slides on / and % operators. The / and % operators for int data types are like the results of long division: a / b is the quotient of a and b (always an integer) a % b is the remainder when a is divided by b (also always an integer) Remember that there is no double % operator!
Computer Arithmetic There are two types of number variables in C++ that we use in this course. These are int and double. The compiler knows how to do either integer arithmetic (only adding, multiplying... integers together), or floating point arithmetic (only adding, multiplying... single or double precision numbers together) If we wish to mix up operations with integers and doubles for example, then the compiler automatically converts from one type to the other.
Data type conversions Whenever there is at least one double in an arithmetic expression, all variables in that expression are converted to double precision. Converting from an int to double can be called upcasting, while converting from a double to int can be called downcasting (truncation). Converting from a double to an integer is not rounding! So for example, int x = 5.6; will set the value of x to 5 and not 6.
Type casting Sometimes we wish to force the compiler to switch from one type to the other (e.g. when mixing integers and floating points in an expression). This is called type casting. For example, let us say that we wish to automatically truncate the answer of a double precision floating point number. This can be done with the following piece of code, e.g.: int wholepart; float x = 3.6; wholepart = (int) x;
Example of automatic type conversion See the quadratic example in the Lecture notes.
Composite Assignments Shorthand for assignments like x = x+1; or x =x*9; See Lecture Notes