lecture 18: 10/31/2002cs149d fall 20021 cs149d elements of computer science ayman abdel-hamid...

12
Lecture 18: 10/3 1/2002 CS149D Fall 2002 1 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 18: 10/31/2002

Upload: brett-mccormick

Post on 02-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 1

CS149D Elements of Computer Science

Ayman Abdel-Hamid

Department of Computer Science

Old Dominion University

Lecture 18: 10/31/2002

Page 2: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 2

Outline•Finish Chapter 2

Mathematical and Trigonometric functions

Example page 48

•Chapter 3

Flowcharts

Program Structures

Conditional expressions

Selection Statements

Page 3: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 3

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) see page 47 for more functions

Page 4: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 4

ExampleExample page 48 Velocity Computation

Velocity = 0.00001 time3-0.00488time2+0.75795time+181.3566

Acceleration = 3 – 0.000062 velocity2

See program on page 51

Page 5: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 5

Chapter 3

•Flowcharts as a tool for algorithm representation

•Control structures (selection, repetition)

•Data Files

Page 6: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 6

Flowchart SymbolsOperation Pseudocode

Input Read radius

Computation Assign area the value PI * radius2

Output Write (or Print) radius, area

Comparisons if radius < 0 then

Begin/End

Read radius

Area = PI * radius2

Write radius, area

Is radius < 0 ?

No

Yes

Start/Stop

Page 7: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 7

Program Structures1/3

Sequence structure

Steps that are performed one after another

(all programs so far)

Flowchart for the triangle area program

Start

read base, height

Triangle area = 0.5 * base * height

Print area

Stop

Page 8: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 8

Program 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

If a > 10

Print B

B = a * 20

Print C

C = a/20

No Yes

Page 9: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 9

Program Structures3/3

Repetition structure

Repeat a set of steps as long as a condition is true

Print x2 for x = 0,1,…., 9

x = 0

Is x < 10 ?

y = x2

Print y

Increment x

Yes

No

Page 10: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 10

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

Page 11: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 11

Selection Statements1/2

if (condition)

Statement 1;

Example

if ( A < 10)

B = 5;

C = 2;

A statement can be a compound statementif (condition){

Statement 1;..Statement n;

}

Exampleif (A < 10){

B = 5;A++;

}C = 2;

if/else statementif (condition)

Statement 1;else

Statement 2 ;

Exampleif ( A < 10)

B = 5;else

C = 2;

Page 12: Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture

Lecture 18: 10/31/2002 CS149D Fall 2002 12

Selection Statements2/2

if ( A < 10)B = 5;

else{

C = 2;D = 3;

}

Nested ifs

if (x > y)

if (y < z)

K++;

else

M++;

else

J++;

Compiler always matches else with closest if

Nested ifs

if (x > y)

if (y < z)

K++;

else

J++;

When J++ gets executed?