0% found this document useful (0 votes)
11 views60 pages

COMPUTATIONAL THINIKING LAB MANUAL Updated 2022-2023 Final Copy-Updated

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)
11 views60 pages

COMPUTATIONAL THINIKING LAB MANUAL Updated 2022-2023 Final Copy-Updated

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/ 60

FRESHMAN ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


2022 - 2023

10210CS302 COMPUTATION THINKING LABORATORY

LAB MANUAL

I YEAR/ SUMMER SEM (SoC)


Vel Tech Rangarajan Dr. Sagunthala R&D Institute of Science and Technology
(Deemed to be University Estd. u/s 3 of UGC Act, 1956)

Course Code : 10210CS302 Course Name: Computational Thinking Lab

LAB TASKS

TASK
NAME OF TASK CO
NO
1 Sum of N Natural Numbers/ Factorial Computation CO1

2 Sine Function Computation/ Show of Hands CO1

3 Maximum Number of A List/ Remove Duplicates From Ordered Array CO1

4 Partition Array/Algorithm to Find Kth smallest element in an Array CO1

5 Breakdown of drawing smiley face/ Science Project Task Breakdown CO2

6 Detection of Criminal Case/Involve Robot To Build A New Car CO2

7 DNA Sequencing/Web searches And Enron Legal Documents CO2

8 Linear Pattern Search/Sub Linear Pattern/Substring Match CO2

9 Setting Up Home Aquarium/Drawing A Dog Using Abstraction CO3

10 Left and Right Justification of Text CO3

11 Text Line Editing CO3

12 GCD of 2 integers/The Smallest Divisor of an Integer, Prime Factors Computation CO4

13 Budget Shopping/Character to Number Conversion CO4

14 The Merge Sort Algorithm Birthday Party CO4

15 Sorting by Exchange, Selection and Insertion CO4


TASK (1.A).SUM OF N NATURAL NUMBERS
AIM: To find the sum of “N” Natural numbers
ALGORITHM
Step 1 : Start
Step 2: Read the values of n.
Step 3: Initialize the sum to 0.
Step 4: Initialize i to 0.
Step 5: Compute sum=sum+i
Step 6: Compute i=i+1
Step 7: Check whether (i<=n)
If the condition is true goto step 5
Step 8: Print sum
Step 9: Stop.
FLOWCHART
ALGORITHM VERIFICATION

STEPS n sum=sum+i i=i+1 i<=n


2 4 Initially sum =0 Initially i=1 1<=4 Yes
3 1 2 Yes
4 3 3 Yes
5 6 4 Yes
6 10 5 No

Logic: The algorithm uses a simple loop to iterate from 0 to n, adding each natural number to the
sum variable. The loop continues until i exceeds n, at which point the final value of sum is printed.

Time Complexity:
The time complexity of this algorithm is O(n), because it needs to iterate n times to calculate the sum.

Space Complexity:
The space complexity of this algorithm is O(1), because it only uses a constant amount of space to
store the variables sum, i, and n.

RESULT:
Hence the algorithm and flowchart for sum of n natural numbers is verified and proved.
TASK (1.B)- FACTORIAL COMPUTATION
AIM: To compute factorial of a given number
ALGORITHM DEVELOPMENT:
n! = 1x2x3x…. (n-1) x n for n>=1
0! =1 1!= 1x1
2! =1x2
3! =1x2x3
4! =1x2x3x4
n! = n x (n-1)! For n>=1
ALGORITHM:
Step 1: Start
Step 2: Read the value of n
Step 3: Initialize the variable fact to 1
Step 4: Initialize the variable i to 1
Step 5: Compute fact = fact * i
Step 6: Increment i by 1
Step 7: Check whether i<=n
If the condition is true go to step 5
Step 8: Display p
Step 9: Stop
FLOWCHART:
Formula:
The formula for computing the factorial of n is:
n! = n × (n-1)!
This formula can be recursively applied to compute the factorial of any positive integer n.
Pseudocode:
function factorial(n) {
if (n <= 1) {
return 1;
} else {
return n * factorial(n-1);
}
}
Logic:
The algorithm uses a recursive approach to compute the factorial of n. The base cases are handled separately,
and the recursive case is used to compute the factorial of n-1 and multiply it by n.
Time Complexity: The time complexity of this algorithm is O(n), since it needs to recursively call itself n times
to compute the factorial.
Space Complexity: The space complexity of this algorithm is O(n), since it needs to store the recursive calls on
the call stack. However, if we use an iterative approach instead of recursion, the space complexity would be
O(1).

ALGORITHM VERIFICATION:
Steps n fact= fact * i i=i+1 i<=n
1 5 Initialize fact = 1 Initialize i=1 1<=5 yes
2 1 2 2<=5 yes
3 2 3 3<=5 yes
4 6 4 4<=5 yes
5 24 5 5<=5 yes
6 120 6 6<=5 no

RESULT: Thus the computation of factorial number is verified successfully.

TASK (2.a): SINE FUNCTION COMPUTATION


