Algorithm and Flow Chart - Lecture 1
Algorithm and Flow Chart - Lecture 1
1.1 Introduction
1.3 Algorithm
Can you think of a day in your life which goes without problem solving?
Answer to this question is of course, No. In our life we are bound to solve
problems. In our day to day activity such as purchasing something from a
general store and making payments, depositing fee in school, or withdrawing
money from bank account. All these activities involve some kind of problem
solving. It can be said that whatever activity a human being or machine do
for achieving a specified objective comes under problem solving. To make it
clearer, let us see some other examples.
Example1: If you are watching a news channel on your TV and you want to
change it to a sports channel, you need to do something i.e. move to that 2
Here it is necessary to mention that all the problems in the world can not be
solved. There are some problems which have no solution and these problems
are called Open Problems.
If you can solve a given problem then you can also write an algorithm for it.
In next section we will learn what is an algorithm.
3
1.3 ALGORITHM
Algorithm:
Step1: Read\input the Radius r of the Circle
Step2: Area PI*r*r // calculation of area
Step3: Print Area
Problem2: Write an algorithm to read two numbers and find their sum.
First num1.
Second num2.
Expected output:
Algorithm:
Step1: Start
Step2: Read\input the first num1.
Step3: Read\input the second num2.
Step4: Sum num1+num2 // calculation of sum
Step5: Print Sum
Step6: End
5
Problem 3: Convert temperature Fahrenheit to Celsius
Inputs to the algorithm:
Temperature in Fahrenheit
Expected output:
Temperature in Celsius
Algorithm:
Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C 5/9*(F32)
Step 4: Print Temperature in Celsius: C
Step5: End
Type of Algorithms
1. Sequence
2. Branching (Selection)
3. Loop (Repetition)
These three control structures are sufficient for all purposes. The sequence is
exemplified by sequence of statements place one after the other – the one
above or before another gets executed first. In flowcharts, sequence of
statements is usually contained in the rectangular process box.
Step1: Start
Step5: Print C
Step6: End
Step1: Start
Step2: Read/input x
Step5: Print F
Step6: End
Step1: Start
Step7: End
The loop allows a statement or a sequence of statements to be
repeatedly executed based on some loop condition. It is represented
by the ‘while’ and ‘for’ constructs in most programming languages,
for unbounded loops and bounded loops respectively. (Unbounded
loops refer to those whose number of iterations depends on the
eventuality that the termination condition is satisfied; bounded loops
refer to those whose number of iterations is known before-hand.) In
the flowcharts, a back arrow hints the presence of a loop. A trip
around the loop is known as iteration. You must ensure that the
condition for the termination of the looping must be satisfied after
some finite number of iterations, otherwise it ends up as an infinite
loop, a common mistake made by inexperienced programmers. The
loop is also known as the repetition structure.
Examples:
1. Start
2. I ← 0
3. Write I in standard output
4. I ← I+2
5. If (I <=98) then go to line 3
6. End
Problem2: Design an algorithm which gets a natural value, n,as its input and 8
calculates odd numbers equal or less than n. Then write them in the standard
output:
1. Start
2. Read n
3. I ← 1
4. Write I
5. I ← I + 2
6. If ( I <= n) then go to line 4
7. End
1. Start
2. I ← 1000 and S ← 0
3. Write I
4. S ← S + I
5. I ← I + 2
6. If (I <= 2000) then go to line 3
else go to line 7
7. Write S
8. End
S = ½ + ¼ + … +1/n
1. Start
2. Read n
3. I ← 2 and S ← 0
4. S= S + 1/I
5. I ← I + 2
6. If (I <= n) then go to line 4
else write S in standard output
7. End
Combining the use of these control structures, for example, a loop within a
loop (nested loops), a branch within another branch (nested if), a branch 9
within a loop, a loop within a branch, and so forth, is not uncommon.
Complex algorithms may have more complicated logic structure and deep
level of nesting, in which case it is best to demarcate parts of the algorithm
as separate smaller modules. Beginners must train themselves to be
proficient in using and combining control structures appropriately, and go
through the trouble of tracing through the algorithm before they convert it
into code.
Donald Ervin Knuth has given a list of five properties for a,algorithm,
these properties are:
1.4 FLOWCHART
For example suppose you are going for a picnic with your friends then you
plan for the activities you will do there. If you have a plan of activities then
you know clearly when you will do what activity. Similarly when you have a
problem to solve using computer or in other word you need to write a
computer program for a problem then it will be good to draw a flowchart
prior to writing a computer program. Flowchart is drawn according to
defined rules.
11
1.4.1 Flowchart Symbols
1. All boxes of the flowchart are connected with Arrows. (Not lines)
2. Flowchart symbols have an entry point on the top of the symbol with no
other entry points. The exit point for all flowchart symbols is on the bottom
except for the Decision symbol.
3. The Decision symbol has two exit points; these can be on the sides or the
bottom and one side.
• From the bottom of the page to the top of the same page.
flowcharts.
Flowcharting uses symbols that have been in use for a number of years to
13
represent the type of operations and/or processes being performed. The
standardised format provides a common method for people to visualise
problems together in the same manner. The use of standardised symbols
makes the flow charts easier to interpret, however, standardizing symbols is
not as important as the sequence of activities that make up the process.
14
Problem 2: Convert temperature Fahrenheit to Celsius.
Problem3: Flowchart for an algorithm which gets two numbers and prints
sum of their value
15
Problem5: Algorithm for find the greater number between two numbers.
Star
t
Read
A,B
Tru Fals
e If e
A>B
Print Print
A B
EN
D
17
problem8: Flowchart for the calculate the average from 25 exam scores.
Start
Sum=0, C=0
Enter
Exam
Scores,
S
Sum=Sum+
S
C=C+1
N
o Is
C=
25
?
Y
es
Av=Sum/25
Print Av
END
18
1.4.5 Advantages of using Flowcharts
19
Check Your Progress 1
1) What is an algorithm?
2) Explain need of an algorithm?
3) Write an algorithm to find average age of a group of 10 players?
4) Write algorithm to this problem:Ramshewak goes to market for buying
some fruits and vegetables. He ishaving a currency of Rs 500 with him
for marketing. From a shop he purchases 2.0 kg Apple priced Rs. 50.0
per kg, 1.5 kg Mango priced Rs.35.0 per kg, 2.5 kg Potato priced
Rs.10.0 per kg, and 1.0 kg Tomato priced Rs.15 per kg. He gives the
currency of Rs. 500 to the shopkeeper. Find out the amount shopkeeper
will return to Ramshewak. and also tell the total item purchased.
5) Find factorial of N?
6) Explain steps involve in drawing of a flowchart.
7) Explain uses of Flowchart.
8) Draw a flowchart to find the sum of first 100 natural numbers.
9) Draw a flowchart to find the largest of three numbers x, y and z.
10) Draw flowchart for the problem of determining prime number?
1, 1, 2, 3, 5, 8,…?
20