ENGI 1020 - Introduction to Computer Programming M A Y 2 8, 2 0 1 0 R E Z A S H A H I D I
Last class Last class we talked about the following topics: Constants Assignment statements Parameters and calling functions Expressions Mixed precision arithmetic Type casting
Some course administration Assignment 2 will be up tonight! Pair programming details make sure that both students are working on the assignments! Raju will be enforcing this!
A bit more on integer and double precision arithmetic In integer arithmetic, all operands are int types, and the output is always an int type. In double precision arithmetic, all operands are double types, and the output is always a double type. Recall that double precision arithmetic is used even if one of the operands is a double. So for example in the expression 2.5*x, double precision will be used even if x is of type int. See quadratic function example in Lecture Slides.
Rounding vs. Truncation See example in Lecture Slides. Truncation is integer part of x. Rounding can be the integer part of x plus 1. /** truncate ******************************* * * @params: x - any double * * @returns: truncated integer version of x *******************************************/ int truncate (double x){ return x; }
Rounding example /** round ******************************* * * @params: x - any double * * @returns: rounded integer version of x *******************************************/ int round (double x){ return (x+0.5); }
Composite Assignment Operators These are shorthand for assignment operators where the same variable appears on both the left-hand side and the right-hand side. E.g. instead of x=x+5, we would write x+= 5. Similarly holds for the other binary operators, subtraction, multiplication and division. These are assignments and not equations, otherwise they would have no solution! Later we see there are special shorthand notations for x=x+1 and x =x-1.
Order of evaluation in compound expressions Order of evaluation: Expressions in parentheses, (), are always evaluated first, e.g. (5+3)*7. Then the order of operations is as follows: unary -,+ Highest (evaluated first) *, /, % -, + Lowest (evaluated last) See example in Lecture Notes
Library functions Libraries are functions that have already been written and built. So often we do not have to write our own functions, which makes code easier to understand and write. Many common functions already included in libraries. Need to use the #include directive to include the appropriate header file.
Library functions Header files contain the declarations needed to use the libraries. Math library has declarations for: Trigonometric functions e.g. double sin(double x), double tan(double x). Hyperbolic functions. e.g. double atan(double x) The power function which has to be used since C++ has no exponentiation operator: double pow(double x, double exp) The special function double atan2(double y, double x) which returns the arc tangent of y/x, a function which is defined for x = 0.
Library Functions To use any of these functions you would add to the top of your file the line #include <cmath> This is an include directive and the # sign means that this is processed by the precompiler. Recall our previous discussion on this.
Calling functions vs. implementing functions Thus far in the course, we have been implementing functions, e.g. in the assignments. When we use libraries, we are in fact calling functions that have already been implemented. Now we become a client (using a service), instead of providing a service for somebody else. We can call functions inside of one another (see polar/rectangular coordinates example in Lecture notes)
E.g. Dot product and Cross product Dot (scalar) product between two vectors a and b is a b. Cross product between two vectors a and b is a x b. The magnitude of a b is ab cos(theta). The magnitude of axb is ab sin(theta). How would we code functions to compute these magnitudes?
Next class Start control structures, where the flow of the program can change. Up to now, we have had a main function where statements are executed in sequence, possibly with calls to functions. Next, we will look at executing examples where some statements will be executed conditionally. We will also look at how we can repeat certain statements more than once, etc.