SlideShare a Scribd company logo
Data Structures and Algorithms
Exercise 1


                Write an algorithm to check whether a number is a prime
                number or not.




     Ver. 1.0                                                       Session 2
Data Structures and Algorithms
Exercise 1: Solution


                1.   Accept a number from the user. Name it as num.
                2.   Declare an integer variable i.
                3.   Assign a value 2 to i.
                4.   Repeat until i > num/2:
                     a. If num % i = 0:
                          i. Display “The number is not prime”
                          ii. Exit
                     b. i = i + 1
                5. Display “The number is prime”.




     Ver. 1.0                                                         Session 2
Data Structures and Algorithms
Exercise 2


                Write an algorithm to generate the first 10 prime numbers.




     Ver. 1.0                                                        Session 2
Data Structures and Algorithms
Exercise 2: Solution


                1. Declare an integer variable, count, and assign the value 0 to
                       it.
                2. Declare an integer variable, num, and assign the value 2 to
                   it.
                3. Repeat until count becomes equal to 10:
                    a. If num is a prime number:
                        i. Display num
                        ii. Set count = count + 1
                    b. Set num = num +1




     Ver. 1.0                                                            Session 2
Data Structures and Algorithms
Exercise 3


                Write an algorithm to accept a number between 1 and 9 and
                display a pattern. For example, if the number entered is 5,
                the following pattern should be displayed:

                1
                2    1
                3    2   1
                4    3   2   1
                5    4   3   2    1




     Ver. 1.0                                                       Session 2
Data Structures and Algorithms
Exercise 3: Solution


                •   Accept a number in the range of 1 - 9 from the user. Store it
                    in a variable, num.
                •   Declare an integer variable, i.
                •   Set i = 1.
                •   Repeat until i becomes greater than num: // To display
                                                                      // num
                    rows
                     a.   Declare an integer variable j
                     b.   Set j = i
                     c.   Display j and insert a space
                     d.   Set j = j – 1
                     e.   If j > 0 go to step c
                     f.   Insert a line break
                     g.   Set i = i + 1

     Ver. 1.0                                                              Session 2
Data Structures and Algorithms
Exercise 4


                Write an algorithm to accept a number between 1 and 9 and
                display a pyramid. For example, if the number entered is 5,
                the following pyramid will be displayed:

                                     1
                                 1   2    1
                             1   2   3    2   1
                         1   2   3   4    3   2   1
                    1    2   3   4   5    4   3   2    1




     Ver. 1.0                                                       Session 2
Data Structures and Algorithms
Exercise 4: Solution


                1. Accept a number from the user in the range of 1-9. Store it
                   in a variable, n.
                2. Set i = 1.
                3. Repeat until i becomes greater than n:
                    •   Set j = 0
                    •   Insert a space
                    •   Set j = j + 1
                    •   If j < n – i, go to step b  // To display n – i spaces
                    •   Set j = 1
                    •   Display the value of j and insert a space
                    •   Set j = j + 1
                    •   If j < i, then go to step f // To display numbers from 1 to i
                    •   Set j = i – 1


     Ver. 1.0                                                                 Session 2
Data Structures and Algorithms
Exercise 4: Solution (Contd.)


                •   Display the value of j and insert a space
                •   Set j = j – 1
                •   If j > 0, go to step j // To display numbers from i – 1 down
                                           // to 1
                •   Give a line break
                •   Set i = i + 1




     Ver. 1.0                                                              Session 2
Data Structures and Algorithms
Exercise 5


                Write an algorithm to accept two strings and check whether
                the second string exists within the first string. For example,
                if the first string is “concatenation” and the second string is
                “cat”, the algorithm should display “Substring found at
                position 4 in the string”. However, if the first string is
                “concatenation” and the second string is “tent”, the algorithm
                should display “Substring not found in the string”.




     Ver. 1.0                                                           Session 2
