120 likes | 364 Views
Algorithm Design. CS105. Problem Solving. Algorithm: set of unambiguous instructions to solve a problem Breaking down a problem into a set of sub-problems Example: Clean the house Without instructions – computers cannot do anything at all!. Algorithm design. Analysis and specification
E N D
Algorithm Design CS105
Problem Solving • Algorithm: set of unambiguous instructions to solve a problem • Breaking down a problem into a set of sub-problems • Example: Clean the house • Without instructions – computers cannot do anything at all!
Algorithm design • Analysis and specification • Analyze: Understand/define the problem • Specify: Specify particulars • Algorithm development phase • Develop: Logical sequence of steps • Test: Follow outline, test cases • Implementation phase • Code: The steps into a programming language • Test: Debug • 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, plate, butter, cutting board Version 1 • Move a loaf of breadon a cutting board • Cut a slice of bread with a knife • Move the slice of bread to the toaster • Turn toaster on • Wait for the toaster to finish • Move the toasted bread on a plate • Spread butter on the toast with knife Variables 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 Version 2 1. moveA to CB 2. cut S with K 3. move S to T 4. turn onT 5. wait forT 6. moveStoP 7. spreadBonS with K move move S= slice of bread move
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 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 IFF likes B { X=B } ELSE IFF likes M { X=M } ELSE { X= NOTHING } Spread X on S with K } S= slice of bread X
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 LOOPS, FLOW OF CONTROL INSTRUCTIONS CONDITIONALS
Pseudocode: Flowchart Fixing non functioning lamp algorithm
Flowchart: Making toast for friends Done? Start End Move A to CB Yes Variables: A= loaf of bread K= knife T= toaster P= plate B= butter M= margarine F= friend CB= cutting board No Cut S with K Move S to T Turn on T Wait for T Spread X on S with K Move S to P Want B? Want M? S= slice of bread X No No Yes Set X to B Set X to NONE Yes Set X to M
Start Add 30 to X Is X > 100? Subtract 15 from X Yes No Multiply X by 3 End Is X < 80? Yes Print X Yes No Is X an integer? No Round it to the nearest integer Divide X by 2
Add 30 to X • IF X >100, subtract 15 • ELSE multiply X by 3 • IF X<80, PRINT X END • ELSE Divide by 2 • IF X is an integer, PRINT X END • ELSE round X, then PRINT X END 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 } }