retagged by
11,274 views
18 votes
18 votes

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 _______.

retagged by

4 Answers

Best answer
18 votes
18 votes

Answer $: 81$

$pp(3,4)$

  • $a=3, b=4 , \text{tot} = 1;$

len = tob(4, array) will return 3 with array set as $001$ as array is updated only once when  b%2 != 0). The for loop actually iterates $3$ times for $b = 4, b = 2$ and $b=1,$ and only when $b=1,$ arr[i] is updated.

Now pp will run for loop 3 times:

  1. $\text{arr}[0] = 0.$ So, $\text{ex} = 3\ast 3 = 9$
  2. $\text{arr}[1] = 0.$ So, $\text{ex} = 9\ast 9 = 81$
  3. $\text{arr}[1] = 1.$ So, $\text{tot} = 1\ast 81 = 81$
edited by
Answer:

Related questions

8 votes
8 votes
4 answers
2
Arjun asked Feb 12, 2020
9,640 views
Consider a graph $G = (V,E)$, where $V = \{v_1,v_2, \dots ,v_{100}\}$, $E = \{(v_i,v_j) \mid 1\leq i < j \leq 100\}$, and weight of the edge $(v_i,v_j)$ is $\mid i – j ...
28 votes
28 votes
8 answers
3
Arjun asked Feb 12, 2020
16,306 views
The number of permutations of the characters in LILAC so that no character appears in its original position, if the two L’s are indistinguishable, is ______.