Lecture 18 - ODU Computer Science

Download Report

Transcript Lecture 18 - ODU Computer Science

CS149D Elements of Computer
Science
Ayman Abdel-Hamid
Department of Computer Science
Old Dominion University
Lecture 18: 10/31/2002
Lecture 18: 10/31/2002
CS149D Fall 2002
1
Outline
•Finish Chapter 2
Mathematical and Trigonometric functions
Example page 48
•Chapter 3
Flowcharts
Program Structures
Conditional expressions
Selection Statements
Lecture 18: 10/31/2002
CS149D Fall 2002
2
Mathematical/Trigonometric functions
#include <math.h>
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 <math.h>
Argument is a double and return value a double, angles are represented in radians (180 = PI *
radians)
sin(x)
cos(x)
tan(x,y)
Lecture 18: 10/31/2002
see page 47 for more functions
CS149D Fall 2002
3
Example
Example page 48
Velocity Computation
Velocity = 0.00001 time3-0.00488time2+0.75795time+181.3566
Acceleration = 3 – 0.000062 velocity2
See program on page 51
Lecture 18: 10/31/2002
CS149D Fall 2002
4
Chapter 3
•Flowcharts as a tool for algorithm representation
•Control structures (selection, repetition)
•Data Files
Lecture 18: 10/31/2002
CS149D Fall 2002
5
Flowchart Symbols
Operation
Pseudocode
Input
Read radius
Computation
Assign area the value PI * radius2
Output
Write (or Print) radius, area
Comparisons
if radius < 0 then
Read radius
Area = PI * radius2
Write radius,
area
Is radius < 0 ?
Yes
No
Begin/End
Start/Stop
Lecture 18: 10/31/2002
CS149D Fall 2002
6
Program Structures
1/3
Start
Sequence structure
Steps that are performed one after
another
read base, height
(all programs so far)
Flowchart for the triangle area
program
Triangle area = 0.5 * base * height
Print area
Stop
Lecture 18: 10/31/2002
CS149D Fall 2002
7
Program Structures
2/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
No
If a > 10
B = a * 20
Print B
Lecture 18: 10/31/2002
Yes
CS149D Fall 2002
C = a/20
Print C
8
Program Structures
3/3
x=0
Repetition structure
Repeat a set of steps as long
as a condition is true
No
Is x < 10 ?
Print x2 for x = 0,1,…., 9
Yes
y = x2
Print y
Increment x
Lecture 18: 10/31/2002
CS149D Fall 2002
9
Conditional 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 ( <, <=, >, >=, ==, !=)
•a < b
a = 10 b = 2 a < b evaluates to false
a = 2 b = 10 a < b evaluates to true
•a = b > c;
If b > c then a is assigned 1 otherwise assigned 0
•Logical operators ( ! (NOT), && (AND), || (OR))
a < b && b < 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
Lecture 18: 10/31/2002
CS149D Fall 2002
10
Selection Statements
if (condition)
Statement 1;
Example
if ( A < 10)
B = 5;
C = 2;
Lecture 18: 10/31/2002
A statement can be a
compound statement
if (condition)
{
Statement 1;
..
Statement n;
}
Example
if (A < 10)
{
B = 5;
A++;
}
C = 2;
CS149D Fall 2002
1/2
if/else statement
if (condition)
Statement 1;
else
Statement 2 ;
Example
if ( A < 10)
B = 5;
else
C = 2;
11
Selection Statements
if ( A < 10)
B = 5;
else
{
C = 2;
D = 3;
}
Nested ifs
if (x > y)
if (y < z)
K++;
else
M++;
else
J++;
2/2
Nested ifs
if (x > y)
if (y < z)
K++;
else
J++;
When J++ gets executed?
Compiler always
matches else with
closest if
Lecture 18: 10/31/2002
CS149D Fall 2002
12