0% found this document useful (0 votes)
341 views38 pages

Algorithm: Algorithm Is A Step-By-Step Procedure or Sequence of Instruction To Solve A Problem

The document discusses algorithms, pseudocode, and flowcharts. It defines algorithms as step-by-step procedures to solve problems and lists the typical steps as start, input, process, output, and stop. Pseudocode is described as instructions written in plain English to solve a problem, following specific rules. Flowcharts are graphical representations of algorithms that use standard symbols like processes, decisions, and terminals. The document provides examples and discusses the building blocks of algorithms, pseudocode, and flowcharts, including sequence, selection, and iteration control structures.

Uploaded by

siva ganesh
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)
341 views38 pages

Algorithm: Algorithm Is A Step-By-Step Procedure or Sequence of Instruction To Solve A Problem

The document discusses algorithms, pseudocode, and flowcharts. It defines algorithms as step-by-step procedures to solve problems and lists the typical steps as start, input, process, output, and stop. Pseudocode is described as instructions written in plain English to solve a problem, following specific rules. Flowcharts are graphical representations of algorithms that use standard symbols like processes, decisions, and terminals. The document provides examples and discusses the building blocks of algorithms, pseudocode, and flowcharts, including sequence, selection, and iteration control structures.

Uploaded by

siva ganesh
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
You are on page 1/ 38

GE8151- PROBLEM SOLVING AND PYTHON PROGRAMMING

UNIT-I -ALGORITHMIC PROBLEM SOLVING

Algorithm
Definition:

 Algorithm is a step-by-step procedure or sequence of instruction

to solve a problem.

Steps:

1. Start
2. Input
3. Process
4. Output
5. Stop

Example: Algorithm to add two numbers

Step:1 Start

Step:2 Read the values of a ,b

Step:3 Calculate c=a+b

Step:4 Print c

Step:5 Stop

1
Characteristics of algorithms

 In the algorithm each and every instruction should be precise and


unambiguous.
 Ensure that the algorithm will ultimately terminate.
 It look like normal English
 The desired result should be obtained only after the algorithm terminates.

Merits / Advantages of Algorithm

1. It makes the logic easy to read and understand.


2. It makes the program portable and efficient.
3. It displays easy steps of processing.
4. It simplifies the modification and to update of the existing program.
5. It provides the facility for testing a program at developing stage.

Qualities of a good algorithm


 Time
 Memory
 Accuracy
 Sequence
 Generability

Representation of algorithm

 The algorithm can be represented in several ways.


 Generally the programmers follow one of the following ways to represent an
algorithm.
1. Normal English
2. Flowchart
3. Pseudocode
4. Decision tables
2
Pseudocode
Definition:

 The pseudocode is an instructions, which are written in normal English to solve


a problem.
 It is a programming analysis tools, that is commonly used for planning the
program logic, where the name itself specific “pseudo” means false, “code”
means the set of statements written in a programming language.

Rules for writing Pseudocode

 Write one statement per line


 Capitalize initial keyword
 Indent to show hierarchy
 End multiline structure
 Keep statement language independent.

Steps:

Start
READ
Process
WRITE
Stop

3
Example: Pseudocode to student‟s mark details

Start
READ name, rollno, m1, m2, m3

Total = m1+m2+m3

Average = Total/3

WRITE name, rollno, class, total, average

Stop

Advantage

 It can easily read and understood easily.


 Easily modified
 Done easily on Word Processor

Disadvantage

 It is not visual.

 We do not get a picture of the design

 There is no standardized style or format, so one pseudocode may be


different from another.

4
Flowchart

Definition:

 Flowchart is a graphical or diagrammatic or pictorial representation


to solve a problem.

Steps:

START

READ

PROCESS

PRINT

STOP

5
Example: Flowchart to addition of two numbers

START

READ A,B

CALCULATE
C=A+B

PRINT C

STOP

Need for Flowchart


 The program logic is made very easy through the flowchart that have
standardised meaning.

Aim of flowchart
 Program preparation can be simplified using the flowchart.
 Flowcharts are easier to understand at glance, than the narrative description of
algorithm.
 Flowcharts are easy to analyze and compare various methods.

6
Flowchart symbols

S.no Description Symbols

Flowlines

2 Terminal symbol

(Start / Stop)

3 Input/output symbol

4 Process symbol

5 Decision symbol

6 Connectors

7
Rules (Guidelines) for drawing a flowchart

 The flowchart should be clear, neat and easy to follow.


 The usual direction of the flow of a process is from left to right or top to bottom.
 Only one flow line should come to a process symbol and only one flow line
should out from a process symbol.

 Only one flow line should enter a decision symbol, but two or three flow lines,
one for each possible answer, should leave the decision symbol.