Sine series is given ;
sin(x) = (x/1!)-((x^3)/3!)+((x^5)/5!) -((x^7)/7!)+..........
AIM:
To find the sine series function computation
ALGORITHM:
Step-1: Start
Step-2: Read the value of x
Step-3: Initialize the tsin to x.
Step-4: Initialize i to 1
Step-5: Intialize term to x
Step-6: Initialize error to 0.0000
Step-7: increase i by 2
Step-8: compute term=-term*x*x/ i(i-1)
Step-9: compute tsin=tsin + term
Step-10: Check if term>error then goto step 8
Step-11: Print tsin
Step-12: stop.

Logic:
The algorithm uses a loop to iteratively compute the terms in the sine series, starting from x and
alternating between adding and subtracting terms. The loop continues until the absolute value of the
new term is less than or equal to the error threshold.
Time Complexity: The time complexity of this algorithm is O(n), where n is the number of terms in the
series that need to be computed.
Space Complexity: The space complexity of this algorithm is O(1), since it only uses a constant amount
of space to store the variables tsin, i, term, and error.
FLOW CHART

START

Read x

tsin = x

i=1

term=x

error = 0.0001

Term=term + I

Term= -1+x+x/i[i-1]

Tsin=tsin + term

term >
error

Display tsin
ALGORITHM VERIFICATION:
Sample input: x=0.5
STEPS x Tsin i Error t | t| error
2 0.5
3 0.5
4 1
5 0.00001
6 0.5
7 3
8 -0.02
9 0.48
10 No
7 5
8 0.00025
9 0.48025
10 No
7 7
8 0.000014
9 0.48024
10 Yes
11 0.48024
Sample Output: 0.48024

RESULT: Hence the algorithm and flowchart the sine series computation logic is analysed and studied.

TASK (2.B): SHOW OF HANDS (IITB)


AIM:
To do the activity of Show of Hands.
ALGORITHM:

Step 1: Start
Step 2: Read the value of N (number of persons)
Step 3: Compute nominee = random (1,n)
Step 4 : Initialize count to 0 and i to 1.
Step 5 : Check whether i not nominee if yes goto step 6 else goto step 9.
Step 6 : Read hands value.
Step 7 : Check whether hands=1 (raised) if yes goto step 8 else goto step 9.
Step 8 : Increment count by 1.
Step 9 : Check whether i=n, if yes goto step 10, else i=i+1 goto step 5.
Step 10 : Check whether count >n/2 if yes goto step 11, else step 12.
Step 11 : Display “argument wins” goto step 13
Step 12 : Display “argument loses”
Step 13 : Stop

FLOW CHART
ALGORITHM VERIFICATION:
STEPS n Nominee Count i hand i=nominee hands=1 i=n Count>n/2
2 5
3 2
4 0 1
5 No
6 1
7 Yes
8 1
9 2 No
5 Yes
9 3
5 No No
6 1
7 Yes
8 2
9 4
5 No No
6
7 Yes
8 3
9 5
10 Yes

Input = 5
Output: Argument wins

RESULT:
Hence the algorithm and flowchart for show of hands is verified.
TASK (3.A) - MAXIMUM NUMBER OF A LIST
AIM: To find the maximum number in a set of N numbers sorted as an array
ALGORITHM:
Step 1: Start
Step 2: Create an array with n element
Step 3: Initialize index[i] = 0
Step 4: Set maximum to a[0]
Step 5: Repeat step 6 by varying i from 1 to (n-1)
Step 6: Check if a[i]>Maximum, if yes set maximum = a[i]
Step 7: Display maximum
Step 8: Stop

ALGORITHM VERIFICATION:

Steps Array n i maximum A[i]>maximum


2 A[8,12,26,13,16] 5
3 0
4 8
5 1
6 12 Yes
5 2
6 26 yes
5 3
6 No
5 4
6 No
5 5
6 26
FLOWCHART:

Start

Input array a of n elements

Maximum a[0]

I1

A[i]>
max Maxa[1]

I=i+1

I<n-1

Display max

End

RESULT: Hence, maximum number in a list is verified and proved.


TASK (3.B): REMOVE DUPLICATES FROM ORDERED ARRAY
AIM: To find the algorithm for removing duplicates from ordered array
ALGORITHM:
Step 1: Start
Step 2: Create an array of n elements a(0…….n-1)
Step 3: Intitialize the index i to 1
Step 4: Repeat step 5 as long as a(i-1)=a(i) and i<n-1
Step 5: Increment i by 1
Step 6: Set j=j-1
Step 7: Repeat step 8 to 10 until i<n-1
Step 8: Increment i by 1
Step 9: Check if a(i-1)=a(i) if yes compute j=j+1, a(j)=a(i)
Step 10: Display j+1
Step 11: Stop
ALGORITHM VERIFICATION:

Steps array N i j A[i-1]!a[i] I<n-1 1&2


2 A[2 3 5 5 5 7 7
7]
3 1
4 A[0]!a[1] 1<6 True
5 2
4 A[1]!a[2] 2<6 True
5 3
4 A[2]!a[3] 2<6 false
6 2
7 3<6
8 4
9 A[3]!a[4]
7 4<6
8 5
9 3 A[4]!a[5] 5<6
7 6
8 A[5]!a[6] 6<6
10 4
FLOWCHART

Start

Read an array a(0……..n-1)

I1

A[i-1]!a[i]
and i<n-1

I=i+1
11

Set j=j-1

I<n-1

I=i+1 Display j+1

Stop
i-1!a[i]

J=j+1

A[j]=a[i]

