algorithm-and-flow-chart 2
algorithm-and-flow-chart 2
1.1 Introduction
1.3 Algorithm
1.3.2 Properties of an
Algorithm
Examples
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 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
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.
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.
Inputs to the algorithm:
First num1.
Second num2.
Expected output:
Sum of the two numbers.
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
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
The algorithm and flowchart, classification to the three types of control
structures. They are:
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
Step2: Read/input x
Step5: Print F
Step6: End
numbers.
Step1: Start
Step2: Read/input A,B and C
Step3: If (A>=B) and (A>=C) then Max=A
Step4: If (B>=A) and (B>=C) then Max=B
Step5:If (C>=A) and (C>=B) then Max=C
Step6: Print Max
7
Step7: End
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
Problem2: Design an algorithm which gets a natural value, n,as its input 8
and 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
Problem3: Design an algorithm which generates even numbers
between 1000 and 2000 and then prints them in the standard output.
It should also print total sum:
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
9
1.4 FLOWCHART
The flowchart is a diagram which visually presents the flow of data
through processing systems. This means by seeing a flow chart one can
know the operations performed and the sequence of these operations in
a system. Algorithms are nothing but sequence of steps for solving
problems. So a flow chart can be used for representing an algorithm. A
flowchart, will describe the operations (and in what sequence) are
required to solve a given problem. You can see a flow chart as a blueprint
of a design you have made for solving a problem.
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
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.
• An upward flow of more then 3 symbols
independent flowcharts.
Flowcharting uses symbols that have been in use for a number of years
13
to represent the type of operations and/or processes being performed.
The
standardised format provides a common method for people to visualise
14
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.
Start
Read A,B
True False
If A>B
Print A Print B
END
Problem7: Flowchart for the problem of printing odd numbers less than a
given number. It should also calculate their sum and count.
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
No
Is C=
25?
Yes
Av=Sum/25
Print Av
END
18
19
20