Home | | Problem Solving and Python Programming | Example Programming Algorithm, Pseudocode, Flowchart

Example Programming Algorithm, Pseudocode, Flowchart

Problem Solving and Python Programming : Algorithmic Problem Solving

ILLUSTRATIVE PROBLEM

1. Guess an integer in a range

Algorithm:

Step1: Start

Step 2: Declare hidden, guess

Step 3: Compute hidden= Choose a random value in a range

Step 5: If guess=hidden, then

Print Guess is hit

Else

Print Guess not hit

Print hidden

Step 6: Stop

Pseudocode:

BEGIN

COMPUTE hidden=random value in range

IF guess=hidden, then

PRINT Guess is hit

ELSE

PRINT Guess not hit

PRINT hidden

END IF-ELSE

END

2. Find minimum in a list

Algorithm:

Step 1: Start

Step 3:Initialize i=0

Step 4: If i<n, then goto

step 4.1, 4.2 else goto

Step 4.2: i=i+1 goto step 4

Step 5: Compute min=a[0]

Step 6: Initialize i=1

Step 7: If i<n, then go to step 8 else goto step 10

Step 8: If a[i]<min, then goto

step 8.1,8.2 else goto 8.2

Step 8.1: min=a[i]

Step 8.2: i=i+1 goto 7

Step 9: Print min

Step 10: Stop

Pseudocode:

FOR i=0 to n, then

INCREMENT i

END FOR

COMPUTE min=a[0]

FOR i=1 to n, then

IF a[i]<min, then

CALCULATE min=a[i]

INCREMENT i

ELSE

INCREMENT i

END IF-ELSE

END FOR

PRINT min

END

3. Insert a card in a list of sorted cards

Algorithm:

Step 1: Start

Step 3:Initialize i=0

Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5

Step 4.2: i=i+1 goto step 4

Step 6: Calculate i=n-1

Step 7: If i>=0 and item<a[i], then go to step 7.1, 7.2 else goto step 8

Step 7.1: a[i+1]=a[i]

Step 7.2: i=i-1 goto step 7

Step 8: Compute a[i+1]=item

Step 9: Compute n=n+1

Step 10: If i<n, then goto step 10.1, 10.2  lse goto st 11

Step10.1: Print a[i]

Step10.2: i=i+1 goto step 10

Step 11: Stop

Pseudocode:

BEGIN

FOR i=0 to n, then

INCREMENT i

END FOR

FOR i=n-1 to 0 and item<a[i], then

CALCULATE a[i+1]=a[i]

DECREMENT i

END FOR

COMPUTE a[i+1]=a[i]

COMPUTE n=n+1

FOR i=0 to n, then

PRINT a[i]

INCREMENT i

END FOR

END

4. Tower of Hanoi

Algorithm:

Step 1: Start

Step 3: Calculate move=pow(2,n)-1

Step 4: Function call T(n,Beg,Aux,End) recursively until n=0

Step 4.1: If n=0, then goto

step 5 else goto step

4.2 Step

4.2: T(n-1,Beg,End,Aux) T(1,Beg,Aux,End) , Move disk from source to desti ation T(n-1,Aux,Beg,End)

Step 5: Stop

Pseudcode:

BEGIN

CALCULATE move=pow(2,n)-1

FUNCTION T(n,Beg,Aux,End) Recursiv ly until n=0

PROCEDURE

IF n=0 then,

No disk to move

Else

T(n-1,Beg,End,Aux)

T(1,Beg,Aux,End), move  isk from source to destination

T(n-1,Aux,Beg,En )

END PROCEDURE

END

Procedure to solve Tower of Hanoi

The goal of the puzzle is to move all the disks from leftmost peg to rightmost peg.

1.        Move only one disk at a time.

2.        A larger disk may not be p1aced on top of a smaller disk. For example, consider n=3 disks

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Algorithmic Problem Solving : Example Programming Algorithm, Pseudocode, Flowchart |