0% found this document useful (0 votes)
101 views

Data Structure Assignment 1

The document provides sample code and questions related to data structures. It includes code snippets testing pointers and arrays, calculating complexity of algorithms, and implementing a recursive function to find the greatest common divisor (GCD) of two integers. Students are asked to evaluate code, determine outputs, compare algorithms, and calculate GCD values through recursion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Data Structure Assignment 1

The document provides sample code and questions related to data structures. It includes code snippets testing pointers and arrays, calculating complexity of algorithms, and implementing a recursive function to find the greatest common divisor (GCD) of two integers. Students are asked to evaluate code, determine outputs, compare algorithms, and calculate GCD values through recursion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

MTS 3023: DATA STRUCTURES

SEMESTER 2 SESI 2019/2020


ASSIGNMENT 1

Answer ALL of the questions below:

1. Given the declaration:

int x;
int *p;
int *q;

Evaluate the following statements as valid or invalid. If the statement is


invalid, please explain why.

a. p = q;
b. *p = 56;
c. p = x;
d. *p = *q;
e. q = &x;
f. *p =q;

Answer:

a. Valid, two pointers can be equated

b. Valid, if memory is allocated for the variable

c. Invalid, as both of them are not of the same type

d. Valid, if q is pointing to a valid memory location

e. Valid, here q contains the address of x

f. Invalid, *p refers to contents at a particular location and q refers to an


address. Thus *p and q were different data-type which can not be assigned
one to another

2. What is the output of the following code?


int *p;
int *q;
int i;

p = new int[5];
p[0] = 5;

for (i = 1; i < 5; i++)


p[i] = p[i – 1] + 2 * i;

cout<< “Array p: “;
for (i = 0; i < 5; i++)
cout<< p[i] <<” “;
cout<< endl;

q = new int[5];

for (i = 1; i < 5; i++)


q[i] = p[4 – i];

cout<< “Array q: “;
for (i = 0; i < 5; i++)
cout<< q[i] <<” “;
cout<< endl;

Answer:

Output:

Array p: 5, 7, 11, 17, 25

Arrat q: -842150451, 17, 11, 7, 5

3. Please compare the algorithm below:


Algorithm 1
1. for i= 0 to i = 4
1.1 get the num[i]
1.2 sum = sum + num[i]
2. Average = sum / 5
3. Print sum
4. Print average

Algorithm 2
1. for i= 0 to i = 4
1.1. get the num[i]
2. for i= 0 to i = 4
2.1 sum = sum + num[i]
3. Average = sum / 5
4. Print sum
5. Print average

Calculate the complexity of the algorithm. Choose the most efficient


algorithm, explain your choice.

Answer:

Algorithm 1 is most efficient because the complexity of algorithm 1 is 11 and


complexity of algorithm 2 is 17 and in algorithm 1 we used only one for loop
and in algorithm2 we used 2 for loops using more for loops will decrease the
efficiency

3. Given two integers x and y, the following recursive definition determines the
greatest common divisor (gcd) of x and y, gcd (x, y):

x if y = 0
gcd(x, y) = gcd(y, x % y) if y ≠ 0
Write the program to test this function. What is the value of:

a. gcd(54, 24)
b. gcd(5, 38)

Please trace/illustrate your answers.

Answer:

You might also like