Data Structures and Algorithms
Exercise 5: Solution


                1. Accept a string value from the user. Store it in a variable, str.
                2. Accept the substring to be searched in str. Store it in a
                   variable, substr.
                3. Store the length of str in an integer variable, len1.
                4. Store the length of substr in an integer variable, len2.
                5. Set i = 0.
                6. Repeat until i becomes equal to len1:
                    •   If str[i] != substr[0], go to step b else go to step d
                        // To find the first matching character
                    •   Set i = i + 1
                    •   If (i < len1) go to step a
                    •   If i = len1, go to step e, else go to step g
                    •   Display “Substring not found in the string”
                    •   Exit

     Ver. 1.0                                                                    Session 2
Data Structures and Algorithms
Exercise 5: Solution (Contd.)


                •   Set j = i     // First character matched. Now match the
                                  // remaining characters
                •   Set k = 0
                •   If str[j] = substr[k], go to step j, else go to step m
                •   Set j = j + 1
                •   Set k = k + 1
                •   If j < len1 and k < len2, go to step i
                •   If k = len2, go to step n, else go to step p
                •   Display “Substring found at position ” + (i + 1) + “ in the string”
                •   Exit
                •   Set i = i + 1




     Ver. 1.0                                                                   Session 2
Data Structures and Algorithms
Exercise 6


                Suppose you have two arrays of size 10 each containing
                elements in ascending order. Write an algorithm to merge
                the two arrays in such a way that the elements in the
                resulting array are arranged in the ascending order.




     Ver. 1.0                                                       Session 2
Data Structures and Algorithms
Exercise 6: Solution


                •   Accept two arrays, A1 and A2, each of size 10, from the
                    user.
                •   Declare an array, result, of size 20.
                •   Set i = j = k = 0.
                •   If (A1 [i] <= A2 [j]): // Insert the smaller element in the
                                           // result array
                     a. result [k] = A1 [i]
                     b. Set k = k + 1
                     c. Set i = i + 1
                •   If (A1[i] > A2[j]):       // Insert the smaller element in the
                                              // result array
                     a. result [k] = A2 [j]
                     b. Set k = k + 1
                     c. Set j = j + 1

     Ver. 1.0                                                                 Session 2
Data Structures and Algorithms
Exercise 6: Solution (Contd.)


                •   If (i < 10 and j < 10), go to step 4.   // If none of the lists
                                                            // has reached its
                    end
                •   Repeat until i equals 10: // If there are any elements left
                                                       // in A1, copy them to
                    result                                      // array

                     a. result [k] = A1 [i]
                     b. Set k = k + 1
                     c. Set i = i + 1
                •   Repeat until j equals 10: // If there are any elements left in
                                             // A2, copy them to the result
                                                       // array

                     a. result [k] = A2 [j]
     Ver. 1.0        b. Set k = k + 1                                        Session 2
Data Structures and Algorithms
Exercise 7


                Write an algorithm to find the Highest Common Factor
                (HCF) of three numbers.




     Ver. 1.0                                                      Session 2
Data Structures and Algorithms
Exercise 7: Solution


                1. Accept three numbers from the user. Store them in
                   variables num1, num2, and num3.
                2. Declare an integer variable, min.
                3. Assign the value of the smallest number among num1,
                   num2, and num3 to min by executing the following steps:
                    a. Set min = num1
                    b. If (num2 < min), set min = num2
                    c. If (num3 < min), set min = num3
                4. Declare an integer variable i.
                5. Set i = min.
                6. If ( num1 % i = 0 and num2 % i = 0 and num3 % i = 0 ):
                    •   Display i      // If i divides all the numbers, then HCF is i
                    •   Exit


     Ver. 1.0                                                                Session 2
Data Structures and Algorithms
Exercise 7: Solution (Contd.)


                1. Set i = i – 1.
                2. Go to step 6.




     Ver. 1.0                       Session 2