< 0 > 0

= 0

 Only one flow line is used in conjunction with terminal symbol.

 The flow lines should not cross each other.


 If a new page is needed for flowcharting, then use connectors for better
representation.

8
Advantage of flowchart

i. To understanding logic clearly


ii. Better communication
iii. Effective analysis
iv. Effective synthesis
v. Effective coding
vi. Proper program documentation
vii. Systematic debugging
viii. Systematic testing

Limitation (Disadvantage) of using flowchart

 Hard to modify
 Complex logic
 Alternations and modifications
 Reproduction

9
Building blocks of Algorithm, Pseudocode and Flowcharts
(or)
Program control structures of Algorithm, Pseudocode and Flowcharts

 There are three types

i. Sequence control structure

ii. Selection control structure

iii. Iteration control structure

i. Sequence control structure

 The sequence logic is used for performing instruction one after another.

(a)Algorithm (b)pseudocode (c) Flowchart


Process1 Process1
Process 1
. .
Process2 Process2 Process 2
. .
. . Process n

Process n Process n

10
Example:

(a)Algorithm to add two numbers

Step:1 Start the program

Step:2 Read the values of a ,b

Step:3 Calculate c=a+b

Step:4 Print c

Step:5 Stop the program

Example:

(b)Pseudocode to add two integers

Start

READ the values of a ,b

Calculate c=a+b

WRITE c

Stop

11
Example:

(c)Flowchart to addition of two numbers

START

READ A,B

CALCULATE
C=A+B

PRINT C

STOP

12
ii. Selection control structure (or) Decision structure

 The selection control structure is the presentation of a condition and the choice
between two actions; the choice depends on whether the condition is true or false
 It is usually depicted as an IF…….THEN or IF…………….THEN….ELSE.

(a)Algorithm (b)Pseudocode
Start Start
Read READ
IF condition THEN IF condition THEN
. Print process 1 . WRITE process 1
ELSE ELSE
Print process 2 WRITE Process 2
. .
END IF END IF
Stop Stop

13
(c)Flowchart

START

READ

IF
No Yes
Condition

PRINT PRINT

Process-2 Process-1

STOP

14
Example:

(a) Algorithm to given number is ODD or EVEN

Step:1 Start the program

Step:2 Read the value of N

Step:3 IF (N % 2 = = 0) THEN

Print “ Even number”

ELSE

Print “Odd number”

Step:4 Stop the program

Example:

(b) Pseudocode to given number is ODD or EVEN

Start

READ the value of N

IF (N % 2 = = 0) THEN

WRITE “ Even number”

ELSE

WRITE “Odd number”

Step:4 Stop

15
Example:

(c) Flowchart to given number is ODD or EVEN

START

READ
N

IF
No Yes
(N%2= 0)

PRINT PRINT

Process-2 Process-1

STOP

16
iii. Iteration control structure (Looping or Repetition)

 The repetition control structure can be defined as the presentation of a set of


instructions to be performed repeatedly, as long as a condition is true
 The basic idea of repetitive code is that a block of statements is executed again
and again, until a terminating condition occurs
 It uses two structures called the WHILE and the DO……WHILE, both of these
structure are used for looping.

 The looping continues until the condition becomes true.

(*) Top tested loop

 The top tested loops are leading decision is evaluated always at the first statement
of the loop.
Pseudocode
Algorithm

Start
Start
.
.
.
.
WHILE condition
WHILE condition
.
.
.
.
.
.
Body of the loop
Body of the loop
.
.
.
.
.
.
ENDWHILE
ENDWHILE
Stop
Stop

17
Flowchart

While No
(condition)

Yes

Body of the loop

18
(*) Bottom tested loop

 This loop is a trailing decision loop, means that the decision is always at last
statement of the loop.
 The body of the loop will be executed once before control even gets to the loop
test.
Algorithm Pseudocode

Start Start
. .
. .
Body of the loop Body of the loop
. .
. .
WHILE condition WHILE condition

END WHILE END WHILE

Stop Stop

Flowchart

Body of the loop

Yes While

(condition)

No
19
Example:

(a)Write an algorithm to find sum of digits

Step:1 Start the program

Step:2 Read the value of n , sum = 0

Step:3 WHILE (n > 0)


r=n%10
sum =sum+r
r=n //10
END WHILE
Print sum
Step:4 Stop the program

Example:

(b)Write a pseudocode to find sum of digits

Start

READ n, sum=0

WHILE (n > 0)
r=n%10
sum =sum+r
r=n //10
END WHILE
WRITE sum
Stop

20
Example:

(c)Draw a flowchart to find sum of digits

Start

Read n ,

sum =0

WHILE No
(n >0)

Yes

r=n%10
Yes
sum =sum+r
r=n //10