RESULT: Thus the duplicates from ordered array is verified and proved
TASK (4.A): PARTITION ARRAY
AIM:
Given a randomly ordered array of n elements, partition the elements into two subset such that elements
<=x are in one subset and elements > x are in the other subset.
j
Variables:
1. i - to traverse from left to right (current upper boundary)
2. j – to traverse from right to left (current lower boundary)
3. x- as given in question. (INPUT)
4. t- temporary variable for exchange
5. Array ‘a’ to store given values - input
6. n- Number of elements in the given list. - input
7. p- partitioning point in the list (output)

PROGRAM LOGIC:
Step 1: Establish an array a of [0 to n-1] and read the partitioning value ‘x’.
Step 2: Move partitions to each other until a wrongly placed pair of elements are uncounted.
Step 3: While the two partitions have not met or crossed over do the following.
a) Exchange the wrongly partitioned pair and extend the partition inwards by one element.
b) Extend the left partition while the element is less than or equal to x(<=x).
c) Extend the right partition while the element are greater than x.
Step 4: Write the partitioning index p and the partitioned array.
ALGORITHM:
Step 1: Start
Step 2: Create an array a of n elements (a[0….n-1])
Step 3: Read the partitioning value i.e. x
Step 4: Initialise i to 0, j to n-1
Step 5: Repeat Step 6 as long as a[i]<=x and i<j
Step 6: Increment I by 1
Step 7: Repeat Step 8 as long as a[j]>x and i<j
Step 8: Decrement j by 1.
Step 9: Repeat Steps(10 to 18) as long as i<j
Step 10: Assign a[i] to t
Step 11: Assign a[j] to a[i]
Step 12: Assign t to a[j]
Step 13: Increment I by 1
Step 14: Decrement j by 1
Step 15: Repeat step 16 as long as a[i]<=x
Step 16: Increment i by 1
Step 17: Repeat step 18 as long as a[j]>x
Step 18: Decrement j by 1
Step 19: Assign p=j, Display the array
Step 20: Stop

FLOWCHART:

RESULT:
Thus the given array is partitioned such that all elements in left sub-list is lesser than the given x value and
the all elements in the right –list is greater than x.
TASK (4.B): FINDING THE KTH SMALLEST ELEMENT IN ARRAY

VARIABLES:
1. For lower limit-l
2. For upper limit-u
3. i – to traverse the array from left to right
4. j – to traverse the array from right to left
5. t-temporary variable for swapping
6. An array say ‘a’ where given list is stored
7. x – the array element present at kth position. i.e. x=a[k]

LOGIC:
1. Let l=1, u=n
2. Repeat while l<u
2.1 i=l, j=u
2.2 x=a[k]
2.3 Repeat while i<=k and j>=k
2.3.1 Repeat while a[i]< x
a) i=i+1
2.3.2 Repeat while x<a[j]
a) j=j-1
2.3.3 t=a[i]
2.3.4 a[i]=a[j]
2.3.5 a[j]=t
2.3.6 i=i+1
2.3.7 j=j-1
2.4 if j<k then l=i
2.5 if i>k then u=j
3. Print the element in the position k.

STOP
NOTE: At the end of the algorithm, the kth smallest element will be placed at the position k.
ALGORITHM:
Step 1: Start
Step 2: Read the value of k and array a[1…n]
Step 3: Initialise l to u and u to n
Step 4: Repeat steps (5 to 17) as long as (l<u)
Step 5: Initialise i to l, j to u and x to a[k]
Step 6: Repeat steps (7 to 15) as long as (i<k) and (j>=k)
Step 7: Repeat step 8 as long as (a[i] <x)
Step 8: Increment i by 1
Step 9: Repeat step 10 as long as (a[j]>x)
Step 10: Decrement j by 1
Step 11: Assign a[i] to t.
Step 12: Assign a[j] to a[i]
Step 13: Assign t to a[j]
Step 14: Increment i by 1
Step 15: Decrement j by 1
Step 16: Check if j<k , if yes, set l to i
Step 17: Check i>k, if yes, set u to j
Step 18: Display a[k]
Step 19: Stop
FLOWCHART:

RESULT: Thus the kth smallest element is found from the given list.
LAB TASK (5.A): SMILEY FACE DECOMPOSITION
AIM : To develop an algorithm to draw a smiley face using the process of decomposition.
ALGORITHM 1:
START
Step 1 🡪 Draw a circle
Step 2 🡪 Draw a curved line for mouth line
Step 3 🡪 Draw two dots for eyes
Step 4 🡪 Color the smiley face
STOP

ILLUSTRATION:
But this doesn’t tell us where the curved line and the dots should go in relation to the
circle, or each other.

ALGORITHM 2:
An algorithm that draws such an image might therefore look like this:

START
1. Draw circle with radius 30 at position 50,50 with line thickness 2.
2. Draw circle with radius 6 at position 40,40 with line thickness 1.
3. Draw circle with radius 3 at position 40,40 filled black.
4. Draw circle with radius 6 at position 60,40 with line thickness 1.
5. Draw circle with radius 3 at position 60,40 filled black.
6. Draw red line from position 30,70 to position 70,70 with line thickness 1.
STOP
FLOWCHART:

RESULT : Thus the algorithm and flowchart for drawing smiley face is successfully done using the
concept of decomposition.
TASK (5.b): SCIENCE PROJECT TASK DECOMPOSITION
AIM: To develop an algorithm for science project task using the process of decomposition.