Data Structures and Algorithms
Exercise 8


                Write an algorithm to multiply two 3 × 3 matrices.




     Ver. 1.0                                                        Session 2
Data Structures and Algorithms
Algorithm 8: Solution


                1. Declare two 3 × 3 arrays, m1 and m2.
                2. Accept the elements of the two matrices and store them in
                    m1 and m2.
                3. Declare a 3 × 3 matrix, result, to store the result of
                    multiplication.
                4. Set i = 0.
                5. Set j = 0.
                6. Set result [i, j] = 0.
                7. Set k = 0.
                8. result [i, j] + = m1 [i, k] × m2 [k, j].
                9. Set k = k + 1.
                10. If k < 3, go to step 8.
                11. Set j = j + 1.

     Ver. 1.0                                                         Session 2
Data Structures and Algorithms
Algorithm 8: Solution (Contd.)


                1.   If j < 3, go to step 6.
                2.   Set i = i + 1.
                3.   If i < 3, go to step 5.
                4.   Display result.




     Ver. 1.0                                  Session 2
Data Structures and Algorithms
Algorithm 9


                Write a recursive algorithm to print the first n numbers in the
                Fibonacci series.




     Ver. 1.0                                                           Session 2
Data Structures and Algorithms
Algorithm 9: Solution


                Algorithm: Fibo (n)
                2. If n = 1, return 0
                3. If n = 2, return 1
                4. Return (Fibo (n – 1) + Fibo (n – 2))




     Ver. 1.0                                             Session 2

More Related Content

What's hot (19)

PPTX
Icom4015 lecture13-f16
BienvenidoVelezUPR
 
PPTX
Icom4015 lecture3-s18
BienvenidoVelezUPR
 
PDF
C sharp chap6
Mukesh Tekwani
 
PPTX
20101017 program analysis_for_security_livshits_lecture02_compilers
Computer Science Club
 
PDF
Lec 8 03_sept [compatibility mode]
Palak Sanghani
 
PDF
Algorithm chapter 1
chidabdu
 
PPTX
CIIC 4010 Chapter 1 f17
BienvenidoVelezUPR
 
PPTX
Java Foundations: Basic Syntax, Conditions, Loops
Svetlin Nakov
 
PDF
C sharp chap5
Mukesh Tekwani
 
PPTX
Algorithm Design and Complexity - Course 1&2
Traian Rebedea
 
PDF
C sharp chap4
Mukesh Tekwani
 
PDF
Daa q.paper
swapnil shinde
 
PPTX
Icom4015 lecture8-f16
BienvenidoVelezUPR
 
PPTX
Introduction to Neural Netwoks
Abdallah Bashir
 
PDF
Mlp trainning algorithm
Hưng Đặng
 
PPTX
The Mathematics of RSA Encryption
Nathan F. Dunn
 
PPTX
Icom4015 lecture3-f17
BienvenidoVelezUPR
 
PPTX
Icom4015 lecture15-f16
BienvenidoVelezUPR
 
Icom4015 lecture13-f16
BienvenidoVelezUPR
 
Icom4015 lecture3-s18
BienvenidoVelezUPR
 
C sharp chap6
Mukesh Tekwani
 
20101017 program analysis_for_security_livshits_lecture02_compilers
Computer Science Club
 
Lec 8 03_sept [compatibility mode]
Palak Sanghani
 
Algorithm chapter 1
chidabdu
 
CIIC 4010 Chapter 1 f17
BienvenidoVelezUPR
 
Java Foundations: Basic Syntax, Conditions, Loops
Svetlin Nakov
 
C sharp chap5
Mukesh Tekwani
 
Algorithm Design and Complexity - Course 1&2
Traian Rebedea
 
C sharp chap4
Mukesh Tekwani
 
Daa q.paper
swapnil shinde
 
Icom4015 lecture8-f16
BienvenidoVelezUPR
 
Introduction to Neural Netwoks
Abdallah Bashir
 