Print sum

Stop

21
Exercise
1. Write the Algorithm, Pseudocode , and Draw the Flowchart to Addition of two
numbers

Algorithm:

Step 1: Start

Step 2: Read the values of a and b.

Step 3: Calculate c =a + b

Step 4: Print c

Step 5: Stop.

Pseudocode: Flowchart

Start

READ the value for a , b.

c= a+b

WRITE c

Stop

22
2. Write the Algorithm, Pseudocode , and Draw the Flowchart to find the sum and
product of two numbers

Algorithm:

Step 1: Start

Step 2: Read the values of a and b.

Step 3: Calculate c =a + b ,

d =a * b

Step 4: Print c, d

Step 5: Stop.

Pseudocode: Flowchart
Start

READ the value for a , b.

c= a+b

d=a*b

WRITE c, d

Stop

23
4. Write the Algorithm, Pseudocode , and Draw the Flowchart to convert temperature in
Fahrenheit to Centigrade

Algorithm:
Step 1: Start

Step 2: Read the value of f.

Step 3: Calculate c =5 / 9 (f - 32).

Step 4: Print c

Step 5: Stop.

Pseudocode: Flowchart
Start
READ the value of f.

c =5 / 9 (f - 32)

WRITE c

Stop

24
3. Write the Algorithm, Pseudocode , and Draw the Flowchart to find the biggest of
given two numbers

Algorithm:
Step 1: Start

Step 2: Read the values of a and b.

Step 3: IF (a>b)

THEN Print “ a is big”

ELSE Print “ b is big”

Step 4: Stop.

Pseudocode: Flowchart:
Start

READ the value for a , b.

IF (a>b) THEN WRITE „a is big‟

ELSE WRITE „b is big‟

ENDIF

Stop

25
4. Write the Algorithm, Pseudocode , and Draw the Flowchart to find roots of quadratic
equation

Algorithm:

Step 1: Start the program

Step 2: Read the value of a, b, c

Step 3: Calculate d=(b * b) - (4 * a * c)

Step 4: if( d > 0 )

begin

Print “THE ROOTS ARE REAL ROOTS”

R1 ← (-b + d) / (2*a)

R2 ← (-b - d) / (2*a)

Print R1, R2

end

else if(d= = 0)

begin

Print “ THE ROOTS ARE REPEATED ROOTS"

R1 ← -b / (2*a)

R2 ← -b / (2*a)

Print R1

Print R2

end

else

Print “THE ROOTS ARE IMAGINARY ROOTS”

Step 5: Stop the program

26
Pseudocode:

Step 1: Start

Step 2: READ the value of a, b, c

Step 3: Calculate d=pow(b * b) - (4 * a * c)

Step 4: if( d > 0 )

begin

WRITE “THE ROOTS ARE REAL ROOTS”

R1 ← (-b + d) / (2*a)

R2 ← (-b - d) / (2*a)

WRITE R1, R2

end

else if(d= = 0)

begin

WRITE “ THE ROOTS ARE REPEATED ROOTS"

R1 ← -b / (2*a)

R2 ← -b / (2*a)

WRITE R1

WRITE R2

end

else

WRITE “THE ROOTS ARE IMAGINARY ROOTS”

Step 5: Stop

27
Flowchart:
Start

Read a,b,c

d=pow(b * b) - (4 * a * c)

False
if( d > 0 )

False True
if(d= = 0)
R1 ← (-b + d) / (2*a)

True R2 ← (-b - d) / (2*a)

R1 ← -b / (2*a)

R2 ← -b / (2*a)
Print R1, R2

Print
Imaginary
Stop

28
ILLUSTRATIVE PROBLEMS

Tower of Hanoi

 Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs)


and more than one rings is as depicted –

 These rings are of different sizes and stacked upon in an ascending order, i.e. the
smaller one sits over the larger one.

Rules

 The mission is to move all the disks to some another tower without violating the
sequence of arrangement.

 Only one disk can be moved among the towers at any given time.
 Only the "top" disk can be removed.
 No large disk can sit over a small disk.

29
Algorithm

 If we have 3 disks (Source, Destination and Auxiliary)


 The steps to follow are

 Steps 1 - First, we move the smaller (top) disk to auxiliary tower.


 Step 2 - Then, we move the larger (bottom) disk to destination tower.
 Step 3 − And finally, we move the smaller disk from auxiliary to
destination tower.

(or)

 Step 1 − Move n-1 disks from source to aux

 Step 2 − Move nth disk from source to dest

 Step 3 − Move n-1 disks from aux to dest

 Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps.
 This presentation shows that a puzzle with 3 disks has taken 23 - 1 = 7 steps.

30
Step-1 Step-2

Step-3 Step-4

