MIDTERM EXAMINATION
SPRING 2020
ETE308 –Higher Data Structure and Algorithms
2 May 2020
(1 Hour)
Name
ID
INSTRUCTIONS TO STUDENT
1. This Question paper consists of 5 pages including cover page with 2 Questions only.
2. Answer ALL questions. The distribution of the marks for each question is given.
3. Please write all your answers in the answer sheet provided.
ETE308 – Higher Data Structure and Algorithms
QUESTION 1: [20 MARKS]
(a) Many of beginners and experienced programmers avoid learning data structure and
algorithms because it is complicated and they think that there is no use of it in real life.
Briefly describe FIVE (5) reasons why data structures and algorithms are important to
learn? Provide an example to support your answer.
[9 Marks]
(b) The following pseudocode displays all multiples of 2 in the range of lower and upper
bounds (both number are inclusive) specified by user. Based on the comments provided,
complete the C program.
#include <stdio.h>
int main() {
int num, lower, upper;
//Prompt user for lower and upper
----------(1)----------//Add the print input lower and upper numbers
scanf("%d %d", &lower, &upper);
------------(2)-------------- //Print “Multiples of 2 are”
----------(3)----------- //Set num = lower
------(4)---------//Using a while number, less than or equal to upper
{
if(num%2==0)
--------(5)------------- //add the Print function for the number
---------(6)-------------//Increment count by 1
}
return 0;
}
[6 Marks]
(c) Consider the following incomplete C program consisting with arrays and nested loops.
Based on the comments provided, write the correct alternative to replace the blank line
and provide the expected output of the program
#include <stdio.h>
#include <stdlib.h>
__________(1)___________________ //Add the constant macro to define array bound of N
__________(2)_________________ //Add the constant macro to define array bound of M
int main(void) {
int i, j;
int c[N][M] = {
{1, 2},
{3, 4},
{5, 6}
};
printf("Array \t\t Value\n");
2
ETE308 – Higher Data Structure and Algorithms
for (i = 0; i < N; i++)
____(3)_____________ //use another for loop to set all values of array C[M] to its subscript
printf("c[%d][%d] \t %d\n",i, j, c[i][j]);
_______(4)_______________ //Return the starting 0
}
[5 Marks]
QUESTION 2: [20 MARKS]
(a) List down the steps involved in using an array
[4 Marks]
(b) The following VP diagram illustrate a linked list in Figure 2(a). Each node has a member
that stores a student’s name and another member that store the student’s age. Write a C
program to create the list.
Figure 2(a)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char name[20];
int age;
struct node *next;
};
int main(){
struct node *head;
struct node *temp;
3
ETE308 – Higher Data Structure and Algorithms
_________(1)________________________
//Create a head node and assign the allocated memory to head
strcpy(head->name, "ang");
________(2)_______________ //Place 20 into the age member of the head node
_________(3)______________________
//Create a new node and assign the allocated memory to head->nx
temp = head->next;
strcpy(temp->name, "khairul");
__________(4)__________ //Place 19 into the age member of the new node
__________(5)__________________________
//Create a new node and assign the allocated memory to temp->next
temp = temp->next;
strcpy(temp->name, "lui");
______(6)__________ //Place 19 into the age member of the new node
_______(7)______________________________
//Create a new node and assign the allocated memory to temp->next
temp = temp->next;
strcpy(temp->name, "wafa");
_______(8)________________ //Place 20 into the age member of the new node
_______(9)______________ //Set temp->next to NULL
_______(10)_________ //Set temp to the head node
while(temp){
printf("%s\t%d\n", temp->name, temp->age);
temp = temp->next;
}
return 0;
}
[10 marks]
4
ETE308 – Higher Data Structure and Algorithms
(c) Consider a binary tree shown in Figure Q2(c), give the sequence of the visited nodes in
preorder, postorder and inorder traversals.
Figure Q2(c)
[6 Marks]
End of Paper