Fig 1: Science project decomposed to sub tasks

Fig 2: Sub Task-Write dissertation is further decomposed.


Algorithm for sub task 3.Write dissertation
3.1.1 write title page;
3.1.2 write copyright section;
3.1.3 write abstract;
3.1.4 write contents section;
3.1.5 write list of figures section
ALGORITHM:
START
1. Perform background research
1.1 Read related journals, magazines and books
1.2 Visit places related to research
1.3 Collect contents and data
1.4 Register the observations of the study

2. Perform related Experimentation


2.1 Document all experiments
2.2 Tabulate and store all necessary data of experimentation
2.3 Register all successful and failure experiments
3. Write dissertation
3.1.1. write title page;
3.1.2 write copyright section;
3.1.3 write abstract;
3.1.4 write contents section;
3.1.5 write list of figures section
3.1.6 write results
3.1.7 write conclusion
3.1.8 write references
4. Submit dissertation
4.1 Get approval of the dissertation
4.2 Present the project work to the outside world.
STOP

RESULT:
Thus the above algorithm and flowchart generated and successfully verified.
TASK (6.A):DETECTION OF CRIMINAL CASE
AIM
To save the crime with decomposition
ALGORITHM
1) Identification of crime
1.1) Diamond Stolen
1.1.1) Cost
1.1.2) Place stolen
1.1.3) cont
1.2) Location
1.2.1) Street Name
1.2.2) Area
1.2.3) Land Mark
1.3)Time
1.3.1)Date
1.3.2)FN/AN
2)Investigate the witness
2.1)Lady Gather
2.1.1)Visual Identification
2.1.2)Suspect information
3)Collect all the evidences
3.1)Foot print
3.1.1)Size
3.1.2)Type
3.1.3)Suspect Height
3.2)CCTV Footage
3.2.1)Track video
3.2.2)Tool used for breaking glass
3.2.3)Suspect physique
3.2.4)Verification information
3.3)Finger Print
3.3.1)Send to forensic department
3.4)Broken Entry Glass
3.4.1)Tools
3.4.2)Broken outside/inside
3.5)Guess
3.5.1)Left Jewellery
4)Identification of suspect
4.1)Similarity in finger
4.1.1)Forensic lab
4.2)Face recognition
4.2.1)Software
4.3)Compare with crime
4.3.1)Suspected list
4.4)Display the suspect
4.4.1)Advertise
FLOWCHART

CRIME
DETECTION

Identification Investigation Collect the Identification


of crimes of witness evidences of suspect

Diamon locatio
d stones n

RESULT:Hence the algorithm for detection of criminal case is verified


TASK (6.b):INVOLVE ROBOT TO BUILD A NEW CAR
AIM:To instruct a robot with step by step procedure to build a new car
ALGORITHM:
1)Building the chasis
1.1)Gather materials
1.2)Attach the wheels and motors to the chasis
1.3)Glue on the battery holder
1.4)Attach the motor driver circuit
1.5)Position the arduino
2)writing the car
2.1)Cut four lenghts of wire
2.2)Solder two wires onto one motor
2.3)solder the other ends onto one motor
2.4)Repeat for the other amount
2.5)connect the battery holder
2.6)connect the motor driver circuit
3)setting up the RX circuit control
3.1)understand the process
3.2)cut four wires of equal lengths
3.3)solder the wires
3.4)connect the wires to the arduino
3.5)check your wiring
4)Programming the car
4.1)connect arduino to a computer
4.2)upload the programme code for automation
4.3)start the car
4.4)play around with the code
NEW CAR

Building the Wiring the car Setting up RX


chasis Organizing the car
control circuit

Conne Und
Attac Solder the
ct erst Connect Chec
Gath Attach Glu h wires
Positi motor and wiring to k
er the e motor
on driver the the wirin
mate wheel on driver
the circuit pro aurdino g
rials s and the circuit
aurdi cess
motor hea r
no
to the der
chasis

Cut Sold Sta Play


the Solder Re
er rt
wire the Con aro
the pe Connect Codin the
wire nec und
ends at aurdino to g for car
on t wit
to for the aurdi
motor the h
the ot componen no
batt t cod
mot her
ery e
or mo
drive tor
r

RESULT:Hence the algorithm for involve robot to build a new car is verified
TASK (7.A):DNA SEQUENCING
AIM:To find the DNA Sequence
ALGORITHM:
1)Start
2)Read the sequence in a DNA database
3)Generate a pattern for any sequence that begins with AC and is followed by one or more T
characters and finally by ac
4)Let i+ be (ACT+C)
5)Search the pattern in the given DNA sequence. If the pattern matches with DNA sequence, print
the sequence
6)stop
INPUT
Enron e-mail Database segment
Subject:enron-Living for country
PRIVILEGED AND CONFIDENTAL

OUTPUT
All caps word pattern(Incorrect)
(A-Z)+
All caps word pattern
v(A-Z)+\w
Revised pattern
Sequence of at least 3 all caps words
(\w[A-Z]+\w){3,}

Final output
PRIVILEGED AND CONFIDENTAL
\w[A-Z]+. \w[A-Z]+ \w[A-Z]+\w
FLOW CHART :

START

