Title: CS149D Elements of Computer Science
1CS149D Elements of Computer Science
Ayman Abdel-Hamid Department of Computer
Science Old Dominion University Lecture 18
10/31/2002
2Outline
- Finish Chapter 2
- Mathematical and Trigonometric functions
- Example page 48
- Chapter 3
- Flowcharts
- Program Structures
- Conditional expressions
- Selection Statements
3Mathematical/Trigonometric functions
include ltmath.hgt Argument is a double and return
value a double fabs(x) absolute value of
x sqrt(x) square root of x pow (x,y) x to the
power of y ceil (x) ceiling (3.1)
4 floor(x) floor (3.99) 3 see page 46 for
more functions
include ltmath.hgt Argument is a double and return
value a double, angles are represented in radians
(180 PI radians) sin(x) cos(x)
tan(x,y) see page 47 for more functions
4Example
Example page 48 Velocity Computation Velocity
0.00001 time3-0.00488time20.75795time181.3566 Ac
celeration 3 0.000062 velocity2 See program
on page 51
5Chapter 3
- Flowcharts as a tool for algorithm representation
- Control structures (selection, repetition)
- Data Files
6Flowchart Symbols
Operation Pseudocode Input Read
radius Computation Assign area the value PI
radius2 Output Write (or Print) radius,
area Comparisons if radius lt 0 then Begin/End
Area PI radius2
Is radius lt 0 ?
Yes
No
Start/Stop
7Program Structures1/3
Sequence structure Steps that are performed one
after another (all programs so far) Flowchart for
the triangle area program
8Program Structures2/3
Selection structure Contains a condition that
evaluates to either true or false If condition is
true one set of statements is executed, otherwise
(if false) another set of statements is executed
9Program Structures3/3
Repetition structure Repeat a set of steps as
long as a condition is true Print x2 for x
0,1,., 9
10Conditional Expressions
- A condition is an expression that can be
evaluated to true or false - Composed of expressions combined with relational
and logical operators - Relational operators ( lt, lt, gt, gt, , !)
- a lt b
- a 10 b 2 a lt b evaluates to false
- a 2 b 10 a lt b evaluates to true
- a b gt c If b gt c then a is assigned 1
otherwise assigned 0 - Logical operators ( ! (NOT), (AND), (OR))
- a lt b b lt c (a less than b) AND (b less than
c) - a is 2, b is 9, c is 2 condition evaluates to
false (Why?) - Relational operators have higher precedence than
logical operators. See precedence table page 67
11Selection Statements1/2
A statement can be a compound statement if
(condition) Statement 1 .. Statement
n Example if (A lt 10) B 5 A C 2
if (condition) Statement 1 Example if ( A lt
10) B 5 C 2
if/else statement if (condition) Statement
1 else Statement 2 Example if ( A lt 10) B
5 else C 2
12Selection Statements2/2
if ( A lt 10) B 5 else C 2 D 3
Nested ifs if (x gt y) if (y lt z) K else M el
se J Compiler always matches else with closest
if
Nested ifs if (x gt y) if (y lt z) K else J Wh
en J gets executed?