Lecture 18: 10/31/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 18: 10/31/2002
CS149D Fall Outline Finish Chapter 2 Mathematical and Trigonometric functions Example page 48 Chapter 3 Flowcharts Program Structures Conditional expressions Selection Statements
Lecture 18: 10/31/2002CS149D Fall 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
Lecture 18: 10/31/2002CS149D Fall Example Example page 48Velocity Computation Velocity = time time time Acceleration = 3 – velocity 2 See program on page 51
Lecture 18: 10/31/2002CS149D Fall Chapter 3 Flowcharts as a tool for algorithm representation Control structures (selection, repetition) Data Files
Lecture 18: 10/31/2002CS149D Fall 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
Lecture 18: 10/31/2002CS149D Fall 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
Lecture 18: 10/31/2002CS149D Fall 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
Lecture 18: 10/31/2002CS149D Fall 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
Lecture 18: 10/31/2002CS149D Fall 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
Lecture 18: 10/31/2002CS149D Fall 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;
Lecture 18: 10/31/2002CS149D Fall 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?