Read the sequence in DNA


Database

Generally pattern sequence ACT+C

NO YES
Pattern
matchin
g with
input

Print the
sequence
Not Matching

STOP

RESULT:Hence the algorithm and flowchart for DNA Sequencing is verified


TASK (7.B): WEB SEARCHES AND ENRON LEGAL DOCUMENTS

AIM: To use patterns for miring textual database for useful legal information
ALGORITHM:
STEP 1: Start
STEP 2: read enron e-mail database segment
STEP 3: Generate a pattern that match only the three words PRIVILEGE and CONFIDENTIAL
STEP 4: Repeat step 3 by updating the pattern till decided results are found
STEP 5: Display the output by matching the pattern with e-mail segment
STEP 6: Stop
LOGIC:
INPUT: Enron e-mail database segment
Subject: enron-livingston country
PRIVILEGED AND CONFIDENTIAL
OUTPUT:

ALL CAPS WORD PATTERN(IN CORRECT)

(A-Z)+

ALL CAPS WORD PATTERN

V>(A-Z)+\W{3}

REVISED PATTERN

SEQUENCE OF ATLEST 3 ALL CAPS

\W(A-Z)+\W){3,}

FINAL OUTPUT:
PRIVILIGED - AND - CONFENDENTIAL

\W[A-Z]+ \W[A-Z]+\W [A-Z]+ \W

RESULT:Hence the algorithm for web seearches is verified


TASK(8.A): LINEAR PATTERN SEARCH
AIM: To do the activity of linear pattern search.
ALGORITHM: PARTIAL TABLE (pat[1..m],m)
STEP1: Start
STEP2: Read a pattern array of m elements pat[1..m]
STEP3: Initialize i to 2 , j to 1.
STEP4: Initialize recover [1] to 0.
STEP5: Repeat step-6 if i<=m else goto step 13
STEP6: check if pat[i]=pat[j], if yes goto step 7 else go to step 10
STEP7: set recover [i]=j
STEP8: increment i by 1
STEP9: increment j by 1 go to step 5
STEP10: set recover [i]=0
STEP11: set j=1
STEP12: increment i by 1 go to step 5
STEP13: display recover [1..m]
STEP14: stop
LOGIC FOR PATTERN SEARCHING ALGORITHM
Input : pattern pat [1..m], m  (pattern length)
Text: txt [1..n] ,n(text length)
PROCESS :
🡺 Use an index variable i and j to point to the element of the text and pattern respectively
🡺 Initialize i=1 ,j,j=1
🡺 i refers to the text position and j prefers to pattern position
🡺 Use a variable called nmatch to count the number of times pattern occurring in the text, n match=0
🡺 Call partial match(pat,m) and get recover [1..m] array
🡺 Repeat below steps as long as i <=m
If (pat[j]=text[i])
Increment j by 1
Increment i by 1
If(j>m)
Increment n match by 1
j=recovery [j-1]+1
If j=1
Increment i by 1
Else
j=recovery[j-1]+1
If j=1
Increment j by 1
OUTPUT: nmatch

ALGORITHM : PATTERN SEARCH (txt[1..n],pat[1…m],m,n,recover[1..m])


Step1:start the program
Step2:Read the text array txt[1..n] and pattern array pat[1..m]
Step3:Initialize i to 1 ,j to 1,nmatch=0
Step4:if i>n go to step 14
Step5:check if txt [i]=pat[j],if yes got to step 6 else go to step 12
Step6:Increment i by 1
Step7:Incrment j by 1
Step8:check if j>m if yes go to step 9 else go to step 4
Step9:Increment nmacth by 1
Step10:set j=recovery[j-1]+1
Step11:If j=1 Increment i by 1 go to step 4
Step12:Set j=recovery [j-1]+1
Step13:If j=1 increment I by 1 go to step 14
Step14:Display nmatch
Step15:Stop

RESULT: Hence the algorithm for linear pattern search is verified


TASK (8.B): SUB LINEAR PATTERN SEARCH/SUBSTRING MATCH
AIM: To do the activity of a sublinear pattern/ substring match
ALGORITHM:
Step 1 : Start
Step 2 :Read a text array txt[1..n] and pattern array [1..m]
Step 3 :Initialize i to m, nmatch=0
Step 4 :If i>n goto step 14
Step 5 :Set nxt = asc (txt[i])
Step 6 :if skip[nxt]> 0 then set i=i+skip[nxt], goto step-4
Step 7 :Set j=i-1, k=m-1, match = true
Step 8 :Repeat step-9 if k>0 and match=true.
Step 9 :if (text[j] = word[k]) goto step-10 else goto step-11
Step 10:Decrement j by 1, decrement k by 1
Step 11:Set match = false
Step 12:if match=true increment nmatch by 1
Step 13:Compute i = i – skip[nxt] goto step-4
Step 14:Display nmatch
Step 15:stop
LOGIC:
Input : Text text[1..n], pattern word[1..m]
Skiptable[0..12]
PROCESS:
🡺 Use a index variable i to point the elements as the text. Initialize i = m (word length)
🡺 S is the index for matching backwards in the word
🡺 Set match to 0
🡺 nxt holds the ascii value of charcter.
Repeat below steps as long as i<=n.
nxt = asc(text[i])
if skip[nxt]>0
i = i + skip[nxt]
else
j=j-1
k=m-1
match= true
while (k>0 and match = true)
if (text[j] = word[k])
decrement j by 1
decrement k by 1
else
j=j-1
k=m-1
match=true
while(k>0and match=true)
if(text[j]=word[k])
decrement j by 1
decrement k by 1
else
match=false
if match =true nmatch=nmatch+1
i=i-skip[nxt]
out put :-n match

