1. Algorithms
1. Algorithms
Step 1 : Start
Step 2 : Print “Good Morning”
Step 3 : Stop
2. Algorithm for planting a Seed
Step 1 : Start
Step 2 : Take a Pot
Step 3 : Fill pot with Soil
Step 4 : Poke hole in Soil
Step 5 : Put Seed in hole
Step 6 : Cover Seed with Soil
Step 7 : Water the Pot
Step 8 : Put pot in Sun-light
Step 9 : Stop
Algorithm for planting a Seed
3. Design an algorithm to add two numbers and display
the result
Step 1 − START
Step 2 − Declare three integers a, b & sum
Step 3 − Define values of a & b
Step 4 − Add values of a & b
Step 5 − Store output of step 4 to sum
Step 6 − Print sum
Step 7 − STOP
4. Design an algorithm find the largest number among
three numbers
Input: A = 2, B = 8, C = 1
Output: Largest number = 8
Input: A = 231, B = 4751, C = 75821
Output: Largest number = 75821
Design an algorithm find the largest number among three
numbers
Step 1. Start
Step 2. Read the three numbers to be compared, as A, B and C.
Step 3. Check if A is greater than B.
3.1 If true, then check if A is greater than C.
3.1.1 If true, print 'A' as the greatest number.
3.1.2 If false, print 'C' as the greatest number.
3.2 If false, then check if B is greater than C.
3.1.1 If true, print 'B' as the greatest number.
3.1.2 If false, print 'C' as the greatest number.
Step 4. End
Characteristics of an Algorithm
4. Output - After executing all the steps of the algorithm at least one
output must be obtained. The WRITE statement is used to print
messages.
5. Effective - The operations to be performed in the algorithm can be
carried out manually in finite intervals of time.
Qualities of a good algorithm
1. It is simple to understand.
2. It is very easy to debug.
3. It is independent of programming language.
4. It can be easily coded into High Level Language.
REAL-TIME EXAMPLES
How it helps?
To understand the usage of algorithm, look over the following
conversation.
How it helps?
Lets discuss about the conversation, tom wants brush his teeth,
so he asks chitti to bring brush, what happens chitti returns
cleaning brush.
Why this was happened ?
Because the statement given by tom was not well defined and it
is ambiguous statement so chitti get confused and bring some
brush to Tom.
This is what happen if the user gives ambiguity statement to the
computer.
Therefore an algorithm should be simple and well defined.
How an algorithm should be?
It should be in simple English, what a programmer wants to say.
It has a start, a middle and an end.
Start
1. In the middle it should have set of tasks that computer wants to do
and it should be in simple English and clear.
2. To avoid ambiguous should give no for each step.
Stop
REAL TIME EXAMPLE FOR ALGORITHM (ATM Machine)
Algorithm
1. ATM reads your information on the
magnetic strip of your debit card.
2. Sends information to your bank’s
server.
3. Confirmation sent back to ATM.
4. After you key in your PIN the
information again sent to bank
server.
5. Confirmation sent back to ATM.
6. ATM dispense cash.
TO FIND A MINIMUM IN A LIST
Problem Description:
• Minimum in a list of elements can be achieved in different ways.
• One way is to sort the list of element in ascending order and get the
first element as minimum.
• Another method is to compare each element with other.
• Assume the first element as minimum element and start comparing
with the next element.
• If the next element is smaller than assume the second the minimum
and keep repeating the procedure till the last element.
ILLUSTRATIVE PROBLEMS
Find a Minimum in a List
Algorithm:
• Step 1: Start
• Step 2: Read n
• Step 3:Initialize i=0
• Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5
• Step4.1: Read a[i]
• 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 9
• 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:
• BEGIN READ n
• FOR i=0 to n, then
• READ a[i]
• 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
INSERTING A CARD IN A LIST OF
SORTED CARDS
Problem Statement:
• Imagine that you are playing a card game. You are holding the cards in
your hand, and these cards are sorted.
• You are given exactly one new card.
• You have to put it into the correct place so that the cards you are
holding are still sorted.
Insert a card in a list of sorted cards
Algorithm:
• Step 1: Start
• Step 2: Read n
• Step 3:Initialize i=0
• Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5
• Step4.1: Read a[i]
• Step 4.2: i=i+1 goto step 4
• Step 5: Read item
• 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
• READ n
• FOR i=0 to n, then
• READ a[i]
• INCREMENT i
• END FOR
• READ item
• 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]=item
• COMPUTE n=n+1
• FOR i=0 to n, then
• PRINT a[i]
• INCREMENT i
• END FOR
• END
Guessing an integer number in
a range.
PROBLEM STATEMENT:
• Imagine you are trying to guess a number in a range. The objective is
to randomly generate integer number from 0 to n. Then the player
has to guess the number. If the player guesses the correctly, output an
appropriate message.
ALGORITHM:
Step 1: Start
Step 2: Generate a random number and call it num.
Step 3: Enter the number to guess.
if (guess is equal to num)
Print “You guessed the correct number”
Otherwise
if(guess is less than num)
print “ Your guess is lower than the number. Guess again!”
otherwise
print “Your guess is higher than the number. Guess again!”
Step 4: End
PSEUDOCODE:
START
READ Guess_num,input_num
IF guess_num>input_num THEN
WRITE “Your guess is higher than the input_number!”
ELSE
IF guess_num<input_num THEN
WRITE “Your guess is lower than the input_number!”
ELSE
WRITE “You guessed the correct number!”
START
READ num
<num >num
If Guess ?
STOP
Explain with relevant diagrams and algorithm the Towers of Hanoi
problem.
Tower of Hanoi:
Problem:
The smaller one sits over the larger one .
The objective is to move the entire disk to some another tower without
violating the sequence of arrangement.
Rules to be followed:
1 Only one disk can be moved among the towers at any given time.
2. Only the “Top” disk can be removed.
3. No large disk can sit over a small disk.
ALGORITHM:
Step 1: Start
Step 2: Read n
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
Step4.2: T(n,Beg,End,Aux) T(1,Beg,Aux,End) , Move disk from
source to destination T(n-1,Aux,Beg,End)
Step 5: Stop
FLOWCHART:
PSEUDCODE:
BEGIN
READ n
CALCULATE move=pow(2,n)-1
FUNCTION T(n,Beg,Aux,End) Recursively until n=0
PROCEDURE
IF n=0 then,
No disk to move
ELSE
T(n,Beg,End,Aux)
T(1,Beg,Aux,End), move disk from source to destination
T(n-1,Aux,Beg,En )
END PROCEDURE
END