Lecture 2 - Program Structure and Basics
Lecture 2 - Program Structure and Basics
Chapter 2
Program Structure and Basics
1
C++ OPERATORS (Section 2.4)
• Assignment Operators
• Arithmetic Operators
• Precedence of Operators
2
Assignment Operator
• The assignment operator (=) is used in C++ to
assign a value to a memory location.
• The assignment statement:
– Syntax: identifier=expression;
– Note: An expression is a C++ constant, identifier, formula,
or function call.
Assignment Statement: x1 = 1.0;
Example 1 - initialization
double sum = 0; sum 0.0
Example 2
int x;
x=5; x 5
Example 3
char ch;
ch = 'a'; ch a
4
Assignment Statements - continued
• Example 3
int x, y, z;
x=y=0;
z=2; 0 x
y 0
z 2
• Example:
int count(10), sum(55); Memory snapshot:
9
Memory Snapshot
#include<iostream>
using namespace std;
int main()
{
double x1(1.0), x2(4.5), side1;
double x1 1.0
double x2 4.5 Output:
side 1 has length: 3.5
double side1 ?
side1 = x2-x1;
double x1 1.0
double x2 4.5
double side1 3.5
cout << “side 1 has length: “ <<side1;
return 0; 10
}
Practice : Identify the output of the following programs
Output
11
Practice : Identify the output of the following programs
Output
12
Solve Practice in Page 71
• Give the value computed by each of the following sets of statements, and
draw a memory map.
1. int a(27), b(6), c;
c = b % a;
2. int a(27), b(6), c;
c = a % b;
3. int a(27), b(6);
double c;
c = a / double(b);
4. int a(27), b(6);
double c;
c = a / b;
5. int a(27);
double b(6), c(18.6);
a = c / b;
6. int b(6);
double a, c(18.6);
a = (int) c / b;
13
Priority of Operators or
Precedence of Operators
1. Parentheses () Inner most first
2. Unary operators Right to left
(+ -)
14
Practice! – Evaluate Expressions
• 7 + 3 * 5 – 2 = 20
• 4+7/3=6
• 8 % 3 * 6 =12
• (7 + 3) * 5 – 2 =48
15
Practice! – Evaluate Expressions
Expression Value
3–4*5
3 – (4 * 5)
(3 – 4) * 5
3*4-5
3 * (4 – 5)
17 – 10 -3
17 – (10 – 3)
(17 – 10) - 3
-42 + 50 %17
16
16
17 / 3
Practice! – Evaluate Expressions
Expression Value
3–4*5 -17
3 – (4 * 5) -17
(3 – 4) * 5 -5
3*4-5 7
3 * (4 – 5) -3
17 – 10 -3 4
17 – (10 – 3) 10
(17 – 10) - 3 4
-42 + 50 %17 -26
17
17
17 / 3 5
Practice! – Evaluate Expressions
Algebra: x3-2x2+x-6.3
f (x)=
x2+0.05005x-3.14
C++ statement:
fx=(pow(x,3)-2*pow(x,2)+x-6.3)/ (pow(x,2)+0.05005*x-3.14) ; 18
18
Practice : Identify the output of the following programs
Output
19
Practice : Identify the output of the following programs
Output
20
Solve Practice in Page 73
In problems 1 through 3, give C++ statements to
compute the indicated values.
Assume that the identifiers in the expression have
been defined as objects of type double and have
also been assigned the appropriate values.
Use the following constant:
Acceleration of gravity g = 9.80665 m/s2
1. Distance travelled
Distance = xo + vot + at2
21
Solve Practice in Page 73
2. Tension in a cord
2m1m2
Tension = *g
m1 m2
3. Fluid pressure at the end of a pipe:
( A A )
2 2
P2 P1 2
2
1
2 A1
22
Solve Practice in Page 74
In problems 4 through 6, give the mathematical
equations computed by the C++ statements.
Assume that the following symbolic constants have
been defined, where the units of G are m3/(kg*s2).
const double PI = acos(-1.0);
const double G = 6.67259e-11;
4. Centripetal acceleration.
centripetal = 4*PI*PI*r/(T*T);
5. Potential energy:
potential_energy = -G * M_E*m/r;
6. Change in potential energy:
change = G * M_E * m * (1/R_E – 1/(R_E + h));23
Overflow and Underflow
• Exponent underflow
– answer’s exponent too small
Example: using float, with exponent range –38 to 38
result = 3.25e-28 *1.0e-15;
24
Practice : Identify the output of the following programs
25
Practice : Identify the output of the following programs
26
Increment and Decrement Operators
• Increment Operator ++
• post increment x++;
• pre increment ++x;
• Decrement Operator - -
• post decrement x- -;
• pre decrement - -x;
• For examples assume a=b=c=d=5 prior to executing
each of the statements.
• m= ++a; both m and a become 6 (pre increment)
• m= b++; m becomes 5 and b becomes 6 (post increment)
• n = --c; both n and c become 4 (pre decrement)
• n = d--; n becomes 5 and d becomes 4 (post decrement)
27
Abbreviated Assignment Operators
28
Precedence of Arithmetic and
Assignment Operators
Precedence Operator Associativity
1 Parentheses: () Innermost first
31