Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
207 views

Module - 02 - Algorithm and Flow Charts

1) An algorithm is a sequence of unambiguous steps to solve a problem with a clear beginning and end, while a flowchart visually represents the steps of an algorithm using standard symbols. 2) The three basic control structures for algorithms are sequence, branching, and looping. Sequence involves steps executed in order, branching uses conditional statements, and looping repeats steps until a condition is met. 3) Key properties of algorithms include being finite, definite, requiring inputs, producing outputs related to inputs, and using basic operations that could in theory be done with pen and paper. Flowcharts help define the steps and sequence of an algorithm using standard symbols like processes, decisions, and connectors.

Uploaded by

Ride Relax
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views

Module - 02 - Algorithm and Flow Charts

1) An algorithm is a sequence of unambiguous steps to solve a problem with a clear beginning and end, while a flowchart visually represents the steps of an algorithm using standard symbols. 2) The three basic control structures for algorithms are sequence, branching, and looping. Sequence involves steps executed in order, branching uses conditional statements, and looping repeats steps until a condition is met. 3) Key properties of algorithms include being finite, definite, requiring inputs, producing outputs related to inputs, and using basic operations that could in theory be done with pen and paper. Flowcharts help define the steps and sequence of an algorithm using standard symbols like processes, decisions, and connectors.

Uploaded by

Ride Relax
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1

Algorithm and Flow charts


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.
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:
 Getting specified output is essential after algorithm is executed.
 One will get output only if algorithm stops after finite time.
 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.
While writing algorithms we will use following symbol for different operations:
‘+’ for Addition
‘-’ for Subtraction
‘*’ for Multiplication
‘/’ for Division and
‘ ’ for assignment.
For example: a x*3 means A will have a value of x*3.
Example of Algorithm:
Problem 1: Find the area of a Circle of radius r.

Inputs to the algorithm: Radius r of the Circle.


Expected output: Area of the Circle

Algorithms and Flowcharts Module 02


2

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 number.


Second number.
Expected output: Sum of the two numbers.

Algorithm:
Step1: Start
Step2: Read num1 //input the first num1.
Step3: Read num2 //input the second num2.
Step4: Sum num1+num2 // calculation of sum
Step5: Print Sum
Step6: End

Problem 3: Convert temperature Fahrenheit to Celsius

Inputs to the algorithm: Temperature in Fahrenheit


Expected output: Temperature in Celsius

Algorithm:
Step 1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C (5/9)*(F-32)
Step 4: Print Temperature in Celsius: C
Step5: End

Algorithms and Flowcharts Module 02


3

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.

Sequence: 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.
The branch refers to a binary decision based on some condition. If the condition
is true, one of the two branches is explored; if the condition is false, the other
alternative is taken. This is usually represented by the ‘if-then’ construct in
pseudo-codes and programs. In flowcharts, this is represented by the diamond-
shaped decision box. This structure is also known as the selection structure.

Problem-1: write algorithm to find the greater number between two numbers
Inputs to the algorithm: Number 1(A) and Number 2(B)
Expected output: Greater number between A and B
Algorithm:
Step1: Start
Step2: Read //input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End
Problem-2: An algorithm to find the largest value of any three numbers.
Inputs to the algorithm: Number 1(A), Number 2(B) and Number 3(C)
Expected output: Greatest number between A, B and C

Algorithms and Flowcharts Module 02


4

Algorithm:
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
Step7: End

Loop: 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:
Problem1: An algorithm to calculate even numbers between 0 and 99
Algorithm:
Step1: Start
Step2: I ← 0
Step3: Write I in standard output

Algorithms and Flowcharts Module 02


5

Step4: I ← I+2
Step5: If (I <=98) then go to step 3
Step6: End
Problem2: 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:
Algorithm:
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

Properties of algorithm:

Donald Ervin Knuth has given a list of five properties for an algorithm. These
properties are:
1) Finiteness: An algorithm must always terminate after a finite number of
steps. It means after every step one reach closer to solution of the problem and
after a finite number of steps algorithm reaches to an end point.
2) Definiteness: Each step of an algorithm must be precisely defined. It is done
by well thought actions to be performed at each step of the algorithm. Also the
actions are defined unambiguously for each activity in the algorithm.
3) Input: Any operation you perform need some beginning value/quantities
associated with different activities in the operation. So the value/quantities are
given to the algorithm before it begins.
4) Output: One always expects output/result (expected value/quantities) in
terms of output from an algorithm. The result may be obtained at different stages
of the algorithm. If some result is from the intermediate stage of the operation
then it is known as intermediate result and result obtained at the end of algorithm

Algorithms and Flowcharts Module 02


6

is known as end result. The output is expected value/quantities always have a


specified relation to the inputs
5) Effectiveness: Algorithms to be developed/written using basic operations.
Actually operations should be basic, so that even they can in principle be done
exactly and in a finite amount of time by a person, by using paper and pencil only.

