Professional Documents
Culture Documents
1. What is an algorithm? State the three different ways an algorithm can be represented
2. Define the following terms
i) Data structure
ii) Null pointer
3. Give the difference between the following stating examples in each case
i) Heterogeneous and homogenous data structure
ii) Static and dynamic data structure
iii) Linear and nonlinear data structure
4. Revers Polish notation (RPN) is a way of representing calculations. Find the results of
each of the following calculations.
a. -87*
b. 93+21-/
c. 87463*2/*+-
5. Figure 1a shows a flow chart of the algorithm in figure 1b.
1. Algorithm WhatAmIDoing.
2. Var F, n i: integer;
3. Start
4. Write(“Enter an integer”);
5. Read(n);
6. i:= 1;
7. F:= 1;
8. While (i<=n) do
9. F: =F*i;
10. i:= i+1 ;
11. Endwhile;
12. Write(“The value of F is”,F);
13. Stop
Figure 1a Figure 1b
i. Distinguish between pseudocode and flowchart
ii. What name is generally given to code segment from line 8 to line 11?
iii. Write the output of the algorithm when “n” takes the following values
a) 4 b) 6
iv. Bearing in mind your output in (iii) above, state in one sentence what the
algorithm does.
SWE114 Introduction to Algorithms (Mr. Ndayo Fonteng R) 1
6. The function recur below is a recursive function. Study the function and answer the questions that
follow.
8. The travelling salesman problem involves finding the shorted route between cities given
that each city is visited once and only once.
a. The traveling salesman problem is an example of an intractable problem. What is an
intractable problem?
b. What strategy can be used to reduce the time needed to solve an intractable problem?
c. What do you understand by algorithm complexity?
d. Write the linear search algorithm
e. Estimate the time complexity of the algorithm written above
f. What is the difference between a computer program and an algorithm?
9. i) Euclid’s algorithm for finding the greatest common divisor of two positive integers, m and n, is
described by the following properties.
3. Otherwise, gcd (m, n) = gcd (n, r); where r is the remainder when m is divided by n
a. What name is given to the algorithm design (problem solving) technique used by Euclid’s
algorithm?
b. What are the first two properties called?
c. Find the greatest common divisor of 243 and 171 using Euclid’s algorithm. Show all the steps
in your solution.
d. Write in pseudocode, a function named gcd that takes as parameters two positive integers m
and n, computes and returns their gcd based on the three properties above. 2marks
SWE114 Introduction to Algorithms (Mr. Ndayo Fonteng R) 2
10. Study the algorithm below and answer the questions that follow.
Trace table
Count <--- 0
Total <--- 0 Count Total Number Average
Input (Number)
While Number >= 0 Do
Count <--- Count + 1
Total <--- Total + Number
Trace table
Input(Number)
Endwhile
Average <--- Total/Count
a) Dry run the above algorithm and complete the trace table. Use the values 5, 7, 23 and -1 as input.
b) Does the dry run in (a) above show any error in the algorithm? Explain
c) Explain the difference between a count-controlled loop and a condition-controlled loop and state
which of them has been used in the above algorithm.
11. What is a data structure? State two main factors that determine the choice of a data structure for a
program.
12. The Big-O notation is often used to describe the efficiency of an algorithm.
(a) Place the following algorithms in order of efficiency, the most efficient first.
Algorithm A that is O(n)
Algorithm B that is O(aⁿ)
Algorithm C that is O(n²)
(b) Describe a linear search and explain why it is O(n).
(c) Describe a bubble sort and explain why it is O(n²).
13. Write an algorithm that will search for a string pattern in a given string using the naïve algorithm
14. Write an algorithm that will search for a string pattern in a given string using the Rabin Karp
algorithm
15. Given the following values 8,4,13,2,6,11,1,5,3,7,10,15,14,12,16 insert these values into a binary
search tree. Hence Perform a pre-order, in-order and post-order tree traversal. What do you
observe from the resulting values
i. Write an algorithm that will perform an in order traversal of the above tree.
ii. Write an algorithm for a breadth traversal of the tree