Step-5 Step-6

Step-7

31
 A recursive algorithm for Tower of Hanoi can be driven as follows

START
Procedure Hanoi(disk, source, destination, auxiliary)

IF disk == 1, THEN
move disk from source to destination
ELSE
Hanoi(disk - 1, source, auxiliary, destination) // Step 1
move disk from source to destination // Step 2
Hanoi(disk - 1, auxiliary, destination, source) // Step 3
END IF

END Procedure
STOP

32
Find Minimum in a list

Problem description:

 Minimum in a list of element can be achieved in different ways.


 One ways is to sort the list of element in ascending order and get the first element
as minimum.
 Sorting algorithms are used to find Minimum in a list of element
 Assume the first element as the minimum and start comparing with the next
(second) element.
 If the next element is smaller then assume second the minimum and keep
repeating the procedure till the last element.

Algorithm:

Step 1: Get the list of elements

Step 2: Assume first element as MIN

Step 3: Compare MIN with next element

Step 4: If MIN is greater than next element; set MIN=next element

Step 5: Repeat step-3 and step-4 till last element

Step 6: Display MIN as the minimum element of the list

33
Insert a card in a list of sorted cards

Problem description:

 In general the sorting is in ascending order and sorted cards would be either
playing card with numbers or flash cards with alphabets.
 In that case, a person would check the new card‟s number or alphabets with set
of card sorted .
 All the least cards will be picked out and when a card with greater number /
alphabet is encountered; the new card is placed above the greater card and the
picked out card will be replaced above the new card.
 In case of playing cards which have Jack, Queen and King along with one to ten
numbers; the type of card, numbers and alphabets must be considered.
 The below algorithm considers the playing cards

Algorithm:

Step 1: Get the sorted cards

Step 2: Get card to be inserted - Insert _ Card

Step 3: Consider first card for comparison; Current_card

Step 4: Compare Current_card with Insert _ Card

Step 5: If type is not same;

Step 5.1 : Pick out 13 cards

Step 5.2 : Consider the first card new of new as Current_card

Step 6: If type is same;

Step 6.1 : If Insert _ Card is a number card compare the numbers in the
list and place in the right order.
Step 6.2 : If Insert _ Card is a role card compare JACK, QUEEN and

King order and place in the right order.


34
Guess an integer number in a range

Problem description:

 The player B can start guessing from the starting number of range till he reaches
the correct number.
 This approaches is called linear search, because the guesses all the numbers in
sequence.
 But it takes several guesses and hence is not efficient.
 If player A can give the player B clue on whether the number he as guessed is
lesser or greater than what he thought of it would be reduced range of numbers
into half.
 Keep going, always eliminating half of the reaming numbers. This approach is
called Binary search.
 For a range of 1to 3, player B can find a number in at most 5 guesses with this
technique.

Algorithm:

Step 1: Get the range – start and end

Step 2: Get the guess number G and thought number T

Step 3: If G is lesser than T then set start as G

Step 4: If G is greater than T then set end as G

Step 5: Repeat Step -3 and Step- 4 until G and T are the same TCS codevita

problems

35
Steps in Problem Solving

1. Program planning method

 Program- A program is a set of instruction written to carryout a particular task,


so that computer can follow them.
 Problem solving is a process and part of the large problem process that includes
problem finding and problem solving.
 The steps in the problem solving process are:

Requirement Analysis

Design

Coding

Software Testing

Documentation

Maintenance

36
2. Waterfall Method

 The first System Development Life Cycle (SDLC) method and it describes the
various phase involved in development.

Feasibility
Analysis
Design
Implementation
Testing
Maintenance

Feasibility:

 The feasibility study is used to determine if the project should get the go-ahead.
 Budget estimates for the future stages of development.

Requirement Analysis and Design:

 Analysis gather requirement for the systems.


 Design focuses on high design like, what programs are needed and how are they
going to interact, low-level design, interface design are the interface design and
data design.

Implementation:

 The designs are translated into code. Computer programs are written using a
conventional programming language generator.
 Programming tools like compilers, interpreters, Debuggers are used to generate
the code.

37
Testing:

 These subjects to separate and detailed test and the system are then tested as a
whole.
 The separate modules are brought together and tested as a complete system.
Maintenance:

 Generally the system will need maintenance.


 The software should be developed to accommodate changes that could happen
during the post implementation period.

3. Purpose of Program planning

 If you have been asked to solve programs then you cannot write the program
unless you know the logic of the problem.
 So before writing any program you should know the concept of the program and
write the logical steps for the given program in correct sequence.
 There are five ways to represent the logical steps for finding the solution to given
problem.
1. Algorithms
2. Flowchart
3. Pseudocode
4. Decision table
5. Program

38

You might also like