You are here: Home » Content » Algorithms

Algorithms

Module by: Dr. Duong Tuan Anh

An algorithm is defined as a step-by-step sequence of instructions that describes how the data are to be processed to produce the desired outputs. In essence, an algorithm answers the question: “What method will you use to solve the problem?”
You can describe an algorithm by using flowchart symbols. By that way, you obtain a flowchart which is an outline of the basic structure or logic of the program.

Flowchart Symbols

To draw flowchart, we employ the symbols shown in the Figure 1. The meaning of each flowchart symbol is given in Table 1.
To illustrate an algorithm, we consider the simple program that computes the pay of a person. The flowchart for this program is given in Figure 2.
Note: Name, Hours and Pay are variables in the program.
Figure 1: Flowchart symbols
Table 1: Meaning of flowchart symbols
Symbol name Description
Terminal Indicates the beginning or end of an algorithm
Input/Output Indicates an input or output operation
Process Indicates computation or data manipulation
Flow lines Connects the flowchart symbols and indicates the logic flow
Decision Indicates a program branch point
Connector Indicates an entry to, or exit from another part of the flowchart or a connection point
Predefined process Indicates a predefined process, as in calling a function

Algorithms in pseudo-code

You also can use English-like phases to describe an algorithm. In this case, the description is called pseudocode. Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode has some ways to represent sequence, decision and repetition in algorithms. A carefully prepared pseudocode can be converted easily to a corresponding C++ program.
Figure 2: Flowchart for calculating the payment of a person
EndDisplayName, PayExample: The following set of instructions forms a detailed algorithm in pseudocode for calculating the payment of person.
Input the three values into the variables Name, Hours, Rate.
Calculate Pay = Hours * Rate.
Display Name and Pay.

Loops in Algorithms

Many problems require repetition capability, in which the same calculation or sequence of instructions is repeated, over and over, using different sets of data.
Example 1: Write a program to do the task
Print a list of the numbers from 4 to 9, next to each number, print the square of the number.
The flowchart for the algorithm that solves this problem is given in Figure 1.5. You will see in Figure 3 the flowchart symbol for decision and the flowline that can connect backward to represent a loop.
Figure 3: Flowchart for calculating the squares of numbers from 4 to 9.
Note:
  1. In the flowchart, the statement
NUM = NUM + 1
means “old value of NUM + 1 becomes new value of NUM ”.
The above algorithm can be described in pseudocode as follows:
NUM = 4
do
SQNUM = NUM*NUM
Print NUM, SQNUM
NUM = NUM + 1
while (NUM <= 9)
You can compare the pseudo-code and the flowchart in Figure 3 to understand the meaning of the do.. while construct used in the pseudo-code.

Flowchart versus pseudocode

Since flowcharts are inconvenient to revise, they have fallen out of favor by programmers. Nowadays, the use of pseudocode has gained increasing acceptance.
Only after an algorithm has been selected and the programmer understands the steps required can the algorithm be written using computer-language statements. The writing of an algorithm using computer-language statements is called coding the algorithm, which is the third step in our program development process.

Comments, questions, feedback, criticisms?

Send feedback