RESULT: Hence, the algorithm for sublinear pattern/substring match is required


TASK (9.A): SETTING UP A HOME AQUARIUM

AIM :
To do the activity of Setting up a home aquarium using algorithm thinking
Algorithm:
1. Remove tank / aquarium from the box
2. Put your tank / aquarium in position
3. Rinse all gravel and ornaments
4. Fill your tank with water from a clean bucket.
5. Turn on filters
6. Repeat step 7 -11 every two weeks to cycle the tank.
7. Add fish food
8. Add water condition
9. Take a sample of water and test. If it is toxic remove the water. Go to step3
10. Add small number of fishes
11. Feed the fish 2 time
12. Stop

Flowchart

RESULT
Thus, the algorithm for setting up a home aquarium solved by using algorithmic thinking is verified.
TASK (9.B)- DRAWING A DOG - ABSTRACTION
AIM: To do the activity of drawing a dog using Abstraction
LOGIC:

ALGORITHM:
1. Draw two circles with dot inside it

2. Draw a small curved line from eyes ending with small circle for its nose

3. Draw a small curved line starts from eyes and end towards its eyes for getting its face

Draw a small down circle starts from nose and end towards its face for getting mouth

4. Draw a small U shaped curve starting below its mouth and ending in the same for getting
its tongue

5. Draw a two small doubled lines above the eyes for getting its eyebrow

6. Draw a curved U shaped line starting from its right eyebrow and ending in right eyebrow

7. Draw its forearm leg by double solid lines staring from its bottom face

8. Draw its another forearm leg , just draw a small straight line attached to it.

9. Draw it bottom leg by making a three shaped curve line, just behind the fore arm leg

10. Draw its tail by sharp edged double line, and join the line with its ears for getting its body
11. Draw a U shaped curve starting from its upper right eye brow and ending it in the same
for getting its right ear

12. Darken the nose circle by black color for distinguishing its nose
13. Draw a small double dashed line just above the tail

14. Draw a water droplet just below its tongue in order to get a complete dog picture

RESULT:
Thus the algorithm for drawing a dog completed using abstraction is verified.
TASK 10: LEFT AND RIGHT JUSTIFICATION OF TEXT

AIM:
To do the activity of left and right justification of text
LOGIC:
->Create an array whose length is equal to the no of space in the given text.
-> Let n space refers to the number of spaces in the given text.
-> Let old text length be the length of the given text.
-> Let new text length be the length of the justified text.
->Find extra space=new text length-old text length.
We use a variable delta=round (nspace/extraspace)
If delta=0, then delta=1
->Compute the next position to be added using
Next=round(delta+1)/2
->Check extraspace>nspace
If extraspace>nspace then
Space block= extra space/ nspace
->Repeat the process while next≤nspace and extra space >0
t[next]=t[next]+space block
next=next+delta
extra space= extra space-space block

ALGORITHM :
Step1: Start
Step2: Input the line to be justified
Step3: Find old Length =length of line
Step4: Input the new length of justification
Step5: compute extra space = new text length - old text length
Step6: if the old text length >new text length then display justification can’t be done go to
else proceed
Step7: Remove all the leading and trailing spaces in the line
Step8: Find the no of Spaces in the lines let it be n spaces
Step9: Create an array +[1 to n space ] and initialize it ‘s value as 1
Step10: Repeat the step (11, 12, 13) if extra spaces >0 and n space >0 else go to step-18
Step11: Compute delta= round (n Space/extra space)
If delta=0 set delta=1
Step12: Compute next=round (delta+1)/2
Step13: If the extra space > n space then compute space block=extra space/n space else space block1
Step 14: Repeat the space (15, 16,17) is next < n space and extra space >0
Step15: compute +[ next] =+ [next] +space block
Step16: compute next= next+ delta
Step17: compute extra space = extra space- space block
Step18: For the character in the line do the following if the character is a space the consult the array
and add the space indicated in the position else copy the character in the output array
Step19: print the justified step
Step20: Stop

Input :
She-sells-sea-shells-in-the-sea-shine
Extra space= new length-old length
47-37=10
N space=7

2 3 2 3 2 3 2
1 2 3 4 5 6 7

Output : sells- sea-shells-----in----the----sea---shine

RESULT: Hence, the Algorithm for left and right justification of text is verified

TASK 11: TEXT LINE EDITING


