ALGORITHM AND FLOWCHART (SS 2 THIRD TERM)

ALGORITHM 

Algorithm can also be defined as a procedure consisting of a series of steps which specify a sequence of operations that provides the solution to a problem.

An algorithm is a finite set of an instruction that specifies a sequence of operations to be carried out in order to solve a specific problem. It is a detailed sequence of simple steps that are needed to solve a problem.

Algorithm: Procedure for solving a problem

Flowchart: These are diagrams that represents an algorithm, workflow or process, showing the steps as boxes of various kinds, and their order by connecting them with arrows.

The boxes represent operations and the arrows represent the sequence in which the operations are implemented.

Characteristics of Algorithm

1.       Finite

2.       Effective

3.       Unambiguous

Functions of algorithm

  •   It provides a step-by-step solution to a problem
  •   It is used to prove that a problem has a solution
  •   It is used to give a sense of direction to the steps of solving a problem
  •   It may also be used to show that a problem does not have a solution

SIMPLE ALGORITHM

An algorithm to calculate the area of a triangle

Step 1:  Enter base value

Step 2:  Enter height value

Step 3:  Calculate the Area of the triangle (½ base X height)

Step 4:  Display Area of triangle

Step 5:  End

Example1: 

Write an algorithm that tells you how to wash dishes

Solution

Step 1: start

Step 2: scrape food off dishes

Steps 3: wash the dishes with soap and water

Step 4: Rinse the dishes

Step 5: Dry them

Step 6: Stop

Example 2: Write an algorithm to evaluate the equation y = a(b-c)^2/d+2

Solution

Step 1: start

Step 2: input the value of a, b, c, d

Step 3: Value of y is to be calculated

Step 4: Calculate the value of b-c and denote f

Step 5: Calculate the square of f

Step 6: Multiply f by a and denote g

Step 7: Calculate the value of d+2 and denote h

Step 8: divide h by g

Step 9: We get the value of y

Step 10:  Print y

Step 11: Stop

Example 3: Calculate the Interest of a Bank Deposit

Algorithm:

Step 1: Start

Step 2: Read amount,

Step 3: Read years,

Step 4: Read rate,

Step 5: Calculate the interest with formula “Interest=Amount*Years*Rate/100

Step 6: Print interest

Step 7: Stop/End

Example 4: Algorithm to Determine and Output Whether Number N is Even or Odd

Algorithm:

Step 1: Start

Step 2: Read number N,

Step 3: Set remainder as N modulo (divide) 2,

Step 4: If remainder is equal to 0 then number N is even, else number N is odd,

Step 5: Print output.

Step 6: Stop/End

Example 5: Algorithm to Determine Whether a Temperature is Below or Above the Freezing Point

Algorithm:

Step 1: Input temperature,

Step 2: Read Temperature

Step 3: Step 2: If it is less than 32, then print "below freezing point", otherwise print "above freezing point"

Step 4: Print output.

Step 5: Stop/End

Example 6: Algorithm to  Determine Whether A Student Passed the Exam or Not:

Algorithm:

Step 1: Input grades of 4 courses M1, M2, M3 and M4,

Step 2: Calculate the average grade with formula "Grade=(M1+M2+M3+M4)/4"

Step 3: If the average grade is less than 60, print "FAIL", else print "PASS".

Step 4: Print output.

Step 5: Stop/End

Example 7: Algorithm to Print 1 to 20:

Algorithm:

Step 1: Start

Step 2: Initialize X as 0,

Step 3: Increment X by 1,

Step 4: Print X,

Step 5: If X is less than 20 then go back to step 2.

Step 6: Stop 

Comments

Popular posts from this blog