Algorithm and FlowChart (Week 01 & 02)
Algorithm and FlowChart (Week 01 & 02)
1.1 Introduction
1.2 Problem Solving
1.3 Algorithm
1.1 INTRODUCTION
Intelligence is one of the key characteristics which differentiate a human
being from other living creatures on the earth. Basic intelligence covers
day to day problem solving and making strategies to handle different
situations which
1
Computer Programming Basics
ALGORITHM AND FLOW CHART | Lecture 1
keep arising in day to day life. One person goes Bank to withdraw money.
After knowing the balance in his account, he/she decides to with draw the
entire amount from his account but he/she has to leave minimum balance in
his account. Here deciding about how much amount he/she may with draw
from the account is one of the examples of the basic intelligence. During the
process of solving any problem, one tries to find the necessary steps to be
taken in a sequence. In this Unit you will develop your understanding about
problem solving and approaches.
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
channel by pressing that channel number on your remote. This is a kind of
problem solving.
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 can be defined as: “A sequence of activities to be processed for
getting desired output from a given input.”
Webopedia defines an algorithm as: “A formula or set of steps for solving a
particular problem. To be an algorithm, a set of rules must be unambiguous
and have a clear stopping point”. There may be more than one way to solve
a problem, so there may be more than one algorithm for a problem.
Now, if we take definition of algorithm as: “A sequence of activities to be
processed for getting desired output from a given input.” Then we can say
that:
1. Getting specified output is essential after algorithm is
executed. 2. One will get output only if algorithm stops after
finite time.
3. Activities in an algorithm to be clearly defined in other words for it to be
unambiguous.
Before writing an algorithm for a problem, one should find out what is/are
the inputs to the algorithm and what is/are expected output after running
the algorithm. Now let us take some exercises to develop an algorithm for
some simple problems: While writing algorithms we will use following
symbol for different operations:
Algorithm:
Step1: Read\input the Radius r of the Circle
Step2: Area 🡨 PI*r*r // calculation of area
Step3: Display Area
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: Display Sum
Step6: End
Algorithm:
Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C 🡨 5/9*(F-32)
Step 4: Display Temperature in Celsius: C
Step5: End
Type of Algorithms
1. Sequence
2. Branching (Selection)
6
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.
Step5: Display C
Step6: End
Step1: Start
Step2: Read/input x
Step3: If X less than zero, then F=-X
Step6: End
Problem3: An algorithm to find the largest value of any three
Step7: End
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:
Problem1: An algorithm to calculate even numbers between 0 and 99
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
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
Problem4: Design an algorithm with a natural number, n, as its input which
calculates the following formula and writes the result in the standard
output:
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
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 10
11
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.
13
Computer Programming Basics
ALGORITHM AND FLOW CHART | Lecture 1
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.
7. All flow charts start with a Terminal or Predefined Process (for interrupt
programs or subroutines) symbol.
14
Flowcharting uses symbols that have been in use for a number of years to
represent the type of operations and/or processes being performed. The
standardized format provides a common method for people to visualize
problems together in the same manner. The use of standardized 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.
Problem3: Flowchart for an algorithm which gets two numbers and prints
sum of their value
16
Start
Read A,B
Print A END
Print B
Problem7: Flowchart for the problem of printing odd numbers less than a
given number. It should also calculate their sum and count.
18
Sum=0, C=0
Enter Exam
Scores, S
Sum=Sum + S
C=C+1
Is C=
No
25?
Yes
Av=Sum/25
Print Av 19
END
20