...
Question 10826 – Algorithm-Paradigms
February 13, 2024
Question 10832 – Algorithm-Paradigms
February 13, 2024
Question 10826 – Algorithm-Paradigms
February 13, 2024
Question 10832 – Algorithm-Paradigms
February 13, 2024

Question 10389 – Programming

Consider the following C functions.
int tob (int b, int* arr) {

int i;

for (i=0; b>0; i++) {

if (b%2) arr [i] = 1;

else arr [i] = 0;

b = b/2;

}

return (i);

}

int pp (int a, int b) {

int arr [20];
int i, tot = 1, ex, len;
ex = a;
len = tob (b,arr);
for (i=0; i<len; i++)="" {=""
if (arr[i] == 1)
tot = tot * ex;
ex = ex * ex;
}
return (tot);
}

The value returned by pp(3,4) is ________.

Correct Answer: A

Question 13 Explanation: 
pp(3,4) ⇒
a=3,b=4
tot=1
ex=a=3
len=tob(b,arr) which is 3
[
tob(4,arr)==>
b=4
b%2 =4%2=0 Condition is false then arr[0]=0
=> b=b/2 =4/2 =2
b=2
b%2 =2%2=0 condition is false then arr[1]=0
=>b=b/2=2/2=1
b=1
then b%2=1%2 condition is true then arr[2]=1
=>b=b/2=1/2=0
The i value is 3 [length is 3]
]

i=0,
arr[0] ==1 condition is false
ex=3*3=9
i=1
arr[1]==1 condition is false
then
ex=9*9=81
i=2
then arr[2]==1 condition is true
tot=tot*ex=1*81=81
ex=81*81
Finally it returns tot value which 81.

A
81
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
error: Alert: Content selection is disabled!!