You are on page 1of 7

ETE308 Higher Data Structure & Algorithms Spring 2020

FINAL EXAMINATION
(Answer Sheet)
Name Ahmed Samyan Nayyef
ID No. A11900712
Write your full name and student ID No.
Candidate must answer ALL questions

Arrange your answer properly

Note: Save your answer in pdf form before you send it

Answer:

Q1/A-
1- False. An array can only store a grouping of data of the same type such as fixed-point variables,
floating-point variables and pointers etc.
2- False. The remaining elements will NOT be initialized to the last value in the initializer list, they
will be initialized with O’s.

B-
Output is:
1
4
9
16
25
Total is 55.

C-(i)
int main()
{

int r[10]={};
for (int index = 0; index< 6; index++)
printf("%d\n",r[index]);
return 0;
}

(ii)-

1
ETE308 Higher Data Structure & Algorithms Spring 2020

int main()
{

int array[3][2] = {1, 2, 3, 4, 5, 6};


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2 ; j++) {
printf("%d\n",array[i][j]);
}
printf("\n");
}
return 0;
}

D-
1- #define N 3
2- char product[20];
3- for(k=0;k<N;k++)
4- sum +=cost[k];
5- average= sum/N;
printf("Average: %5.1f\n",average);

Q2/A-
1- head=(struct node *)malloc(sizeof(struct node));
2- head->value=1;
3- temp=(struct node *)malloc(sizeof(struct node));
4- head->next->value=10;
5- struct node *newnod;
newnod=(struct node *)malloc(sizeof(struct node));
temp = &newnod;
6- head->next->next->value=100;
7- head->next->next->next=NULL;
8- temp=&head;

B-
(i)-

2
ETE308 Higher Data Structure & Algorithms Spring 2020

(ii)-

C-
(i)-

3
ETE308 Higher Data Structure & Algorithms Spring 2020

(ii)-
1- struct BinTreeNode {
char data;
struct BinTreeNode *left;
struct BinTreeNode *right;
};

2- setTree();
3- printf("\nInorder traversal: ");
inorder(root);
printf("\nPostorder traversal: ");
postorder(root);
printf("\nPreorder traversal: ");
preorder(root);
4- temp = (struct BinTreeNode *)malloc(sizeof(struct BinTreeNode));
5- temp->data = data;
6- return(temp);
7- root->right->right = initBinTreeNode('e');
root->right->left = initBinTreeNode('c');
8- parent1 = root->right->right;
parent1->right =initBinTreeNode('g');
9- inorder(node->left);
printf("%c",node->data);
inorder(node->right);

4
ETE308 Higher Data Structure & Algorithms Spring 2020

Q3/A-
Recursion Iteration
The statement in a body of a function calls Allows the set of instructions to be
the function itself. repeatedly executed.
It is always applied to functions. It is applied to iteration statements or
“loops”.
Slow in execution Fast in execution
It Reduces the size of the code It makes the code longer

Example program for recursion:

int factorial(int num){


int answer;
if (num==1) {
return 1;
}else{
answer = factorial(num-1) * num; //recursive calling
}
return (answer);
}

Example program for iteration:

int factorial(int num){


int answer=1;
for(int t =1; t>num; t++)
{
answer=answer * (t);
return (answer);
}
}

B-
(i)-
F(3)= 5*5^2*5^1 =625
(ii)-
int power(int n)
{
if (n==0)
return 1;
else
return 5 * power(n-1);
}

5
ETE308 Higher Data Structure & Algorithms Spring 2020

(iii)-
#include <stdio.h>
#include <stdlib.h>

int power(int n);


int main(void){
int n;
printf("Enter N: ");
scanf("%d", &n);
printf("\n5 to %d power is: %d",n,power(n));
return 0;
}
int power(int n)
{
if (n==0)
return 1;
else
return 5 * power(n-1);
}

(iv)-

int power(int n)
{
int product=1;
for(int i=1;i<=n;i++)
product *=5;
return product;
}

Q4\A-
(i)-
T(n) = W(n) = A(n)
= 1 + 4n^2+ 6n + 2
= 4n^2+ 6n + 3

(ii)-
T(n)=3+T(n-1)
=3+{3+T(n-2)}=3(2)+T(n-2)
=3(2)+{3+T(n-3)}=3(3)+T(n-3)
=…
=3(n)+T(n-n)=3(n)+T(0)
=3n+1

6
ETE308 Higher Data Structure & Algorithms Spring 2020

(iii)-

T(n)=(2n+2)+n(n+1) = 2n+2 = n2+n = n2+3n+2


Big-O: O(n2)

B-
(i)-
int NumberEven(int a[], int n){ //none
int j, Number=0, n; //1

for(j=0; j<n; j++){ //1+(n+1)+n= 2n+2


if(a[j]%2==0) //2n
Number++; //n
printf("%d", Number);
return Number;
}
(ii)-

W(n)=1 + 2n + 2 + 2n + n = 5n + 3
A(n)=1 + 2n/2 + 2 + 2n/2 + n/2 = 2.5n+3
Big-O(f(n)) = n times if n is any positive integer

You might also like