Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic & other operations

Similar presentations


Presentation on theme: "Arithmetic & other operations"— Presentation transcript:

1 Arithmetic & other operations
A Lecture for the c++ Course Each slide has its own narration in an audio file. For the explanation of any slide, click on the audio icon to start the narration. The Professor‘s C++Course by Linda W. Friedman is licensed under a  Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

2 Arithmetic Operators The built-in arithmetic operations are Addition +
Subtraction  Multiplication * Division / (is integer division if operators are integers) Modulus % (remainder) Arithmetic

3 Arithmetic Operators e.g., if the value of is to be assigned to variable x, it is coded: x = b + c - d * e / f; Parentheses may be used. These are evaluated first. e.g., x = (b + c - d)* e / f; is evaluated as: Arithmetic

4 order of operations 1. ( ) 2. * / % left to right Example. What is the order of operations in the following expression? z= p*r%q+w/x-y; Z = P * R % Q + W / X - Y ; 6 1 2 4 3 5 Arithmetic

5 Exercise For each of the following arithmetic expressions, construct the equivalent C++ expression. Arithmetic

6 Using Stored Data and Arithmetic
// from Hubbard book // Prints the sum, difference, etc. of given integers. #include <iostream> using namespace std; int main(){ int m = 6, n = 7; cout << "The integers are " << m << " and " << n << endl; cout << "Their sum is " << (m+n) << endl; cout << "Their difference is " << (m-n) << endl; cout << "Their product is " << (m*n) << endl; cout << "Their quotient is " << (m/n) << endl; cout << "Their remainder is " << (m%n) << endl << endl << endl; return 0; } Trace this program. Arithmetic

7 Assignment We will usually see an assignment operation in this format: <variable> = <expression> An expression is a combination of operators and operands. For example, c = c + 3; or average = sum / count; Arithmetic

8 Assignment c = c + 3; same as c +=3; The += operation adds the value of the expression of the right to the value of the variable on the left and stores the result in the variable on the left. In general, <var> = <var> op <exp>; can be written as: <var> op = <exp>; Examples: c = 4; //same as c = c  4; c *= 5; //same as c = c *5; c /= 6; //same as c = c /6; Can we reverse the order of the double operator? Say, c = 4; No. This simply is the same as the assignment c = 4; Arithmetic

9 Increment / Decrement Operators
a++; is the same as a = a + 1; and also the same as ++a; a; is the same as a = a  1; and also the same as a; a++ postincrement ++a preincrement a postdecrement a predecrement These are unary operators. They operate on only a single operand. The operators we are more familiar with are binary operands; they operate on two operands, much like the + operator in the expression a+b. The degree of an operator refers to the number of operands it takes. Arithmetic

10 Increment / Decrement Operators
Example: int c; c = 5; cout << c << endl; // outputs 5 cout << c++ << endl; // outputs 5 (then increments) cout << c << endl << endl; // outputs 6 cout << ++c << endl; // outputs 6 (after incrementing) cout << c << endl; // outputs 6 Arithmetic

11 Grade Point Average // This program will calculate grade point average. #include <iostream> using namespace std; int main(){ int A, B, C, D, F; float sum, GPA; cout << "Tell me your grades and I will calculate your GPA."; cout << endl << endl; cout << "How many units of A? "; cin >> A; cout << "How many units of B? "; cin >> B; cout << "How many units of C? "; cin >> C; cout << "How many units of D? "; cin >> D; cout << "How many units of F? "; cin >> F; sum = A + B + C + D + F; GPA = (4*A + 3*B + 2*C + D)/sum; cout << endl; cout << "Your grade point average is " << GPA <<endl; return 0; } Arithmetic

12 A Polynomial Program #include <iostream> using namespace std; int main(){ float A, B, C, X; cout << "A=? "; cin >> A; cout << "B=? "; cin >> B; cout << "C=? "; cin >> C; X = 5*A*A*B*C*C*C + 8*A*B*B*C*C - 4*B*B*B*C; cout << endl; cout << "X = " << X <<endl; return 0; } Why do we have to multiply a value by itself instead of just raising to a power with an exponentiation operator? Arithmetic

13 A Polynomial Program C++ has no arithmetic operator for exponentiation. There is, however, a power function that can do it for us. Use the math header file. // Polynomial Program using the pow() function #include <iostream> #include <math> using namespace std; int main(){ float A, B, C, X; cout << "A=? "; cin >> A; cout << "B=? "; cin >> B; cout << "C=? "; cin >> C; X = 5*pow(A,2)*B*pow(C,3) + 8*A*pow(B,2)*pow(C,2) - 4*pow(B,3)*C; cout << endl; cout << "X = " << X <<endl; return 0; } Arithmetic

14 Functions in the math library
To use these built-in functions we need to include the <math.h> header file function what it does returned value abs(a) absolute value of a same data type as argument pow(a1,a2) a1 raised to the power of a2 data type of argument a1 sqrt(a) square root of a sin(a) sine of a (a in radians) double cos(a) cosine tan(a) tangent log(a) natural logarithm of a log10(a) base 10 log of a exp(a) e raised to the power of a Arithmetic

15 Review What did we learn in this lecture? Plenty. Some terms to jog your memory: binary operator degree of an operator expression operand operator tertiary operator unary operator Arithmetic


Download ppt "Arithmetic & other operations"

Similar presentations


Ads by Google