ADVANCED PLACEMENT Computer Science - C++
STUDENT OUTLINE
Lesson 4 - Math Functions, Constants
INTRODUCTION: The C++ language provides some unusual operators called assignment operators. These shortcuts allow you to write shorter source code and they allow the compiler to create more efficient runtime code. Because of the abundance of operators in this language, the operator precedence table in C++ is a long one. We begin our study of operator precedence with the familiar math operators.
In keeping with the concept of recycling code, we will examine some of the functions available in math.h. A fundamental set of math functions are pre-defined in C++ and ready for use.
The key topics for this lesson are:
A. Standard Math Functions
B. Precedence of Math Operators
C. Assignment Operators
D. Increment Operators
E. Named Constants
VOCABULARY: PRECEDENCE ASSIGNMENT OPERATORS
INCREMENT OPERATOR DECREMENT OPERATOR
NAMED CONSTANTS
DISCUSSION: A. Standard Math Functions
See Handout H.A.4.1, Standard Functions in C++, a Partial List. |
1. C++ provides fundamental functions which are available in the libraries math.h and stdlib.h. To access these functions you will need to include the compiler directive #include <math.h> and/or #include <stdlib.h> at the top of your program. |
2. The syntax of a standard function is described in the function prototype as listed in your C++ reference manuals. For example, the power function raises a value x to the y power, xy.
double pow (double x, double y);
This function will accept two doubles and it returns xy as a double type. If either parameter (a value passed to the function) is a float or an integer, it will get promoted to a double. Here is a sample call of this function.
answer = pow (a,b);
B. Precedence of Math Operators
1. Precedence rules govern the order in which an expression is solved. For example:
2 + 3 * 6 = 20 The * operator has priority over +.
2. Associativity refers to the order in which operators are applied if they have the same precedence level. The two possibilities are from left-to-right or right-to-left.
3. The following table summarizes precedence and associativity of math operators:
Level of Precedence Operator Associativity
Highest unary- right to left
* / % left to right
Lowest + - left to right
4. An example follows:
span>9 + 5 * 7 % 8 - 5 (solve * first)
span> 9 + 35 % 8 - 5 (solve % next)
  ; 9 + 3 - 5 (solve left-to-right)
  ; 7
5. Parentheses take priority over all the math operators.
(5 + 6) / (9 - 7) = 11 / 2 = 5 (integer division is used here)
See Handout H.A.4.2, Operator Precedence in C++. |
6. C++ has a long list of operators for which there are 10 different levels of precedence. A partial list of the operators in C++ have been summarized in a handout, organized by precedence level. |
C. Assignment Operators
1. The statement number = number + 5; is an example of an accumulation statement. The old value of number is incremented by 5 and the new value is stored in number.
2. The above statement can be replaced as follows:
number += 5;
3. C++ provides for the following assignment operators:
+= -= *= /= %=
4. The following examples are equivalent statements:
rate *= 1.05; rate = rate * 1.05;
sum += 25; sum = sum + 25;
number %= 5; number = number % 5;
See Handout H.A.4.2, Operator Precedence in C++. |
5. The precedence of the assignment operators is the lowest of all operators.
|
D. Increment Operators
1. Incrementing or decrementing by one is a common task in programs. This can be solved by the statements:
n = n + 1; or n += 1;
2. C++ also provides a unary operator called an increment operator, ++.
3. The statement n = n + 1 can be rewritten as ++n. The following statements are equivalent:
n = n + 1; ++n;
sum = sum + 1; ++sum;
4. C++ also provides for a decrement operator, --, which decrements a value by one. The following are equivalent statements:
n = n - 1; --n;
sum = sum - 1; --sum;
5. The increment and decrement operator can be written as either a prefix or postfix unary operator. If the ++ is placed before the variable it is called a prefix increment operator (++number), but it can follow after the variable (number++), which is called a postfix increment operator. The following three statements have the same effect:
++number; number++; number = number + 1;
6. Before we look at the difference between prefix and postfix unary operators it is important to remember C++ operators solve problems and often return values. Just as the assignment operator (=) returns a value, the ++ and -- operators return values. Consider the following code fragments:
int a=1, b;
b = ++a;
After execution of the code b = ++a;
a =2 and b = 2 |
int a=1, b;
b = a++;
After execution of the code b = a++;
a =2 and b = 1 |
7. The statement b = ++a uses the preincrement operator. It increments the value of a and returns the new value of a.
8. The statement b = a++ uses the postincrement operator. It increments the value of a and returns the old value of a.
See Handout H.A.4.2, Operator Precedence in C++. |
9. The precedence and associativity of the unary increment and decrement operators is the same as the unary - operator.
|
E. Named Constants
1. C++ allows for declaration of named constants in a program. The general syntax is:
const type
ConstantName = Expression;
a. const is a C++ reserved word
b. type is any previously defined type
c. ConstantName is a valid C++ identifier
d. Expression is any valid expression of the appropriate type
2. Examples:
const int
DRIVINGAGE = 16;
const double
PI = 3.14159265,
TAXRATE = 0.0525;
3. Some programmers declare constant identifiers using all uppercase letters. This distinguishes such identifiers from variables. This curriculum guide will use uppercase identifiers for constant values.
4. The major benefit of using constant values occurs when such constants need to be modified. Suppose you have a program which uses a tax rate value in 20 different lines of code. If you had used the literal value of 0.0525, you would need to change the value in 20 different spots. However, if you had declared a constant identifier and used the identifier in those 20 spots, then one change at the top of the program would update the value throughout the program.
const double
TAXRATE = 0.0625;
All references to TAXRATE are now changed.
5. Constants are usually declared at the top of the source code, below the #include statements but outside of function main. This will allow such constants to be used globally throughout the source code. If a constant is declared inside of a function it can only be used within that function. The location of identifiers in a file will be discussed more in Lesson 6. For now, declare constants above and outside of function main.
SUMMARY/REVIEW: Your lab work will incorporate the material from Lessons 1-4. The lab on computing and printing taxes will require detailed usage of format manipulators. The worksheet will provide practice on operator precedence and assignment operators.
ASSIGNMENT: Worksheet W.A.4.1, Precedence and Assignment Operators
Lab Exercise, L.A.4.1, Taxes
Lab Exercise, L.A.4.2, Quadratic