Data Structures and Algorithm2
Data Structures and Algorithm2
INSTRUCTIONS
1. The Paper is made up of FIVE (5) Questions, question ONE is compulsory plus any other
TWO questions.
2. Credit is given for legibility, clarity of expressions and use of relevant illustrations.
3. Clearly write your registration number on each answer sheet used.
QUESTION ONE
(4 marks)
c) Illustrate using a block diagram, how FIFO (first in first out) in a queue data
structure indicating position of front and rear
(8 marks)
QUESTION TWO
a) Write a program with elements {1,3,5,7,8} that will delete an element index 3 or
insert an element 4 at index 3
(10 arks)
b) The program below is a linear ARRAY with mistakes, debug it and rewrite its
executable code
#include <stdio.h> void main() { int LA[] = {1,3,5,7,8}; int item = 5, n = 5; int i =
0, j = 0; printf("The original array elements are :\n"); for(i = 0; i<n; i++)
{ printf("LA[%d] = %d \n", i, LA[i]); } while( j < n){ if( LA[j] == item )
{ break; } j = j + 1; } printf("Found element %d at position %d\n", item, j+1); }
(10 marks)
QUESTION THREE
(12 marks)
QUESTION FOUR
b) Queue program
#include<iotream>
#define SIZE 10
class Queue
{
int a[SIZE];
int rear;
int front;
public:
Queue()
{
rear = front = -1;
Queue q;
q.enqueue(10);
q.enqueue(100);
q.enqueue(1000);
q.enqueue(1001);
q.enqueue(1002);
q.dequeue();
q.enqueue(1003);
q.dequeue();
q.dequeue();
q.enqueue(1004);
q.display();
return 0;
}
The program above has several errors. Debug it and reconstruct the program to
executable format
(14 marks)
QUESTION FIVE
a) Define encapsulation
(2 marks)
b) Write a programming structure to show how encapsulation is implemented in a
program using any basic data structure in the build in data types
(14marks)