Flowchart: A pictorial representation of an algorithm is called a Flow chart. 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.
Flowchart Symbols: There are six basic symbols commonly used
in flow charting of assembly language Programs: Terminal, Process, and
input/output, Decision, Connector and Predefined Process. This is not a complete
list of all the possible flowcharting symbols, it is the ones used most often in the
structure of Assembly language programming.

Algorithms and Flowcharts Module 02


7

Examples of Flowchart:
Problem1: Find the area of a circle of radius r.

Problem 2: Convert temperature Fahrenheit to Celsius.

Algorithms and Flowcharts Module 02


8

Problem 3: Algorithm for find the greater number between two numbers.

Problem 4: Flowchart for the problem of printing even numbers between 9 and
100.

Algorithms and Flowcharts Module 02


9

Decision Structures: Creating a flowchart for decision making helps people to


make a decision correctly and quickly. Decision making is a daily activity for any
human being and business organizations. A flowchart is the graphical
representation of a process, people who need to make a decision will do in a same
easy way if there is a flowchart for decision making in place. They can also
remove any duplicated steps as all steps are visible in the flowchart.
The expression A>B is a logical expression that describes a condition we want to
test.
 If A>B is true (if A is greater than B) we take the action on left
Print the value of A
 If A>B is false (if A is not greater than B) we take the action on right
Print the value of B

Yes No
is
A>B

Print A Print B

1. IF–THEN–ELSE STRUCTURE: The structure is as follows-


if (condition) then
True alternative
else
False alternative
endif

Algorithms and Flowcharts Module 02


10

The algorithm for the flowchart is as follows:


If A>B then
print A Yes No
is
else A>B

print B
endif Print A Print B

2. NESTED IFS:
 One of the alternatives within an IF–THEN–ELSE statement
 may involve further IF–THEN–ELSE statement
Example: Write an algorithm that reads three numbers and prints the value
of the largest number.

Algorithm:

Step 1: Input N1, N2, N3


Step 2: if (N1>N2) then
if (N1>N3) then
MAX N1 //[N1>N2, N1>N3]
else
MAX N3 //[N3>N1>N2]
endif
else
if (N2>N3) then
MAX N2 //[N2>N1, N2>N3]
else
MAX N3 //[N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX

Algorithms and Flowcharts Module 02


11

Flowchart:

Exercise 01 Determine and Output Whether Number N is even or odd.


Algorithm: Flowchart:
Step 1: Read number N,
Step 2: Set remainder as N modulo 2,
Step 3: If remainder is equal to 0 then
number N is even,
else
number N is odd,
Step 4: Print output.

Algorithms and Flowcharts Module 02


12

Exercise 02. Draw an algorithm and flowchart to log in to Facebook account.


Algorithm:
1. Enter www.facebook.com in your browser. (I/O)
2. facebook Home page loads (PROCESS)
3. Enter your Email ID and Password (I/O)
4. Is Email ID and Password Valid (DECISION)
if NO then
Log in error (PROCESS)
go to step 3
else
Display Facebook Account (I/O)
Stop

Flowchart:

Algorithms and Flowcharts Module 02


13

Exercise 03. Draw an algorithm & flowchart to find Even number in between 1
to 50
Algorithm: Flowchart:
Step 1: Start
Step 2: I = 1
Step 3: IF (I >50) THEN
GO TO Step-7
END IF
Step 4: IF ( (I % 2) =0) THEN
Display I
END IF
Step 5: I = I + 1
Step 6: GO TO Step-3
Step 7: Stop
Exercise 04. Draw an algorithm & flowchart to find sum of series 1+2+3+…..+N
Algorithm: Flowchart:
Step 1: Start
Step 2: Input Value of N
Step 3: I = 1, SUM=0
Step 4: IF (I >N) THEN
GO TO Step-8
ENDIF
Step 5: SUM = SUM + I
Step 6: I = I + 1
Step 7: Go to step-4
Step 8: Display value of SUM
Step 9: Stop

Algorithms and Flowcharts Module 02


14

Advantages of using Flowcharts:


The pictorial representation called flowchart of a solution/system is having many
advantages. These advantages are as follows:
1. Communication: A Flowchart can be used as a better way of
communication of the logic of a system and steps involve in the solution, to
all concerned particularly to the client of system.
2. Effective analysis: A flowchart of a problem can be used for effective
analysis of the problem.
3. Documentation of Program/System: Program flowcharts are a vital part of
a good program documentation. Program document is used for various
purposes like knowing the components in the program, complexity of the
program etc.
4. Efficient Program Maintenance: Once a program is developed and becomes
operational it needs time to time maintenance. With help of flowchart
maintenance become easier.
5. Coding of the Program: Any design of solution of a problem is finally
converted into computer program. Writing code referring the flowchart of
the solution become easy.

----------------------------------------0--------------------------------------------

Algorithms and Flowcharts Module 02

You might also like