Académique Documents
Professionnel Documents
Culture Documents
Q1.
(a). What is a function?
A function is a group of statements that together perform a task. Every C program
has at least one function, which is main(), and all the most trivial programs can
define additional functions. You can divide up your code into separate functions.
(2marks)
A function is recursive if a statement in the body of the function calls itself. Recursion is the
process of defining something in terms of itself. For a computer language to be recursive, a
function must be able to call itself. (2marks)
For example, let us consider the function factr() shown below, which computers the factorial
of an integer. (4marks)
#include
<stdio.h>
int
factorial (int);
main()
{
int num, fact;
printf (Enter a positive integer value: ");
scanf (%d, &num);
fact = factorial
(num); printf ("\n Factorial of %d =%5d\n", num, fact);
}
int factorial (int n)
{
int result;
if (n == 0)
return (1);
else
result = n * factorial (n-1);
return (result);
}
Q2.
(A) What is the importance of the stopping case in recursive functions?
When writing recursive functions, you must have a exit condition somewhere to force the
function to return without the recursive call being executed. If you do not have an exit
condition, the recursive function will recurse forever until you run out of stack space and
indicate error about lack of memory, or stack overflow.
(2marks)
For example, let us consider the function factr() shown below, which computers the factorial
of an integer.
#include
<stdio.h>
int
factorial (int);
main()
{
int num, fact;
printf (Enter a positive integer value: ");
scanf (%d, &num);
fact = factorial
(num); printf ("\n Factorial of %d =%5d\n", num, fact);
}
int factorial (int n)
{
int result;
if (n == 0)
return (1);
else
result = n * factorial (n-1);
return (result);
}
(8marks)
Searching is used to find the location where an element is available. There are two types of
search techniques. They are:
(3marks)
1. Bubble sort
2. Quick sort
3. Selection sort and
4. Heap sort
(3marks)
1. Internal sorting
2. External sorting
(2marks)
If all the elements to be sorted are present in the main memory then such sorting is called
internal sorting on the other hand, if some of the elements to be sorted are kept on the
secondary storage, it is called external sorting.
(2marks)
Q4. Briefly explain the following terms associated with binary tree:
(a) Leaf node
(b) Path
(c) Siblings
(d) Ancestor and Descendent
(e) Subtree
(f) Level
(g) Height
(h) Depth
a. A node with no children is called a leaf (or external node). A node which is not a leaf
is called an internal node.(1mark)
b. A sequence of nodes .(1mark)
c. The children of the same parent are called siblings (1mark)
d. If there is a path from node A to node B, then A is called an ancestor of B and
B is called a descendent of A. (2marks)
h. The depth of a node is the number of nodes along the path from the root to that
node. (2marks)
45
30
25
4
20
50
2
40
55
35
5
15
(a) A spanning tree for a connected graph is a tree whose vertex set is the same as the
vertex set of the given graph, and whose edge set is a subset of the edge set of the
given graph. i.e., any connected graph will have a spanning tree. (3marks)
(b)
Cost
10
15
20
25
30
35
40
45
50
55
Edge (1, 2) (3, 6) (4, 6) (2, 6) (1, 4) (3, 5) (2, 5) (1, 5) (2, 3) (5, 6)
COST
STAGES IN KRUSKALS
ALGORITHM
REMARKS
(1,
10
2
3
5
6
(3, 6)
15
2
3
5
6
(4, 6)
20
2
3
5
6
(2, 6)
25
2
3
5
6
(1, 4)
30
(3, 5)
35
Reject
3
5
Q6.
(A) What is the importance of the stopping case in recursive functions?
When writing recursive functions, you must have a exit condition somewhere to force the
function to return without the recursive call being executed. If you do not have an exit
condition, the recursive function will recurse forever until you run out of stack space and
indicate error about lack of memory, or stack overflow.
(2marks)
For example, let us consider the function factr() shown below, which computers the factorial
of an integer.
#include
<stdio.h>
int
factorial (int);
main()
{
int num, fact;
printf (Enter a positive integer value: ");
scanf (%d, &num);
fact = factorial
(num); printf ("\n Factorial of %d =%5d\n", num, fact);
}
int factorial (int n)
{
int result;
if (n == 0)
return (1);
else
result = n * factorial (n-1);
return (result);
}
(8marks)
Iteration
Iteration explicitly user a repetition
structure.
Iteration terminates when the loop
continuation.
Iteration keeps modifying the counter
until the loop continuation condition
fails.
Iteration normally occurs within a loop
so the extra memory assigned is
omitted.
It reduces the processors operating
time.
(7marks)
Recursion
Recursion achieves repetition through
repeated function calls.
Recursion terminates when a base case
is recognized.
Recursion keeps producing simple
versions of the original problem until
the base case is reached.
Recursion causes another copy of the
function and hence a considerable
memory spaces occupied.
It increases the processors operating
time.