Download presentation
Presentation is loading. Please wait.
1
Algorithm Design CS105
2
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!
3
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
4
An example: Variables Output
5
An example: Variables: A= loaf of bread K= knife T= toaster P= plate B= butter CB= cutting board Making a perfect piece of toast Version 2 1. move A to CB 2. cut S with K 3. move S to T 4. turn on T 5. wait for T 6. move S to P 7. spread B on S with K S= slice of bread move
6
An example: Variables: A= loaf of bread K= knife T= toaster P= plate B= butter M= margarine F= friend CB= cutting board Making toast for fussy friends: plain, butter, margarine 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 } S= slice of bread X
7
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
8
Pseudocode: Flowchart Flowchart: diagram that represents an algorithm Symbols: START, END LOOPS, FLOW OF CONTROL INSTRUCTIONS CONDITIONALS
9
Pseudocode: Flowchart Fixing non functioning lamp algorithm
10
Flowchart: Making toast for friends 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 Start Move A to CB Done ? Cut S with K Move S to T Turn on T End Wait for T Move S to P Want B? Want M? Set X to B Set X to M Set X to NONE Spread X on S with K Yes No Yes No Yes No
11
Start End Add 30 to X Multiply X by 3 Is X > 100 ? Is X < 80 ? Subtract 15 from X Print X Yes No Yes Divide X by 2 Is X an integer ? Yes No Round it to the nearest integer
12
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.Add 30 to X 2.IF X >100, subtract 15 3.ELSE multiply X by 3 4.IF X<80, PRINT X END 5.ELSE Divide by 2 1.IF X is an integer, PRINT X END 2.ELSE round X, then PRINT X END
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.