Algorithm & Flowchart
An informal definition of an algorithm is: Algorithm: a step-by-step method for solving a problem or doing a task. Figure 8.1 Informal definition of an algorithm used in a computer
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.
Develop an algorithm/method of solution Understand the problem. Define the problem Analyze the problem Develop an algorithm/method of solution Write a computer program corresponding to the algorithm Test and debug the program Document the program (how it works and how to use it)
There are two commonly used tools to help to document program logic (the algorithm). These are : Flowcharts Pseudo code.
Flowchart A flowchart is a type of diagram that represents an algorithm or process, showing the steps as boxes of various kinds and their order by connecting these with arrows. This diagrammatic representation can give a step-by-step solution to a given problem.
Process operations are represented in these boxes, and arrows connecting them represent flow of control. Flowcharts are used in analyzing, designing, documenting or managing a process or program in various fields.
Symbols Start and end symbols, represented as lozenges, ovals or rounded rectangles, usually containing the word "Start" or "End", or another phrase signaling the start or end of a process, such as "submit enquiry" or "receive product". Arrows, showing what's called "flow of control" in computer science. An arrow coming from one symbol and ending at another symbol signifies flow passes to the symbol the arrow points to. Processing steps, represented as rectangles. Examples: "Add 1 to X"; "replace identified part"; "save changes" or similar. Input/Output, represented as a parallelogram. Examples: Get X from the user; display X. Conditional (or decision), represented as a diamond (rhombus). These typically contain a Yes/No question or True/False test .
Start or end of the program Computational steps or processing function of a program Input or output operation Decision making and branching Connector or joining of two parts of program
1- Simple sequential Flowchart • Example 1: - Read X, Y, Z Compute Sum (S) as X + Y +Z Compute Average (A) as S / 3 Compute Product (P) as X xYxZ - Write (Display) the Sum,Average and Product
• Example 2: Find the sum of two numbers • Example 2: Find the sum of two numbers. – Variables: • A: First Number • B: Second Number • C: Sum (A+B) – Algorithm: • Step 1 – Start • Step 2 – Input A • Step 3 – Input B • Step 4 – Calculate C = A + B • Step 5 – Output C • Step 6 – Stop
• Example 3: Find the difference and the division of two numbers and display the result
• Example 4: Find the circle area and circumcenter of a circle where R (radius) is given (exercise) – Variables: • R: Radius • A: Area •C: Circumcenter – Algorithm: • Step 1 – Start • Step 2 – Input R • Step 3 – Calculate Pi • Step 4 – Calculate A = Pi(R)2 • Step 5 – Calculate C = 2Pi(R) • Step 6 – Print R, A, C • Step 7– Stop
• Example 1: 2- Branched Flowcharts Draw a flowchart that shows the traffic light processing (exercise) – Algorithm: • Step 1 – Start • Step 2 – make a Decision (what is the color) • Step 3 – if the color is Red then STOP • Step 4 – if the color is Yellow then WAIT • Step 5 – if the color is Green then PASS • Step 6– Stop
3- Loop Flowcharts • Example 1:
• Example 2 Average for 10 numbers