AIM :- To design an algorithm that will search a line as text for a particular pattern, if pattern is found it is
replaced by another given pattern
LOGIC :-
Input : Text line, search pattern and replacement pattern and their
Corresponding lengths.
Original Text : The two wrongs in the line are wrong.
Old pattern : Wrong
New Pattern : Right
Edited Text : The two rights in the line are right.
Process :-
The two wrongs in the line are wrong
↓ |||||
Wrong w r o n g
While
i <= text length – pattern length + 1
Pattern [ j ] = text [ i + j – i ]
New pattern :- right .
Input :- Edited text :- The two rights in the line are right.
ALGORITHM:-
Step-1: start
Step-2: Read the text-line, search pattern and replacement pattern and their corresponding lengths
Step-3: set the initial value for the position in the old text, new text and search pattern let it be I, j & k
Step-4: Repeat step -5 until
I<=text length-pattern length + 1
Step-5: Check whether pattern [j]=text [I+ j-k]
If no copy text[i+j-k]
Increment by 1
Set j to 1
If yes increment j by 1, go to step-6
Step-6: Check whether all the characters in j is completely matches with the characters in text-line, if yes
replace search pattern with new pattern in output
Step-7: Copy the leftover character in original text in to the output
Step-8: Display “Edited text”
Step-9: Stop
FLOW CHART:

RESULT : Hence, the algorithm and flowchart for text-line editing is verified
TASK (12.A) : GCD OF TWO INTEGERS

AIM: To find the greatest common divisor (GCD) of two integers.


ALGORITHM:
1. start
2. Read value of n, m.
3. Compute r=n%m
4. Set n=m
5. Set m=r
6. Check if r==0 if yes print n as GCD
7. Else goto step 3
8. stop
FLOWCHART

TRACING TABLE:
Steps M n r m%n=0

2 15 5

3 No

4 1

5 2

3 Yes

SAMPLE INPUT:
Enter m, n value
15 2
GCD= 1
RESULT: Thus the GCD of two numbers is found out by using the algorithm and the flowchart is drawn.

TASK (12.B) .THE SMALLEST DIVISOR OF AN INTEGER


AIM:
To find the smallest divisor as an integer
LOGIC:
Input: n
Process: if n%2=0
Print sdivisor=2
Else
r=trunc(sqrt(n))
d=3
while (n mod d <>0) and (d<r) do d:=d+2;
if n%d=0
assign sdivisor = d
print sdivisor and stop
else
print sdivisor=1
Output: print sdivisor

ALGORITHM :
Step 1: Start
Step 2: Read the value of n
Step3: Check whether n%2=0 print sdivisor=2
Step4: initialize d to 3
Step5: check whether (n mod d <>0) and (d<r) do d:=d+2;
Step6: check if n%d=0
Step 7: if yes assign sdivisor = d and print sdivisor
else
print sdivisor=1
Step8: Stop
FLOW CHART:
ALGORITHM DEVELOPMENT

steps n n%2=0 d n%d=0


2 15
3 NO
4 3
5 Yes

Input =15
Output =3

RESULT: Hence, the algorithm and flowchart for smallest divisor of an integer is verified

TASK (12.C).PRIME FACTORS COMPUTATION

AIM: To find the prime factor computation


Logic:
Input: Read N
Process and output: As long as n is divisible by 2, print “2” and divide n/2.
After above step N must be add row start checking for the values i=3, 2, n/2 as long
as i
divides N print i and divide N/i.
Increment i by 2.
->If N is a prime number and greater than 2 print N

ALGORITHM

Step 1: Start
Step 2: Read the value of n
Step 3: Repeat the step 4 as long as n%2=0
Step 4: Print ‘2’, compute n=n/2
Step 5: Repeat step 6 by initializing i to 3 upto n/2
Step 6: Check if n%i=0, if yes print ‘i’, compute n=n/i
Step 7: Repeat step 6 as long as n%i=0
Step 8: Increment i by 2
Step 9: Check n>2, if yes print n
Step 10: Stop

FLOWCHART

ALGORITHM VERIFICATION

steps N n%2=0 Print “2” N=n/2 i n%i=0 Print i n=n/i

2 36
3 YES
4 2 18
3 YES
4 2 9
3 NO
5 3
6 YES
7 3 3
6 YES
7 3 1
6 NO
8 5
NO

Input = 36

Output =2,2,3,3

RESULT: Hence, the algorithm and flowchart for prime factors computation is verified
TASK (13.A):BUDGET SHOPPING

AIM : To do the activity of budget shopping.


LOGIC :
Input : total = 0
Process : Read budget / item code and quantity
Read price = quantity * unit price
total = total + price
check for more purchase ,if yes Repeat.
While total > budget
Enter item code to remove
price= quantity*unit price
total=total-price
Output: Display “Shopping Over”

ALGORITHM
Step 1 : Start
Step 2 : Set total to zero.
Step 3 : Display list of item code and unit price
Step 4 : Read Budget
Step 5 : Read item code and quantity
Step 6 : Compute price 🡪 quantity*unit price
Step 7 : Compute total 🡪 total + price
Step 8 : Check whether do you want purchase any more ,else go to step – 5
Step 9 : Check whether total > budget ,else go to step – 13
Step 10 : Read enter and quantity to remove
Step 11 : Compute price quantity to unit price
Step 12 : compute total 🡪 total price
Step 13 : Display “Shopping Cover”
Step 14 : Stop
FLOWCHART
START

Total = 0

Display List of item code and unit price

Read Budget

Price = quantity * unit price

Total = total + price

Do you
want to
purchase
anything

YES Total > NO


Budget

Enter item code to remove Display “Shopping Over”

Total = Total -
Price
STOP

RESULT : Hence, the algorithm and flowchart for budget shopping is verified.
TASK (13.B) - CHARACTER TO NUMBER CONVERSION

