02 ds and algorithm session_02

23
Session 2 Ver. 1.0 Data Structures and Algorithms Write an algorithm to check whether a number is a prime number or not. Exercise 1

Transcript of 02 ds and algorithm session_02

Page 1: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

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

Exercise 1

Page 2: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

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”.

Exercise 1: Solution

Page 3: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

Exercise 2

Write an algorithm to generate the first 10 prime numbers.

Page 4: 02 ds and algorithm session_02

Session 2Ver. 1.0

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

Page 5: 02 ds and algorithm session_02

Session 2Ver. 1.0

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

Page 6: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

Exercise 3: Solution

1. Accept a number in the range of 1 - 9 from the user. Store it in a variable, num.

2. Declare an integer variable, i.

3. Set i = 1.

4. Repeat until i becomes greater than num: // To display // num

rowsa. 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

Page 7: 02 ds and algorithm session_02

Session 2Ver. 1.0

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

Page 8: 02 ds and algorithm session_02

Session 2Ver. 1.0

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:a. Set j = 0

b. Insert a space

c. Set j = j + 1

d. If j < n – i, go to step b // To display n – i spaces

e. Set j = 1

f. Display the value of j and insert a space

g. Set j = j + 1

h. If j < i, then go to step f // To display numbers from 1 to i

i. Set j = i – 1

Page 9: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

Exercise 4: Solution (Contd.)

j. Display the value of j and insert a space

k. Set j = j – 1

l. If j > 0, go to step j // To display numbers from i – 1 down // to 1

m. Give a line break

n. Set i = i + 1

Page 10: 02 ds and algorithm session_02

Session 2Ver. 1.0

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”.

Page 11: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

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:a. If str[i] != substr[0], go to step b else go to step d

// To find the first matching character

b. Set i = i + 1

c. If (i < len1) go to step a

d. If i = len1, go to step e, else go to step g

e. Display “Substring not found in the string”

f. Exit

Exercise 5: Solution

Page 12: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

g. Set j = i // First character matched. Now match the // remaining characters

h. Set k = 0

i. If str[j] = substr[k], go to step j, else go to step m

j. Set j = j + 1

k. Set k = k + 1

l. If j < len1 and k < len2, go to step i

m. If k = len2, go to step n, else go to step p

n. Display “Substring found at position ” + (i + 1) + “ in the string”

o. Exit

p. Set i = i + 1

Exercise 5: Solution (Contd.)

Page 13: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

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.

Exercise 6

Page 14: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

Exercise 6: Solution

1. Accept two arrays, A1 and A2, each of size 10, from the user.

2. Declare an array, result, of size 20.

3. Set i = j = k = 0.

4. If (A1 [i] <= A2 [j]): // Insert the smaller element in the // result arraya. result [k] = A1 [i]

b. Set k = k + 1

c. Set i = i + 1

5. If (A1[i] > A2[j]): // Insert the smaller element in the // result arraya. result [k] = A2 [j]

b. Set k = k + 1

c. Set j = j + 1

Page 15: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

6. If (i < 10 and j < 10), go to step 4. // If none of the lists // has reached its end

7. 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

8. Repeat until j equals 10: // If there are any elements left in // A2, copy them to the result

// array a. result [k] = A2 [j]

b. Set k = k + 1

c. Set j = j + 1

9. Display result.

Exercise 6: Solution (Contd.)

Page 16: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

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

Exercise 7

Page 17: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

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 ):a. Display i // If i divides all the numbers, then HCF is i

b. Exit

Exercise 7: Solution

Page 18: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

7. Set i = i – 1.

8. Go to step 6.

Exercise 7: Solution (Contd.)

Page 19: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

Write an algorithm to multiply two 3 × 3 matrices.

Exercise 8

Page 20: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

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.

Algorithm 8: Solution

Page 21: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

12. If j < 3, go to step 6.

13. Set i = i + 1.

14. If i < 3, go to step 5.

15. Display result.

Algorithm 8: Solution (Contd.)

Page 22: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

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

Algorithm 9

Page 23: 02 ds and algorithm session_02

Session 2Ver. 1.0

Data Structures and Algorithms

Algorithm: Fibo (n)

1. If n = 1, return 0

2. If n = 2, return 1

3. Return (Fibo (n – 1) + Fibo (n – 2))

Algorithm 9: Solution