1. Explain the building blocks of an algorithm ? Explain in detail.
(16 Marks)
The building blocks are,
◦ Statements
◦ State
◦ Control flow and
◦ Functions
Statements / Instructions
The algorithm consists of finite number of statements.The statements
must be in an ordered form. The time taken to execute all the statements
of the algorithm should be finite and within a reasonable limit.
State
The algorithm must have an instruction for a program to do.The state of
the instruction will be changed during execution. The state or condition of
the instruction determines the logic flow of the program or program
termination. During execution of the instruction the state will be stored in
the data structure.
Control flow
The logic of a program may not always be executed in a particular order.
The execution of statements is based on a decision. The basic control
flows needed for writing good and efficient algorithms are, Sequence
Control Flow Selection Control Flow Iteration (Looping) Control Flow These
control flows allow the program to make choices, change direction or
repeat actions.
Sequence Control
Flow In sequence control flow, the instructions are executed in a linear
order one after the other. The instructions in sequence control flow are
executed exactly once.
Example: Algorithm to find the sum of two numbers.
◦ Step 1: Start
◦ Step 2: Read two numbers A and B
◦ Step 3: Calculate sum = A + B
◦ Step 4: Print the sum value
◦ Step 5: Stop.
This algorithm performs the steps in a purely sequential order.
Selection Control Flow
In a selection control flow, the step to be executed next is based on a
decision taken. If the condition is true, one path is followed. If the
condition is false, another path is followed.
Example:
Algorithm to find the greatest among three numbers.
Step 1: Start.
Step 2: Read the three numbers A, B, C.
Step 3: Compare A and B. If A is the greatest perform
step 4 else perform step 5. Step 4: Compare A and C. If A is the greatest,
print “A is the greatest” else print “C is the greatest”.
Step 5: Compare B and C. If B is the greatest, print “B is the greatest”
else print “C is the greatest”.
Step 6: Stop.
Iteration (Looping) Control Flow
In iterative control flow, one or more instructions are executed repeatedly.
In this control flow a condition is checked. Based on the result of this
conditional check (true or false) the control goes back to one of the
already executed steps to make a loop.
Example:
Algorithm to find the sum of first 100 integers.
step1. Start
step2. Assign sum = 0, i = 0.
step3. Calculate i = i + 1 and sum = sum + i
step4. Check whether i>= 100, if no repeat
step 3. Otherwise go to next
step. 5. Print the value of sum
step 6.Stop
Functions
Any complex problem will become simpler if the problem is broken smaller
and the smaller problems are solved. The functions are solutions to
smaller problems. These can be used to solve bigger, complex problems.
During algorithm design the complex problems are divided into smaller
and simpler tasks. These tasks are solved independently by functions. A
function is a block of organized, reusable code that is used to perform a
similar task of some kind. It avoids the repetition of some codes over and
over. It means the programmer need not have to write same instructions
for a number of times, to perform the same action. Functions help easy
debugging, testing and understanding of the program. Functions reduce
program size and the program development time. Functions provide
better modularity and high degree of reusability for the problems.