Algorithm Design CS105 Problem Solving • Algorithm: set of unambiguous instructions to solve a problem – Breaking down a problem into a set of subproblems – Example: Clean the house • Without instructions – computers cannot do anything at all! Algorithm design 1. Analysis and specification - Analyze: Understand/define the problem Specify: Specify particulars 2. Algorithm development phase - Develop: Logical sequence of steps Test: Follow outline, test cases 3. Implementation phase - Code: The steps into a programming language Test: Debug 4. Maintenance phase - Use the program Maintain: Correct errors, meet changing requirements An example: Making a perfect piece of toast What do we need: a loaf of bread, knife, toaster, Variables plate, butter, cutting board Version 1 1. Move a loaf of bread on a cutting board 2. Cut a slice of bread with a knife 3. Move the slice of bread to the toaster 4. Turn toaster on 5. Wait for the toaster to finish 6. Move the toasted bread on a plate 7. Spread butter on the with knife Output An example: Making a perfect piece of toast Variables: A= loaf of bread K= knife T= toaster P= plate B= butter CB= cutting board S= slice of bread Version 2 1. move move A to CB 2. cut S with K 3. move move S to T 4. turn on T 5. wait for T 6. move move S to P 7. spread B on S with K An example: Making toast for fussy friends: plain, butter, margarine Variables: A= loaf of bread K= knife T= toaster P= plate B= butter M= margarine F= friend CB= cutting board S= slice of bread X Version 3 Move A to CB FOR every F eating toast { Cut S with K Move S to T Turn on T Wait for T Move S to P IF F likes B { X=B } ELSE IF F likes M { X=M } ELSE { X= NOTHING } Spread X on S with K } Basic concepts • • • • • Example contains concepts used in most algorithms Instructions – simple and unambiguous Variables – input and temporary Subprocedures – smaller tasks Looping: FOR each variable, WHILE – Act of repeating tasks • Conditional statements: IF ELSE – Selectively execute instructions Pseudocode: Flowchart • Flowchart: diagram that represents an algorithm • Symbols: START , END INSTRUCTIONS CONDITIONALS LOOPS, FLOW OF CONTROL Pseudocode: Flowchart Fixing non functioning lamp algorithm Flowchart: Making toast for friends Start End No Variables: A= loaf of bread K= knife T= toaster P= plate B= butter M= margarine F= friend CB= cutting board Cut S with K Move S to T Turn on T Wait for T Spread X on S with K Move S to P Set X to B S= slice of bread X Yes Done ? Move A to CB Yes Want B? No No Want M? Yes Set X to M Set X to NONE Add 30 to X Start Subtract 15 from X Yes Is X > 100? No Multiply X by 3 End Is X < 80? Yes Print X Yes No Divide X by 2 Is X an integer ? No Round it to the nearest integer Add 30 to X IF ( x>100) { Subtract 15 from X } ELSE { Multiply X by 3 } IF ( x<80) { PRINT X } ELSE { Divide X by 2 IF ( X is an integer) { PRINT X } ELSE { Round X to the nearest integer PRINT X } } 1. 2. 3. 4. 5. Add 30 to X IF X >100, subtract 15 ELSE multiply X by 3 IF X<80, PRINT X END ELSE Divide by 2 1. IF X is an integer, PRINT X END 2. ELSE round X, then PRINT X END