IXS1124-PROBLEM SOLVING ALGORITHM ARITHMETIC OPERATORS
1
PROGRAM STRUCTURE • General format // Introductory comments // file name, programmer, when writtten or modified // what program does #include void main () { constant declarations variable declarations executable statements } 2
ARITHMETIC OPERATORS • Arithmetic Operators Operation
Operator
Subtraction
-
Addition
+
Multiplication
*
Division
/
Modulus Division
%
Decrement by 1
--
Increment by 1
++
3
ARITHMETIC OPERATORS • Unary Operators – Operators that have only one operand – Example: -5, +27
• Binary Operators – Operators that have two operands – Example: 8-7, 3+4
4
ARITHMETIC OPERATORS Arithmetic Expression
Result
Description
2+5
7
13 + 89
102
34 - 20
14
35 - 90
-45
2*7
14
5/2
2
34 % 5
4
-34 % 5
-4
34 % -5
4
-34 % -5
-4
4%6
4
Quotient = 2, Remainder = 1 Quotient = 6, Remainder = 4 Quotient = -6, Remainder = -4 Quotient = -6, Remainder = 4 Quotient = 6, Remainder = -4 Quotient = 0, Remainder = 4
5
ARITHMETIC OPERATORS • Example 1 #include void main () { cout cout cout cout cout cout cout cout cout cout cout cout }
<< << << << << << << << << << << <<
“2 + 5 = ”<<2 + 5<<endl; “13 + 89 = ”<<13 + 89<<endl; “34 - 20 = ”<<34 - 20<<endl; “45 - 90 = ”<<45 - 90<<endl; “2 * 7 = ”<<2 * 7<<endl; “5 / 2 = ”<<5 / 2<<endl; “14 / 7 = ”<<14 / 7<<endl; “34 % 5 = ”<<34 % 5<<endl; “-34 % 5 = ”<<-34 % 5<<endl; “34 % -5 = ”<<34 % -5<<endl; “-34 % -5 = ”<<-34 % -5<<endl; “4 % 6 = ”<<4 % 6<<endl; 6
ARITHMETIC OPERATORS • Sample run 2+5=7 13 + 89 = 102 34 – 20 = 14 45 -90 = -45 2 * 7 = 14 5/2=2 14 / 7 = 2 34 % 5 = 4 -34 % 5 = -4 34 % -5 = 4 -34 % -5 = -4 4%6=4
7
ARITHMETIC OPERATORS • Example 2 #include void main () { cout << cout << cout << cout << cout << cout << cout <<
“5.0 + 3.5 = ”<<5.0 + 3.5<<endl; “3.0 + 9.4 = ”<<3.0 + 9.4<<endl; “16.3 – 5.2 = ”<<16.3 – 5.2<<endl; “4.2 * 2.5 = ”<<4.2 * 2.5<<endl; “5.0 / 2.0 = ”<<5.0 / 2.0<<endl; “34.5 / 6.0 = ”<<34.5 / 6.0<<endl; “34.5 / 6.5 = ”<<34.5 / 6.5<<endl;
} 8
ARITHMETIC OPERATORS • Sample run 5.0 + 3.5 = 8.5 3.0 + 9.4 = 12.4 16.3 – 5.2 = 11.1 4.2 * 2.5 = 10.5 5.0 / 2.0 = 2.5 34.5 / 6.0 = 5.75 34.5 / 6.5 = 5.30769
9
ARITHMETIC OPERATORS • Rules – If both operands are integers or floating point-value, the result of the operation will be an integer or floating-point value – If the operator has both type of operands (that is one is integer, the other one is floating-point number), the integer is changed to floating-point number with the decimal part of zero and the result is a floating-point number 10
ARITHMETIC OPERATORS • Rules – The division of two integers will yield an integer result (the fractional part will be truncated) • Example: 7 / 3 = 2 – Modulus Operator: calculates the remainder of an integer division • Example: 7 % 2 = 1
11
ORDER of PRECEDENCE • Order of Precedence Order of Precedence ()
Description
+-
brackets unary plus, unary minus multiply, divide,
*/%
modulus
+-
add, substract
12
ORDER of PRECEDENCE • Example 1 3 * 7 – 6 + 2 *5 / 4 + 6 Means the following: ( (3 * 7) – 6) + ( (2 * 5) / 4) ) + 6 = ( (21 – 6) + (10 / 4) ) + 6 (Evaluate *) = ( (21 – 6) + 2) + 6 (Evaluate /) = (15 + 2) + 6 (Evaluate -) = 17 + 6 (Evaluate +) = 23 (Evaluate +)
13
ORDER of PRECEDENCE • Example 2 #include void main () { cout cout cout cout
<< “3 / 2 + 5.5 = ”<<3 / 2 + 5.5<<endl; << “15.6 / 2 + 5 = ”<<15.6 / 2 + 5<<endl; << “4 + 5 / 2.0 = ”<<4 + 5 / 2.0<<endl; << “4 * 3 + 7 / 5 – 25.5 = ” << 4 * 3 + 7 / 5 – 25.5 <<endl;
} Sample Run: 3 / 2 + 5.5 = 6.5 15.6 / 2 + 5 = 12.8 4 + 5 / 2.0 = 6.5 4 * 3 + 7 / 5 – 25.5 = -12.5
14
PARENTHESES • Parentheses – Used to specify logical grouping of operands – Clarify the order of evaluation for operators in expressions – Example: • if (a < b < c) interpreted as (a < b) < c, not as (a < b) && (b < c) • if (a & b < 8) interpreted as a & (b < 8), not as (a & b) < 8 • int i = a >= b && c < d && e + f <= g + h 15
ASSIGNMENT STATEMENTS • Assignment Statements – Assign a value to a variable – Example: int num; num = 45; – A single = sign is used for assignment – It does not mean “equal” as in algebra (C++ uses ==, two equals sign) – Executed from right to left – First, the value on the right hard side is figured out (number 45) and then copied into the variable on the left hand side (the variable num)
16
ASSIGNMENT STATEMENTS • Assignment Statements – The left hand side needs to be a variable, something that can be assigned a value – The right hand side can be an expression, anything that evaluates to a reasonable value – Example int num, answer, temp, value; num = 45; answer = num + 1; temp = num; value = 2 * num + answer;
17
ASSIGNMENT STATEMENTS • Assignment Statements – Also be used to put character data into character variables – Literal character value must be written in single quotes around it, whereas a literal integer/ floating point is written without quote – Example char ch, save; ch = ‘T’; save = ch; 18
INCREMENT/DECREMENT OPERATOR • Increment and Decrement Operator – Some operations occur frequently in writing assignment statements – Example: • n = n + 1; • n = n - 1; – Increment operator ++ and decrement operator -– Example • n++ is equal as n = n + 1; • n-- is equal as n = n – 1; – Postincrement/postdecrement
19
INCREMENT/DECREMENT OPERATOR • Increment and Decrement Operator – Preincrement/predecrement • ++n, --n – Both versions of the increments and decrement operators have a side effect which means that they are not equivalent in all cases – Example n = 3:
n = 3:
cout<
cout<<++n; cout<
INCREMENT/DECREMENT OPERATOR • Increment and Decrement Operator – Example int item, left, right; left = 5 * --item + right; cout<
21
COMPOUND ASSIGNMENT • Compound Assignment – Another common assignments sum = sum + x; a variable, sum, is increased by some amount and the result assigned back to the original variable – Can be represented as sum += x; – Can be used with arithmetic operators +, -, *, / and % 22
COMPOUND ASSIGNMENT • Compound Assignment – Example Operato r
Description
*=
Multiply the value of the first operand by the value of the second operand ; store the result in the object specified by the first operand
/=
Divide the value of the first operand by the value of the second operand ; store the result in the object specified by the first operand
%=
Take modulus of the first operand specified by the value of the second operand ; store the result in the object specified by the first operand
+=
Add the value of the first operand by the value of the second operand ; store the result in the object specified by the first operand
-=
Subtract the value of the first operand by the value of the second operand ; store the result in the object specified by the first operand
23
COMPOUND ASSIGNMENT • Compound Assignment – Example Symbol
Normal Assignment
+=
A = A + 2;
Compoun d Assignme nt A += 2;
-=
B = B - 3;
B -= 3;
*=
C=C*D
C *= D;
/=
E = E / 2.5
E /= 2.5;
%=
F = F % 4;
F %= 4;
24
TYPE CONVERSIONS • Type Conversions (Typecast) – The rules stated that when evaluating an arithmetic expression if the operator has mixed operands, the integer value is changed to floating-point value with zero decimal part – Implicit type coercion when value of one data type is automatically changed to another data type thus can generate unexpected results – Type conversion or type casting used to avoid implicit type coercion – General form: 25 static_cast (expression)
TYPE CONVERSIONS • Type Conversions (Typecast) – Example Expression
Evaluates to
static_cast(7.9)
7
static_cast(3.3)
3.3
static_cast<double>(25)
25.0
static_cast<double>(5+3) static_cast<double>(15)/2
8.0 = static_cast<double> (15)= 15.0, 15.0/2.0 = 7.5
static_cast<double>(15/2)
= static_cast<double>(7)= 7.0
static_cast(7.8 + static_cast<double>(15)/2 )
= static_cast(7.8 + 7.5)
= 15
static_cast(7.8 + static_cast<double>(15/2) )
= static_cast(7.8 + 7.0)
=14
= static_cast (15.3)
= static_cast(14.8) 26
RELATIONAL OPERATORS • Relational Operators – A condition or logical expression is an expression that can only take the values true or false – A simple form of logical expression is the relational expression x
27
RELATIONAL OPERATORS • Relational Operators – The relational operator allowable in c++ Symbol ==
Description Equal to
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
!=
Not equal to
– The condition is true if the values of two operands satisfy the relational operator 28
RELATIONAL OPERATORS • Relational Operators – Any numerical expression can be used for the value of condition, with 0 being interpreted as false and any no zero value as true – Example Relational Expression
Meaning
8<9
1
6 == 4
0
7 != 2
1
9 >= 3
1
29
LOGICAL OPERATORS • Logical Operators – Logical values – true and false – Logical operator Symbol
Meaning
&&
AND
||
OR
!
NOT
– Truth table AND (&&) Operator Condition Condition 1
2
Output
True
True
True
True
False
False
False
True
False
False
False
False
30
INPUT/OUTPUT OPERATORS • Console input (cin) – Reading data from the cin input stream – Enter the data via system console (keyboard) int num; cin>>num; – Note the use of variable num. A variable is used to hold a value, much like a variable in algebra – The variable is declared to be of type int, which means it can hold a whole number (with some reasonable range) 31
INPUT/OUTPUT OPERATORS • Console input (cin) – A variable is implemented as a named memory location. Thus, the name num refers to some location in the computer’s main memory where the values for num is stored – Input statement is one way to place a value into a variable num – Note that the >> symbol in the direction of data flow: from the cin stream to variable
32
INPUT/OUTPUT OPERATORS • Console output (cout) – Send the output to the system console (monitor) cout<<“My first C++ program”; – Note that a message string is enclosed in double quotes – The << symbol point in the direction of data flow; from the message string to the cout output stream.
33
VARIABLES DECLARATION • Declaration of variables – Associates a type and identifier – A typical set of variable declarations int i, j, count; float sum, product; bool passed_exam; – Initialization of variables int i, j , count = 0; bool passed_exam = false;
34
C++ MATHEMATICAL LIBRARY • Mathematical Library – Include math.h and stdlib.h #include <math.h> #include <stdlib.h> Function
Description
abs(x)
Compute absolute value of x, and x is integer
ceil(x)
Round up value of x, either greater or equal to x
floor(x)
Round up value of x, either less or equal to x
sqrt(x)
Compute square root of x and x >=0
pow(x,y)
Raise to power, x to the power of y
log(x)
Compute natural logarithm of x
log 10(x)
Compute base-10 logarithm of x
cos(x)
Compute cosine x
exp(x)
Compute exponential x with base-e and e=2.718282
sin(x)
Compute sine x
tan(x)
Compute tangent x
35