0% found this document useful (0 votes)
115 views40 pages

Algorithm PPT Slides

The document provides an overview of algorithms, defining them as step-by-step instructions for solving problems, and includes various examples such as making tea and swapping values. It also discusses flowcharts as graphical representations of algorithms, control structures like sequential, conditional, and repetitive flows, and introduces pseudocode as a tool for planning program logic. Additionally, it outlines programming techniques such as structured and modular programming.

Uploaded by

shourya9867
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views40 pages

Algorithm PPT Slides

The document provides an overview of algorithms, defining them as step-by-step instructions for solving problems, and includes various examples such as making tea and swapping values. It also discusses flowcharts as graphical representations of algorithms, control structures like sequential, conditional, and repetitive flows, and introduces pseudocode as a tool for planning program logic. Additionally, it outlines programming techniques such as structured and modular programming.

Uploaded by

shourya9867
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ALGORITHMS

What is an algorithm?
• An algorithm is a finite step by step list of well-
defined instructions for solving a particular
problem.
• It is a tool to write the job in simple steps in simple
English language.
• It is not programming language or program.
• It guides the programmer to prepare the program.
How to make a tea

Algorithm to make a tea


1. Boil water in a pot
2. Add sugar and tea powder
in it and boil it for 3-4 mins
3. Add milk and boil it for
another 5 mins
4. Strain tea in cup and enjoy
Algorithm to swap the value of two
variables (A & B)
Step 1: Store the value of A in temporary
variable
t=A
Step 2: transfer B value to A
A=B
Step 3: Transfer t to B
B=t
Step 4: Display the value of A and B
Step 5: Stop
Characteristics of algorithm
• It should be easy and simple in language.
• It should not repeat the task again and again
• Every step must be accurate and complete.
• It can be easily modified if necessary.
• It should be easily understandable to others and steps should be
clear
• It should be economical in the use of computer time, computer
storage.
Algorithm to add two numbers entered by the user
Step 1: Declare variables num1, num2 and sum.
Step 2: Read values num1 and num2.
Step 3: Add num1 and num2 and assign the result to sum.
sum = num1 + num2
Step 4: Display sum
Step 5: Stop
Algorithm to accept marks of 3 subjects and find sum and
average.
Step 1: Declare variables sub1, sub2, sub3, sum and avg.
Step 2: Read values of sub1, sub2 and sub3.
Step 3: Add marks of 3 subjects
sum = sub1 + sub2 + sub3
Step 4: Calculate average
avg = sum / 3
Step 5: Display sum and avg
Step 6: Stop
Algorithm to accept a number and display its square

Step 1: Declare variables A and Sq


Step 2: Read values of A
Step 3: Multiply the number A twice by itself and store in Sq
Sq = A *A
Step 5: Display Sq
Step 6: Stop
Flowcharts
• Flowchart is the graphical/ pictorial representation
of algorithm.
• This is one tool used in the design of program.
• Flowchart consist of set of graphical symbols. Each
symbol represents activity.
Symbols in Flowchart
Start / End

Process

Input or output
More Alternate
paths
Decision

Flow lines
Connector
Flowchart of addition of two numbers

Start
Algorithm to add two numbers entered by the
Declare variable num1, num2 user
and sum Step 1: Declare variables num1, num2 and
sum.
Read num1 and
num2
Step 2: Read values num1 and num2.
Step 3: Add num1 and num2 and assign
Sum = num1 + num2 the result to sum.
sum = num1 + num2
Display sum Step 4: Display sum
Step 5: Stop
Stop
Following rules must be considered while
drawing a flowchart:
1. The standard symbol should be used.
2. The arrows should be used to represent the
direction of flow.
3. Flow lines should not cross each other.
4. Program flow should be top to bottom.
Advantage of Flowchart:

1. Better programming steps


2. Minimum length
3. Systematic analysis
4. Minimization of errors
5. Effective testing and results
6. Easy coding
7. Universal logical solution
Control Structures
1. Sequential flow ( sequential logic)
2. Conditional flow (Selection logic)
3. Repetitive flow (Iteration logic)
Sequential flow ( sequential logic)
• In this modules are executed sequentially, one
after the another.
Module A

Module B

