Programming Fundamentals

Algorithms

An algorithm is a stepwise solution to a problem. Each step in an algorithm represents a solution to a small problem. Hence, algorithm gives a sequence of steps which makes complete solution of a problem in hand. An algorithm itself is decomposition of problem into small steps which are easily understandable. Consider the following examples.

EXAMPLE 1

Problem: Write an algorithm to find area of circle.

In this problem, first radius is known. Once radius is known, it can be used to computer area as

            A = PIE * R*R

Which finally is given as output. An algorithm is given below.

Algorithm:

            1. Input R
            2. Compute A = 3.14 * R*R
            3. Print A
            4. STOP

Each steps starts with unique number (which are in increasing order) followed by operations.  Operation uses appropriate word and quantities(constant or variables). For example 1. Input R has 1 as step number and Input R represents that a value of R(radius) is to taken from user. The last step is STOP which denotes end of algorithm. The word i.e. Input, Compute, Print should denotes appropriate action and user is free to use any meaningful words. Example 1 is purely sequential problem.

EXAMPLE 2

Problem: Write an algorithm to find minimum of two numbers.

This require two numbers N1 and N2 to be read, compare and whichever is smaller is to be printed. Algorithm is as follows:

Algorithm:

            1. Input N1 , N2
            2. If N1 < N2, goto 5
            3. Print N2
            4. goto 6
            5. Print N1
            6. STOP

Step 1 reads two numbers N1, and N2. In step 2, N1, and N2 are compared. If N1, is smaller than N2, condition N1, < N2 is true and next step done is step 5 where N1, is printed. If N1, < N2, is false, then next step done is step 3 which prints N2. After printing N1, or N2, it is ended with step 6.

Algorithms are written in physical sequence, but they are evaluated in logical sequence. Step 1 to 6 in increasing order defines a physical sequence in example 2. Logical sequence which is used to evaluate, depends on the input values given. There may be more than one logical sequence where each corresponds to exactly one possible input combination. Example 2 is evaluated as follows :

Case 1:

            1. N1 = 5 , N2 = 7
            2. N1 < N2, goto 5
            5. Print 5
            6. STOP

The sequence of steps used here are 1, 2, 5 and 6. It forms a logical sequence or a path through which evaluation is made. This path is taken as input value N1, = 5 is less than N2 = 7.

Case 2:

            1. N1 = 7 , N2 = 4
            2. N1 < N2, (next step, as it is false)
            3. Print 4
            4. goto 6
            6. STOP

as N1 = 7 which is greater than N2 = 4. Logical sequence followed is 1, 2, 3, 4 and 6. For. N1 = N2 also, the same sequence is followed

Hence, algorithm of example 2 as two logical sequence inside it :

            1.   1,2,5,6
            2.   1,2,3,4,6

This defines set of possible paths for evaluation. As size and complexity of algorithm increases, number of paths increases. But remember that for given input, there will be always a one path. Algorithm once translated to higher level language statements, it is called a program. A program is defined as a logical sequence of instructions. Instructions are done in logical order as discussed for algorithms. A point or step at which currently evaluation (or execution) is being performed is called control. Control always flows in logical sequence. Normally it goes in sequential order, but decision making, looping or flow breaking statements causes it to alter the normal sequence as per requirement.