Mlp trainning algorithm
Hưng Đặng
 
The Mathematics of RSA Encryption
Nathan F. Dunn
 
Icom4015 lecture3-f17
BienvenidoVelezUPR
 
Icom4015 lecture15-f16
BienvenidoVelezUPR
 

Viewers also liked (16)

PPS
01 ds and algorithm session_01
Niit Care
 
PPS
Ds 1
Niit Care
 
PPS
03 ds and algorithm session_04
Niit Care
 
PPS
Learn C
kantila
 
PPS
05 ds and algorithm session_07
Niit Care
 
PPTX
Flowchart and algorithm
Sayali Shivarkar
 
PPTX
Python Programming Language
Laxman Puri
 
PPTX
Python PPT
Edureka!
 
PPT
Introduction to Python
amiable_indian
 
PPTX
Algorithms
Liam Dunphy
 
PDF
Writing algorithms
Krishna Chaytaniah
 
PPTX
Introduction to Pseudocode
Damian T. Gordon
 
PPTX
Algorithm and flowchart2010
Jordan Delacruz
 
PPTX
Algorithms and Flowcharts
Deva Singh
 
PPTX
Algorithm and flowchart
Elizabeth de Leon Aler
 
PPT
Introduction to Python
Nowell Strite
 
01 ds and algorithm session_01
Niit Care
 
Ds 1
Niit Care
 
03 ds and algorithm session_04
Niit Care
 
Learn C
kantila
 
05 ds and algorithm session_07
Niit Care
 
Flowchart and algorithm
Sayali Shivarkar
 
Python Programming Language
Laxman Puri
 
Python PPT
Edureka!
 
Introduction to Python
amiable_indian
 
Algorithms
Liam Dunphy
 
Writing algorithms
Krishna Chaytaniah
 
Introduction to Pseudocode
Damian T. Gordon
 
Algorithm and flowchart2010
Jordan Delacruz
 
Algorithms and Flowcharts
Deva Singh
 
Algorithm and flowchart
Elizabeth de Leon Aler
 
Introduction to Python
Nowell Strite
 
Ad

Similar to 02 ds and algorithm session_02 (20)

PDF
classVIII_Coding_Book018979929470479.pdf
menolem379
 
PPTX
classVIII_Coding_Teacher_Presentation.pptx
bhanutickets
 
DOC
Course module of DS
PCTE
 
DOC
Course Breakup Plan- C
swatisinghal
 
PDF
Anu DAA i1t unit
GANDIKOTA2012
 
PPT
L12 complexity
mondalakash2012
 
PPT
Programming.pptVBVBBMCGHFGFDFDHGDFKJKJKKJ;J
AlthimeseAnderson
 
PDF
CSE031.Lecture_07-FlowCharts_Pseudocode .Part_II.pdf
NourhanTarek23
 
PPTX
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
christinamary2620
 
PDF
Recursive algorithms in depth - Geek gap webinar #2
junior Teudjio
 
DOCX
UNIT-1.docx Design and Analysis of Algorithm
swethajosephsastry
 
PPTX
Array Data StructureData StructureData Structure.pptx
menukam354
 
PPTX
Programing techniques
Prabhjit Singh
 
PDF
Programming techniques
Prabhjit Singh
 
PDF
Plc (1)
James Croft
 
PDF
DATA STRUCTURE
RobinRohit2
 
PDF
DATA STRUCTURE.pdf
ibrahim386946
 
DOC
Paper
mrecedu
 
PPT
Unit6 jwfiles
mrecedu
 
PDF
June 05 P2
Samimvez
 
classVIII_Coding_Book018979929470479.pdf
menolem379
 
classVIII_Coding_Teacher_Presentation.pptx
bhanutickets
 
Course module of DS
PCTE
 
Course Breakup Plan- C
swatisinghal
 
Anu DAA i1t unit
GANDIKOTA2012
 
L12 complexity
mondalakash2012
 