AIM:
To do the activity of character to number conversion
LOGIC:
Input: Read value of string
Process:
Initialize decimal to 0
i from 1 to n
if yes
read character(c )
get ASCII value of c
digit=ascii -48
decimal=decimal *10+digit
if no
output: display decimal
ALGORITHM:
1. Start
2. Read the value of string [character number]
3. Compute length of string, let it be n, initialize dec to 0
4. Repeat step [5 -8] by varying I from 1 to n.
5. Read character (c)
6. compute digit= ascii-48
7. compute dec= dec*10+digit
8. display dec
9. stop

TRACING TABLE
character (c
Steps String n i ) asc digit dec i<n
2 2194
3 4
4 1
5 2
6 50
7 2
8 2
4 yes
5 2
6 1
7 49
8 1
4 21
5 yes
6 3
7 9
8 57
4 9
5 219
6 yes
7 4
8 4
4 52
5 4
6 2194
7 no
8 5
4 2194

RESULT: Thus, the algorithm and flowchart for character to number conversion is verified.
TASK 14. THE MERGE SORT ALGORITHM BIRTHDAY PARTY
AIM :
To arrange and order the party guests by comparing their ages using Merge Sort
Algorithm.
ALGORITHM:
Step 1 : Find the middle index of the array,
Middle = 1 + (last - first)/2
Step 2. : Divide the array from the middle.
Step 3 : Repeat the steps 1 and 2 in first half of the array, until the atomic units of the array is
reached and further division is not possible.
Step 4 : Repeat the steps 1 and 2 in Second half of the array, until the atomic units of the
array is reached and further division is not possible.
Step 5 : After dividing the group of guests into individual persons, start merging the guests
based on comparison.
LOGIC :
64 25 12 22 11 36

[ 64, 25, 12 ] [ 22,11,36 ]

[ 64, 25 ], 12 [ 22,11 ] 36

[25,64] 12 [ 11,22 ] 36

[ 12, 25, 64 ] [11, 22, 36]

[ 11, 12, 22, 25, 36,64 ]

RESULT : Hence, the algorithm for merge sort - birthday party is verified
TASK 15 :SORTING BY EXCHANGE,SELECTION& INSERTION

TASK 15.A. EXCHANGE SORT

AIM
Apply Exchange Sort (Exchange Bubble Sort) algorithm to sort the given number of
elements in an array.

THE EXCHANGE SORT ALGORITHM:


1. Compare the first pair of numbers (positions 0 and 1) and reverse them if they
are not in the correct order.

2. Repeat for the next pair (positions 1 and 2).

3. Continue the process until all pairs have been checked.


4. Repeat the steps(1,2,3) for positions 0 through “n – 1” until no pairs remain to be
checked.
5. Display the sorted list.

FLOW CHART
RESULT: Thus, the Exchange sort has been successfully completed and verified.

TASK 15.B. SELECTION SORT

AIM
Apply selection sort algorithm to sort the given array of numbers.

ALGORITHM
1. Get a list of unsorted numbers
2. Set a marker for the unsorted section at the front of the list
3. Repeat steps 4 - 6 until one number remains in the unsorted section
4. Compare all unsorted numbers in order to select the smallest one
5. Swap this number with the first number in the unsorted section
6. Advance the marker to the right one position
7. Stop

FLOWCHART
RESULT
Thus, the selection sort algorithm has been successfully completed and verified.

TASK 15.C. INSERTION SORT


AIM
Apply insertion sort algorithm to sort the given array of numbers.

ALGORITHM
Step 1 − If it is the first element, it is already sorted return 1.
Step 2 − Pick next element.
Step 3 − Compare with all elements in the sorted sub-list.
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be
sorted.
Step 5 − Insert the value.
Step 6 − Repeat until list is sorted.

FLOW CHART
RESULT: Thus, the Insertion sort algorithm has been successfully completed and verified.

VIVA QUESTIONS:
1. What is computational thinking?
2. Define logic.
3. Mention the types of logic.
4. List out the four pillars of computational thinking.
5. What is decomposition?
6. Give some examples for decomposition.
7. Define abstraction.
8. Give examples for pattern.
9. What is algorithm?
10. Define Linear pattern search ?
11. What is meant by algorithm ?
12. What is text editing ?
13. Define sub Linear pattern search
14. Write the sequence of searching DNA Database?
15. Define Linear search ?
16. Define Binary search ?
17. What is the definition of problem ?
18. Write the way of programming the car ?
19. Tell the procedure of detection of criminal case?\
20. Define sorting?
21. Can you explain how Insertion sort works?
22. Can you explain how Selection sort works?
23. Can you explain how Exchange sort works?
24. What is the worst case complexity for insertion sort?
25. What is difference between sorting and searching?
26. How is merge sort different from insertion sort?
27. Can you explain how Merge sort works?
28. How would you improve the performance of merge
sort?
29. What is the worst case complexity for Merge sort?
30. What are some ways to optimize merge sort?

OPEN ENDED PROGRAM


1. Create a use case diagram for handling books in a library. Be certain to capture the work of
patrons and librarian
2. Create a use case diagram for Checkout system in a grocery store with two actors—the
customer and the checkout clerk.
3. Assuming the pattern below continues, how many squares will be in Figure 5?

4. What number fits into the question mark?


5. Which shape is the odd one out?

You might also like