You are on page 1of 5

UNIVERSITY EXAMINATIONS

EXAMINATION FOR JANUARY/APRIL 2015/2016 FOR DIPLOMA IN COMPUTER


SCIENCE

DIT 012 DATA STRUCTURES AND ALGORITHM PRACTICALS

DATE 7th APRIL, 2016 TIME: 1 HOUR

GENERAL INSTRUCTIONS:

Students are NOT permitted to write on the examination paper during reading time.

This is a closed book examination. Text book/Reference books/notes are not permitted.

SPECIAL INSTRUCTIONS:

This examination paper consists Questions in Section A followed by section B.

Answer ALL questions.

QUESTIONS in ALL Sections should be answered in answer booklet(s).

1. PLEASE start the answer to EACH question on a NEW PAGE.


2. Keep your phone(s) switched off at the front of the examination room and NOT
on your person.
3. Keep ALL bags and caps at the front of the examination room and DO NOT
refer to ANY unauthorized material before or during the course of the
examination.
4. ALWAYS show your working.
5. Marks indicated in parenthesis i.e. ( ) will be awarded for clear and logical
answers.
6. Write your REGISTRATION No. clearly on the answer booklet(s).
7. For the Questions, write the number of the question on the answer booklet(s) in
the order you answered them.
8. DO NOT use your PHONE as a CALCULATOR.
9. YOU are ONLY ALLOWED to leave the exam room 30minutes to the end of the
Exam.
10. DO NOT write on the QUESTION PAPER. Use the back of your BOOKLET for
any calculations or rough work.
Answer ALL Questions
a) Study the following diagram.

Write a program that will execute the above algorithm. (3 Marks)


Program

b) Execute the following program code. ( 3 Marks)

main()
{
int a, b, c;

printf("Enter two numbers to add\n");


scanf("%d%d",&a,&b);

c = a + b;

printf("Sum of entered numbers = %d\n",c);


return 0;
}

i. Where there any errors? (1 Mark)


ii. Sketch a flow chart of the algorithm of correcting errors in (i) above if any.
(2 Marks)
iii. Explain the output. ( 2 Marks)
c) Execute the code below. ( 3 Marks)

#include<stdio.h>
#include<stdlib.h>

typedef struct ADT


{
int capacity;
int size;
int *elements;
}ADT;

ADT * createADT(int maxElements)


{
/* Create a ADT */
Adt *S;
S = (ADT *)malloc(sizeof(Stack));
/* Initialise its properties */
S->elements = (int
*)malloc(sizeof(int)*maxElements);
S->size = 0;
S->capacity = maxElements;

return S;
}

void remove(ADT *S)


{

if(S->size==0)
{
printf("ADT is Empty\n");
return;
}

else
{
S->size--;
}
return;
}
int top(ADT *S)
{
if(S->size==0)
{
printf("ADT is Empty\n");
exit(0);
}
return S->elements[S->size-1];
}

void add(ADT *S,int element)


{
if(S->size == S->capacity)
{
printf("ADT is Full\n");
}
else
{
S->elements[S->size++] = element;
}
return;
}
int main()
{
ADT *S = createStack(5);
add(S,7);
add(S,5);
add(S,21);
add(S,-1);

remove(S);
printf("Top element is %d\n",top(S));

}
i. Were there any errors? ( 1 Mark)
ii. What was the Abstract Data Type (ADT) being implemented by the program? Give
Reason. (3 Marks)
iii. What was the top element at the end? Give Reason. (2 Marks)
iv. What do you think were the functions of add and remove? ( 2 Marks)

d) Run the following program code. ( 2 Marks)

main()
{
int x = 5

while ( x > 0 )
{
printf("You are a visitor number: %d\n", i );
i = i -1;
}
}
i. Were there any errors in the program? ( 1 Mark)
ii. Use words to explain the algorithm of correcting errors in (i) above if any.
(2 Marks)
iii. How many visitors were present? ( 2 Marks)
iv. Were there visitors assigned student number 0 and 5? ( 1 Mark)

You might also like