Module C
Conditional flow (Selection logic)
• Selection logic uses number of conditions,
which cause selection of one out of several
alternative modules.
• There are three types of conditional structures
– Single alternative
– Double alternative
– Multiple alternative
Single alternative
• This has the form:
If condition, then: Is No
condition
[module A] ?
[End of if structure] Yes
• If condition is true the Module A
module A, is executed.
• Otherwise, module A is
skipped.
Double alternative
• This has the form:
If condition, then:
Is
[module A] condition
Else: ?
[module B] Yes No
[End of if structure]
• If condition is true the Module A Module B
module A, is executed.
• Otherwise, module B is
executed
Multiple alternative
• This has the form:
• This allows only one module to be
If condition (1), then: executed.
[module A1] • The condition which is true the
else if condition (2), then: corresponding module will be
[module A2] executed.
: • If no condition is satisfied, then
: the module, which follows last
Else statement will be executed.
else if condition (n), then:
[module An]
else:
[module B]
[End of if structure]
Multiple alternative
Algorithm: Largest
The algorithm inputs two number a & b and outputs
the largest number.
Step 1: Declare variables a and b.
Step 2: Read variables a and b.
Step 3: If a > b
Display a is the largest number.
Else
Display b is the largest number.
[End of If structure]
Step 4: Stop
Repetitive flow (Iteration logic)
• Here certain module is executed repeatedly
until condition satisfies.
• There are two repetitive flow structures
– Repeat for loop
– Repeat while
Repeat for loop
• It has the form: KR
Repeat for K = R to S by T
[module] NO
Is K < S ?
[End of for loop]
Here K = Index variable YES
R = initial value Module
S = final value
T = increment KK+T
Repeat while
• It has the form:
Repeat while condition: NO
Is K < S ?
[module]
[End of while loop] YES
Here, module is executed Module
repeatedly, until the
condition is satisfied.
Algorithm to check whether the given number is positive or negative

Step 1: Declare variable N


Step 2: Read value of N
Step 3: If N > = 0 then
Display N is positive number
Else
Display N is negative number
[End of if structure]
Step 4: Stop
Algorithm to check whether the given number is even or odd
Step 1: Declare variable N
Step 2: Read value of N
Step 3: Divide N by 2 and check remainder r
Step 4: If r = 0 then
Display N is even number
Else
Display N is odd number
[End of if structure]
Step 4: Stop
Algorithm to find the sum of N numbers

Step 1: Declare variable count, N and sum


Step 2: Read values of N
Step 3: Set sum = 0 and count = 1
Step 4: Add count to sum
sum = sum + count
Step 5: Increment count by 1
count = count + 1
Step 6: if count <= N then go to step 4
Step 7: Display sum
Step 8: Stop
Algorithm to find factorial of a given number

Step 1: Declare variable count, N and fact


Step 2: Read values of N
Step 3: Set fact = 1 and count = 1
Step 4: Repeat while count <= N
fact = fact * count
Increment count by 1
count = count + 1
[End of while structure]
Step 5: Display fact
Step 6: Stop
Algorithm to find Fibonacci series
Step 1: Declare variable f, s, t, N, count
Step 2: Read values of N
Step 3: Set f = 0, s = 1
Step 4: Display f and s
Step 5: Repeat for count = 3 to N
t=f+s
Display t
f=s
s=t
[End of for structure]
Step 6: Stop
Algorithm to find largest element in array
Largest [DATA, N, MAX]
Here, DATA is a linear array with N elements. This algorithm finds the
largest element MAX of DATA.
1. Set k = 1 and MAX = DATA[1]
2. If MAX < DATA [ k + 1]
Then: MAX = DATA [K+1]
[end of if structure]
3. SET k = k+1
4. If k<N, then go to step 2
5. Write : MAX
6. Stop
Pseudocode
• It is a programming analysis tool used
for planning program logic.
• Pseudo means false and code refers
to the instruction written in
programming language.
• It is an imitation of actual computer
instructions.
• Pseudo instructions are phrases
written in ordinary natural language.
(e.g English, French, German)
Advantages of Pseudocode
• Converting a pseudocode to programming
language is much more easier as compared to
flowchart.
• It’s easy to write pseudocode as compared to
drawing flowchart and actual program because
pseudocode has few rules to follow. So it will
concentrate on logic of program.
Limitations of pseudocode
1. Graphic representation of program logic is not
available in pseudocode.
2. As there are no standard rules for writing
pseudocode, different programmers may use
their own style, which may cause
communication problem.
Programming Techniques
Before programming, user has to decide the
method in which program is to be developed.
There are two basic techniques.
1. Structured programming
2. Modular programming
Structured Programming
In this programming method, the program is developed in
hierarchy of modules where module refers to subprogram
in the main program. It is a top to bottom approach.
In this technique three basic structures are normally
adopted such as:
1. A simple sequence
2. A selection by decision
3. A repetition
Modular programming
• Modular programming reduces rewriting the subprograms.
Each subprogram may be called as “Functions “ or
“Procedures”.
• Each function performs a specific routine, which may be
required many times in the man program.
• All these subprograms are
controlled by a single program.
Each module may be called a
subroutine.

You might also like