Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture."— Presentation transcript:

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

2 CS149D Fall 20022 Outline Finish Chapter 2  Mathematical and Trigonometric functions  Example page 48 Chapter 3  Flowcharts  Program Structures  Conditional expressions  Selection Statements

3 Lecture 18: 10/31/2002CS149D Fall 20023 Mathematical/Trigonometric functions #include 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) = 3see page 46 for more functions #include 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

4 Lecture 18: 10/31/2002CS149D Fall 20024 Example Example page 48Velocity Computation Velocity = 0.00001 time 3 -0.00488time 2 +0.75795time+181.3566 Acceleration = 3 – 0.000062 velocity 2 See program on page 51

5 Lecture 18: 10/31/2002CS149D Fall 20025 Chapter 3 Flowcharts as a tool for algorithm representation Control structures (selection, repetition) Data Files

6 Lecture 18: 10/31/2002CS149D Fall 20026 Flowchart Symbols OperationPseudocode InputRead radius ComputationAssign area the value PI * radius 2 OutputWrite (or Print) radius, area Comparisonsif radius < 0 then Begin/End Read radius Area = PI * radius2 Write radius, area Is radius < 0 ? No Yes Start/Stop

7 Lecture 18: 10/31/2002CS149D Fall 20027 Program Structures 1/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

8 Lecture 18: 10/31/2002CS149D Fall 20028 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 If a > 10 Print B B = a * 20 Print C C = a/20 NoYes

9 Lecture 18: 10/31/2002CS149D Fall 20029 Program Structures 3/3 Repetition structure Repeat a set of steps as long as a condition is true Print x 2 for x = 0,1,…., 9 x = 0 Is x < 10 ? y = x 2 Print y Increment x Yes No

10 Lecture 18: 10/31/2002CS149D Fall 200210 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 = 2a < b evaluates to false  a = 2 b = 10a < 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 2condition evaluates to false (Why?)  Relational operators have higher precedence than logical operators. See precedence table page 67

11 Lecture 18: 10/31/2002CS149D Fall 200211 Selection Statements 1/2 if (condition) Statement 1; Example if ( A < 10) B = 5; C = 2; A statement can be a compound statement if (condition) { Statement 1;.. Statement n; } Example if (A < 10) { B = 5; A++; } C = 2; if/else statement if (condition) Statement 1; else Statement 2 ; Example if ( A < 10) B = 5; else C = 2;

12 Lecture 18: 10/31/2002CS149D Fall 200212 Selection Statements 2/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?


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

Similar presentations


Ads by Google