THE EAST AFRICAN UNIVERSITY (TEAU)
SCHOOL OF COMPUTER SCIENCE AND IT
DEPARTMENT OF COMPUTER SCIENCE
JAN – APRIL2019, MAIN EXAMINATION
COURSE : DATA STRUCTURES AND ALGORITHM
CODE : BIT 220
TIME : 2 Hours
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.
DO NOT WRITE ANYWHERE ON THIS QUESTION PAPER
QUESTION ONE
a) State any three characteristics that define a data structure
(3 marks)
b) Describe two main elements of an instruction
(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)
d) Write a program to display 1,3,5 , 7,8 as a queued array program
(15 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
a) State two operations performed by a stack
(2 marks)
b) Compare a stack and queue operation as used in data structure
(6 marks)
c) The figure below shows how a stack works
Write a program to display one of the arrows shown
(12 marks)
QUESTION FOUR
a) State three advantages and three disadvantages of linking a list
(6 marks)
b) Queue program
#include<iotream>
using name space std;
#define SIZE 10
class Queue
{
int a[SIZE];
int rear;
int front;
public:
Queue()
{
rear = front = -1;
void enqueue(int x);
int dequeue();
void display();
};
void Queue :: enqueue(int x)
{
if(front == -1) {
front++;
}
if( rear == SIZE-1)
cout << "Queue is full";
}
else
{
a[++rear] = x;
}
}
int Queue :: dequeue()
{
return a[++front]; }
void Queue :: display()
{
int i;
for( i = front; i <= rear; i++)
cout << a[i] << endl;
}
}
int main()
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)
c) State four (4) benefits of encapsulation in a program
(4 marks)