Programming.pptVBVBBMCGHFGFDFDHGDFKJKJKKJ;J
AlthimeseAnderson
 
CSE031.Lecture_07-FlowCharts_Pseudocode .Part_II.pdf
NourhanTarek23
 
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
christinamary2620
 
Recursive algorithms in depth - Geek gap webinar #2
junior Teudjio
 
UNIT-1.docx Design and Analysis of Algorithm
swethajosephsastry
 
Array Data StructureData StructureData Structure.pptx
menukam354
 
Programing techniques
Prabhjit Singh
 
Programming techniques
Prabhjit Singh
 
Plc (1)
James Croft
 
DATA STRUCTURE
RobinRohit2
 
DATA STRUCTURE.pdf
ibrahim386946
 
Paper
mrecedu
 
Unit6 jwfiles
mrecedu
 
June 05 P2
Samimvez
 
Ad

More from Niit Care (20)

PPS
Ajs 1 b
Niit Care
 
PPS
Ajs 4 b
Niit Care
 
PPS
Ajs 4 a
Niit Care
 
PPS
Ajs 4 c
Niit Care
 
PPS
Ajs 3 b
Niit Care
 
PPS
Ajs 3 a
Niit Care
 
PPS
Ajs 3 c
Niit Care
 
PPS
Ajs 2 b
Niit Care
 
PPS
Ajs 2 a
Niit Care
 
PPS
Ajs 2 c
Niit Care
 
PPS
Ajs 1 a
Niit Care
 
PPS
Ajs 1 c
Niit Care
 
PPS
Dacj 4 2-c
Niit Care
 
PPS
Dacj 4 2-b
Niit Care
 
PPS
Dacj 4 2-a
Niit Care
 
PPS
Dacj 4 1-c
Niit Care
 
PPS
Dacj 4 1-b
Niit Care
 
PPS
Dacj 4 1-a
Niit Care
 
PPS
Dacj 1-2 b
Niit Care
 
PPS
Dacj 1-3 c
Niit Care
 
Ajs 1 b
Niit Care
 
Ajs 4 b
Niit Care
 
Ajs 4 a
Niit Care
 
Ajs 4 c
Niit Care
 
Ajs 3 b
Niit Care
 
Ajs 3 a
Niit Care
 
Ajs 3 c
Niit Care
 
Ajs 2 b
Niit Care
 
Ajs 2 a
Niit Care
 
Ajs 2 c
Niit Care
 
Ajs 1 a
Niit Care
 
Ajs 1 c
Niit Care
 
Dacj 4 2-c
Niit Care
 
Dacj 4 2-b
Niit Care
 
Dacj 4 2-a
Niit Care
 
Dacj 4 1-c
Niit Care
 
Dacj 4 1-b
Niit Care
 
Dacj 4 1-a
Niit Care
 
Dacj 1-2 b
Niit Care
 
Dacj 1-3 c
Niit Care
 

Recently uploaded (20)

PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
Designing Production-Ready AI Agents
Kunal Rai
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Designing Production-Ready AI Agents
Kunal Rai
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 

