2 An informal definition of an algorithm is: a step-by-step method for solving a problem or doing a task.
3 Algorithm A step-by-step problem-solving procedure An algorithm is a sequence of unambiguous instructions for solving a problem.The number of steps of an algorithm will be countable and finite.It is a sequence of instructions (or set of instructions) to make a program more readable; a process used to answer a question.
4 How to understand the problem? Define the problemAnalyze the problemDevelop an algorithm/method of solutionWrite a computer program corresponding to the algorithmTest and debug the programDocument the program (how it works and how to use it)
5 Tools Flowcharts Pseudo code There are two commonly used tools to help to document program logic (the algorithm)FlowchartsPseudo code
6 A flowchart indicates: DefinitionThe production flowchart is a visual representation of the sequence of the program. It shows what comes first, second, third, etcA flowchart indicates:The steps to be taken in order to solve a problem.The order or the sequence of these steps.
7 Flowchart Rules1. Use only one start and one stop per flowchart, --that is, one way in and one way out of the flowchart.2. The logic flow of the solution is displayed from top to bottom and from left to right.3. Use the appropriate symbol for each type of operation.4. Use arrows when moving to another part of the flowchart rather than lines.5. Do not leave dead-ends--that is, a part of a question unanswered.
8 Symbols Description Symbol TERMINAL - To start or end a flowchart TERMINAL - To start or end a flowchartINPUT / OUTPUT - Used with Read, Input, Print and other I/O commands.PROCESSING - Used for operations done inside the computer. Such as calculations, storing and moving of data.DECISION - Used to ask a question in programming. Questions are Yes/No format (Used with the If Statement).DIRECTION FLOW - Used to connect symbols and to represent the direction of flow. Lines should not cross each other. Arrowheads should be placed at the end close to the symbol.Connector - or joining of two parts of program
9 1.Simple sequential Flowchart Example 1Construct a flow chart that prints "Hello, World"?
15 1.Simple sequential Flowchart Example 3Construct a flow chart that finds the sum, average and product of three numbers.
16 1.Simple sequential Flowchart VariablesAlgorithmX: First NumberStep 1- StartY: Second NumberStep 2- Read X, Y, ZStep 3- Calculate S = X+Y+ZZ: Third NumberStep 4- Calculate A = S/3S: Sum (X+Y+Z)Step 5- Calculate P = X*Y*ZA: Average (S/3)Step 6- Print S, A, PP: Product (X*Y*Z)Step 7- Stop
28 2. Branched Flowcharts Result When X=20 When X=-10 X= 20 X= -10 W= 21
29 Draw a flowchart that shows the traffic light processing 2. Branched FlowchartsExample 3ExerciseDraw a flowchart that shows the traffic light processing
30 2. Branched Flowcharts Variables Algorithm C : Traffic light color Step 1- StartStep 2- Read CStep 3- make a Decision (what is c)Step 4- if C is RED then Print STOPStep 5- if C is YELLOW then Print WAITStep 6- if C is GREEN then Print PASSStep 7- Stop
31 Trace the following flowchart and write the output of it. 3. Loop FlowchartsExample 1Trace the following flowchart and write the output of it.
32 3. Loop Flowcharts Flowchart Start N=1 Print N F While N<=7 Stop T N=N+3While N<=7FTN=1Print N