Академический Документы
Профессиональный Документы
Культура Документы
int x;
int *p;
int *q;
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.
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.
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.
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.
int *p;
int *q;
int i;
p = new int[5];
p[0] = 5;
cout<< “Array p: “;
for (i = 0; i < 5; i++)
cout<< p[i] <<” “;
cout<< endl;
q = new int[5];
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
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
Solution :
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-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
explanation :
Conclusion :
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)
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