Вы находитесь на странице: 1из 5

MTS 3023: DATA STRUCTURES

SEMESTER 2 SESI 2019/2020


ASSIGNMENT 1

Name : Muhamad Hilmi bin Husin


No. Matrik : D20191087001

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;
 This statement is valid because two pointers can be equated. 

b. *p = 56;
This statement is invalid because '&' should be used  before x and
then p=&x; *p = 56;

c. p = x;
This is an illegal conversion from int to int* because either the pointer p
can be pointed to the address of x or the pointer p can be assigned to
another pointer.

This statement is invalid.

d. *p = *q;
*p means the value to which the pointer p is pointing. Here, the value of
pointer p is assigned to the value of pointer q.

This statement is valid.

e. q = &x;
The pointer q is pointing to the address of x. Therefore, q will have the
address of x and *q will have the value of x.

This statement is valid

f. *p =q;
This is an illegal conversion from *int to int because *p can only have the
value of pointer q and not the address. Therefore, *p = *q will be valid and
*p = q be invalid.

This statement is invalid.

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;

Output:
Array p: 5 7 11 17 25
Array 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.

Solution :

we calculate the complexity of first algorithm

step 1 for i=0 to i=4 -> this will takes the complexity of 6 because it will true for 5
times and 6th time it will terminate so the complexity is 6

for the above loop complexity is 6

get the num[i] -> it will take the complexity of 1 because it is a normal statement i.e.
not a loop.

sum=sum+num[i] -> it will be the complexity of 1 because it is a normal statement

step-2 average=sum/5-> this is a normal statement complexity is 1

step-3 print sum-> normal statement complexity is 1

step-4 print average-> normal statement complexity is 1

so the total complexity will be 6+1+1+1+1+1=11

the complexity for the first algorithm is 11


algorithm2 :

step-1 for i=0 to i=4 -> this will takes the complexity of 6 because it will true for 5
times and 6th time it will terminate so the complexity is 6

get the num[i] -> it will take the complexity of 1 because it is a normal statement i.e.
not a loop.

step-2 for i=0 to i=4 -> this will takes the complexity of 6 because it will true for 5
times and 6th time it will terminate so the complexity is 6

sum=sum+num[i] -> it will be the complexity of 1 because it is a normal statement

step-3 average=sum/5-> this is a normal statement complexity is 1

step-4 print sum-> normal statement complexity is 1

step-5 print average-> normal statement complexity is 1

so total complexity is 6+1+6+1+1+1+1=17

explanation :

By comparing the algorithm 1 is efficient than algorithm 2 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

Conclusion :

algorithm 1 is efficient that algorithm 2


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.

a)

GCD(54, 24)

GCD(24, 6)

GCD(6, 0)

GCD(54, 24) is 6

b)
GCD(5, 38)
GCD(38, 5)
GCD(5, 3)
GCD(3, 2)
GCD(2, 1)
GCD(1, 0)
GCD(5, 38) is 1

Вам также может понравиться