0% found this document useful (0 votes)
47 views

Lecture 2 - Program Structure and Basics

Here are the memory snapshots after each statement: 1. x++; x = 3 y = 4 2. y--; x = 3 y = 3 3. ++x; x = 4 y = 3 4. x+=2; x = 6 y = 3 5. y-=x; x = 6 y = -2

Uploaded by

Partha Deb
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Lecture 2 - Program Structure and Basics

Here are the memory snapshots after each statement: 1. x++; x = 3 y = 4 2. y--; x = 3 y = 3 3. ++x; x = 4 y = 3 4. x+=2; x = 6 y = 3 5. y-=x; x = 6 y = -2

Uploaded by

Partha Deb
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Part 2

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;

• assigns the value 1.0 to the variable x1.


• Thus, the value 1.0 is stored in the memory location
associated with the identifier x1.
3
Assignment Operators - Examples

 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

• How is the memory map affected by the


following statement?
y = z;
5
Arithmetic Operators
• Addition +
• Subtraction-
• Multiplication *
• Division /
• Modulusor remainder %
– Modulus returns remainder of division between
two integers
– Examples
5%2 returns a value of 1
2%2 returns a value of 0
5%3 returns a value of 2
6%4 returns a value of
6%2 returns a value of 6
Integer Division
• Division between two integers results in an
integer.
• The result is truncated, not rounded
• Examples:
The expression 5/3 evaluates to 1
The expression 3/6 evaluates to 0
The expression 10/4 evaluates to 2
The expression 20/6 evaluates to 3
The expression 15/6 evaluates to 2
The expression 11/6 evaluates to
7
float or double Division
• Division between two double results in an
integer.
• The result is truncated, not rounded
• Examples:
The expression 5.0/3 evaluates to 1.66
The expression 3/6.0 evaluates to 0.5
The expression 10.0/4 evaluates to 2.5
The expression 20/6.0 evaluates to 3.33
The expression 15/6 evaluates to 2
The expression 11/6.0 evaluates to 1.833
8
The cast Operator
• The cast operator.
– The cast operator : changes the type of the value of the
operand to a new type for the next computation.
– The type of the operand is not affected.
– Syntax: (type) expression;

• Example:
int count(10), sum(55); Memory snapshot:

double average; int count 10


average = (double) sum/count;
int sum 55

double average 5.5

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
(+ -)

3. Binary operators Left to right


(* / %)

4. Binary operators Left to right


(+ -)

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:

Solution1: fx=(x*x*x -2*x*x+x-6.3)/(x*x+0.05005*x-3.14)

Solution2: using pow(x,y) from <cmath> library

#include <cmath> //for using pow()

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

• Overflow: the assigned value is too large


• Underflow: the assigned value is too small
• Exponent overflow
– answer’s exponent is too large
Example: using float, with exponent range –38 to 38
result = 3.25e28 * 1.0e15;

• 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

operator example equivalent statement


+= x+=2; x=x+2;
-= x-=2; x=x-2;
*= x*=y; x=x*y;
/= x/=y; x=x/y;
%= x%=y; x=x%y;

28
Precedence of Arithmetic and
Assignment Operators
Precedence Operator Associativity
1 Parentheses: () Innermost first

2 Unary operators Right to left


+ - ++ -- (type)

3 Binary operators Left ot right


*/%

4 Binary operators Left ot right


+-

5 Assignment Right to left


operators
= += -= *= /= %=
29
Solve Practice in Page 77
Give a memory snapshot after each statement is
executed, assuming that
x is equal to 2 and that
y is equal to 4
before the statement is executed.
Also, assume that all the objects are integers.
1.z = x++ * y;
2.z = ++x * y;
3.x += y;
4.y %= x;
30
Practice : Identify the output of the following programs

31

You might also like