02 ds and algorithm session_02

  • 1. Data Structures and Algorithms Exercise 1 Write an algorithm to check whether a number is a prime number or not. Ver. 1.0 Session 2
  • 2. Data Structures and Algorithms Exercise 1: Solution 1. Accept a number from the user. Name it as num. 2. Declare an integer variable i. 3. Assign a value 2 to i. 4. Repeat until i > num/2: a. If num % i = 0: i. Display “The number is not prime” ii. Exit b. i = i + 1 5. Display “The number is prime”. Ver. 1.0 Session 2
  • 3. Data Structures and Algorithms Exercise 2 Write an algorithm to generate the first 10 prime numbers. Ver. 1.0 Session 2
  • 4. Data Structures and Algorithms Exercise 2: Solution 1. Declare an integer variable, count, and assign the value 0 to it. 2. Declare an integer variable, num, and assign the value 2 to it. 3. Repeat until count becomes equal to 10: a. If num is a prime number: i. Display num ii. Set count = count + 1 b. Set num = num +1 Ver. 1.0 Session 2
  • 5. Data Structures and Algorithms Exercise 3 Write an algorithm to accept a number between 1 and 9 and display a pattern. For example, if the number entered is 5, the following pattern should be displayed: 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 Ver. 1.0 Session 2
  • 6. Data Structures and Algorithms Exercise 3: Solution • Accept a number in the range of 1 - 9 from the user. Store it in a variable, num. • Declare an integer variable, i. • Set i = 1. • Repeat until i becomes greater than num: // To display // num rows a. Declare an integer variable j b. Set j = i c. Display j and insert a space d. Set j = j – 1 e. If j > 0 go to step c f. Insert a line break g. Set i = i + 1 Ver. 1.0 Session 2
  • 7. Data Structures and Algorithms Exercise 4 Write an algorithm to accept a number between 1 and 9 and display a pyramid. For example, if the number entered is 5, the following pyramid will be displayed: 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 Ver. 1.0 Session 2
  • 8. Data Structures and Algorithms Exercise 4: Solution 1. Accept a number from the user in the range of 1-9. Store it in a variable, n. 2. Set i = 1. 3. Repeat until i becomes greater than n: • Set j = 0 • Insert a space • Set j = j + 1 • If j < n – i, go to step b // To display n – i spaces • Set j = 1 • Display the value of j and insert a space • Set j = j + 1 • If j < i, then go to step f // To display numbers from 1 to i • Set j = i – 1 Ver. 1.0 Session 2
  • 9. Data Structures and Algorithms Exercise 4: Solution (Contd.) • Display the value of j and insert a space • Set j = j – 1 • If j > 0, go to step j // To display numbers from i – 1 down // to 1 • Give a line break • Set i = i + 1 Ver. 1.0 Session 2
  • 10. Data Structures and Algorithms Exercise 5 Write an algorithm to accept two strings and check whether the second string exists within the first string. For example, if the first string is “concatenation” and the second string is “cat”, the algorithm should display “Substring found at position 4 in the string”. However, if the first string is “concatenation” and the second string is “tent”, the algorithm should display “Substring not found in the string”. Ver. 1.0 Session 2
  • 11. Data Structures and Algorithms Exercise 5: Solution 1. Accept a string value from the user. Store it in a variable, str. 2. Accept the substring to be searched in str. Store it in a variable, substr. 3. Store the length of str in an integer variable, len1. 4. Store the length of substr in an integer variable, len2. 5. Set i = 0. 6. Repeat until i becomes equal to len1: • If str[i] != substr[0], go to step b else go to step d // To find the first matching character • Set i = i + 1 • If (i < len1) go to step a • If i = len1, go to step e, else go to step g • Display “Substring not found in the string” • Exit Ver. 1.0 Session 2
  • 12. Data Structures and Algorithms Exercise 5: Solution (Contd.) • Set j = i // First character matched. Now match the // remaining characters • Set k = 0 • If str[j] = substr[k], go to step j, else go to step m • Set j = j + 1 • Set k = k + 1 • If j < len1 and k < len2, go to step i • If k = len2, go to step n, else go to step p • Display “Substring found at position ” + (i + 1) + “ in the string” • Exit • Set i = i + 1 Ver. 1.0 Session 2
  • 13. Data Structures and Algorithms Exercise 6 Suppose you have two arrays of size 10 each containing elements in ascending order. Write an algorithm to merge the two arrays in such a way that the elements in the resulting array are arranged in the ascending order. Ver. 1.0 Session 2
  • 14. Data Structures and Algorithms Exercise 6: Solution • Accept two arrays, A1 and A2, each of size 10, from the user. • Declare an array, result, of size 20. • Set i = j = k = 0. • If (A1 [i] <= A2 [j]): // Insert the smaller element in the // result array a. result [k] = A1 [i] b. Set k = k + 1 c. Set i = i + 1 • If (A1[i] > A2[j]): // Insert the smaller element in the // result array a. result [k] = A2 [j] b. Set k = k + 1 c. Set j = j + 1 Ver. 1.0 Session 2
  • 15. Data Structures and Algorithms Exercise 6: Solution (Contd.) • If (i < 10 and j < 10), go to step 4. // If none of the lists // has reached its end • Repeat until i equals 10: // If there are any elements left // in A1, copy them to result // array a. result [k] = A1 [i] b. Set k = k + 1 c. Set i = i + 1 • Repeat until j equals 10: // If there are any elements left in // A2, copy them to the result // array a. result [k] = A2 [j] Ver. 1.0 b. Set k = k + 1 Session 2
  • 16. Data Structures and Algorithms Exercise 7 Write an algorithm to find the Highest Common Factor (HCF) of three numbers. Ver. 1.0 Session 2
  • 17. Data Structures and Algorithms Exercise 7: Solution 1. Accept three numbers from the user. Store them in variables num1, num2, and num3. 2. Declare an integer variable, min. 3. Assign the value of the smallest number among num1, num2, and num3 to min by executing the following steps: a. Set min = num1 b. If (num2 < min), set min = num2 c. If (num3 < min), set min = num3 4. Declare an integer variable i. 5. Set i = min. 6. If ( num1 % i = 0 and num2 % i = 0 and num3 % i = 0 ): • Display i // If i divides all the numbers, then HCF is i • Exit Ver. 1.0 Session 2
  • 18. Data Structures and Algorithms Exercise 7: Solution (Contd.) 1. Set i = i – 1. 2. Go to step 6. Ver. 1.0 Session 2
  • 19. Data Structures and Algorithms Exercise 8 Write an algorithm to multiply two 3 × 3 matrices. Ver. 1.0 Session 2
  • 20. Data Structures and Algorithms Algorithm 8: Solution 1. Declare two 3 × 3 arrays, m1 and m2. 2. Accept the elements of the two matrices and store them in m1 and m2. 3. Declare a 3 × 3 matrix, result, to store the result of multiplication. 4. Set i = 0. 5. Set j = 0. 6. Set result [i, j] = 0. 7. Set k = 0. 8. result [i, j] + = m1 [i, k] × m2 [k, j]. 9. Set k = k + 1. 10. If k < 3, go to step 8. 11. Set j = j + 1. Ver. 1.0 Session 2
  • 21. Data Structures and Algorithms Algorithm 8: Solution (Contd.) 1. If j < 3, go to step 6. 2. Set i = i + 1. 3. If i < 3, go to step 5. 4. Display result. Ver. 1.0 Session 2
  • 22. Data Structures and Algorithms Algorithm 9 Write a recursive algorithm to print the first n numbers in the Fibonacci series. Ver. 1.0 Session 2
  • 23. Data Structures and Algorithms Algorithm 9: Solution Algorithm: Fibo (n) 2. If n = 1, return 0 3. If n = 2, return 1 4. Return (Fibo (n – 1) + Fibo (n – 2)) Ver. 1.0 Session 2

Editor's Notes

  • #2: Ask the student to write the algorithm first, and then move to the next slide.
  • #4: Ask the students to write the algorithm first and then move to the next slide.
  • #6: Ask the students to write the algorithm first and then move to the next slide.
  • #8: Ask the students to write the algorithm first and then move to the next slide.
  • #11: Ask the students to write the algorithm first and then move to the next slide.
  • #14: Ask the students to write the algorithm first and then move to the next slide.
  • #17: Ask the students to write the algorithm first and then move to the next slide.
  • #20: Ask the students to write the algorithm first and then move to the next slide.
  • #23: Ask the students to write the algorithm first and then move to the next slide.