Flowchart and Algorithm Basics
Flowchart and Algorithm Basics
INTRODUCTION
A computer program is a sequential set of instructions written in a computer language that
directs the computer to perform a specific computational task.
The objective of programming is to solve problems using computers quickly and accurately.
A set of steps that generates a finite sequence of elementary computational operations leading to
the solution of a given problem is called an algorithm.
The textual description of an algorithm may not be easily understood. This is why a pictorial
representation may be used as a substitute for an algorithm. Such a pictorial representation is
called a flowchart.
The following are the standard symbols used in program flowcharts:
Flowchart and Algorithm Basics
Problem 1.1. Draw a flowchart to show how the sum of two numbers can be obtained.
A sequence structure shows simple input, output, and process operations.
Problem 1.2. Construct a flowchart to show the procedure for obtaining the average of two
given numbers.
Task Analysis. From the concept
of determining the average of two
given numbers, we know that the
given numbers must be added
together to obtain the sum first;
the sum is then divided by 2 to
obtain the average. The flowchart
for Problem 1.2 illustrates this
idea.
Flowchart and Algorithm Basics
The algorithm corresponding to Problem 1.2 is shown below:
Problem 1.3. Construct a flowchart to show how to obtain the volume of a rectangular box.
Task Analysis. We know that the formula to determine the volume of a rectangular box is
Volume = Length × Breadth × Height. To determine the volume of a rectangular box, we need
to know the length, breadth, and height of the box. When these values are multiplied together, the
product represents the desired volume.
Flowchart and Algorithm Basics
Problem 1.4. Construct a flowchart to show how to obtain the daily wage of a worker based on
the hours worked during the day.
Task Analysis. The daily wage depends on two factors: the hours worked and the hourly rate of
pay. When the hours worked are multiplied by the rate of pay, the product represents the wage of
the worker.
The
algorithm for the solution of Problem 1.4. is given below:
Problem 1.5. Construct a flowchart to show how to obtain the area of a triangle on the basis of
the base and height.
Task Analysis. We know that the formula to find the area of a triangle is
Area = 1/2 × base × height
The inputs required to obtain the area of a triangle are its base and height. We can then put the
values in the above formula to obtain the area.
Flowchart and Algorithm Basics
Problem 1.6. Develop a flowchart to show the steps in finding the simple interest on a given
amount at a given rate of interest.
Task Analysis. We know that if P is the principal, R is the rate of interest, and T is the term in
years, then the simple interest I is given by the formula
I = (P*R*T)/100
To determine the simple interest on a given amount, we need the principal amount (P), the rate of
interest (R), and the term in years (T). By putting the values in the formula above, we get the
desired simple interest.
Flowchart and Algorithm Basics
Problem 1.7. If P amount of money is invested for N years at an annual rate of interest I, the
money grows to an amount T, where T is given by T = P (1 + I/100)N.
Draw a flowchart to show how T is determined.
Task Analysis. The solution to this problem is very simple, and it is similar to the preceding one.
The inputs required are the values for P, I, and N. The output T can then be obtained by putting
the values in the formula.
Problem 1.8. Construct a flowchart to show how a student’s registration number and grades in 3
subjects, m1, m2, and m3, are displayed along with the total average grade.
Task Analysis. The data supplied as inputs are the registration number and grades obtained in
three subjects. The registration number contributes nothing to the process of deriving the desired
output; it just identifies the person about whom the total grade and the average grade are obtained.
The total grade can be obtained by taking the sum of the marks m1, m2, and m3, and the average
can be obtained by dividing the total by 3. The steps are illustrated below.
The algorithm corresponding to the above problem is given below:
Flowchart and Algorithm Basics
Problem 1.9. Draw a flowchart to accept the item’s code, stock on hand, and
the rate per unit of stock in a department store, and display the stock value of the store.
Task Analysis. The inputs required to determine the stock value of the store are the stock on
hand and the rate per unit of stock, which are multiplied together to determine the stock value.
The item’s code is used as the identification data.
The algorithm corresponding to the solution for Problem 1.9 is as follows:
Flowchart and Algorithm Basics
Flowchart and Algorithm Basics
Problem 1.11. Draw a flowchart to show how to interchange the values of two variables.
Task Analysis. The task of interchanging the values of two variables implies that the values
contained by the variables are to be exchanged i.e., the data value contained by the first variable
should be contained by the second variable and that by the second variable should be contained by
the first variable. If A and B are two variables, and if the values contained by them are 10 and 20
respectively, the problem is to make the contents of A and B, 20 and 10, respectively. This can be
done simply with the help of a third variable used as an intermediate variable. The third variable
holds the value of either A or B, so that if the value of one variable is assigned to the other, the
assignee’s value is not lost forever but is available in the intermediate variable. Hence, it can then
be assigned to the other variable.
Flowchart and Algorithm Basics
The algorithm of the problem is as follows:
Flowchart and Algorithm Basics
Flowchart and Algorithm Basics