retagged by
20,804 views
36 36 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 _______.

6 Answers

Best answer
30 30 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
3 3 votes
  1. The function tob(4, arr) sets the initial values of the first three indices of the array as 0, 0, and 1
  2. The function returns index 3 after setting these values.
  3. In each iteration of the for loop, whenever an array index has a value of 1, the result 3 * 3 (i.e., ex * ex) is multiplied by the variable tot.
  4. The final value of tot is returned, which computes the power of 3, specifically 3^4 = 81.
  5. In the GATE exam, don't rush to solve programming questions on paper immediately.
  6. Take 10-15 seconds to observe the program and understand what is happening before proceeding.
  7. After analyzing, solve the problem carefully and double-check your final answer to ensure its correctness.
  8. In this case, the program computes 3^4, so the correct answer is 81.
Answer:
Position:
Show:

Related questions

60 60 votes
4 answers 4 answers
24.9k
24.9k views
Arjun asked Feb 12, 2020
24,941 views
Consider a double hashing scheme in which the primary hash function is $h_1(k)= k \text{ mod } 23$, and the secondary hash function is $h_2(k)=1+(k \text{ mod } 19)$. Ass...
27 27 votes
7 answers 7 answers
18.4k
18.4k views
Arjun asked Feb 12, 2020
18,389 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 \m...
20 20 votes
6 answers 6 answers
12.8k
12.8k views
Arjun asked Feb 18, 2021
12,777 views
Consider the following $\text{ANSI C}$ function:int SimpleFunction(int Y[], int n, int x) { int total = Y[0], loopIndex; for (loopIndex=1; loopIndex<=n-1; loopIndex++) to...
63 63 votes
9 answers 9 answers
30.0k
30.0k views
Arjun asked Feb 12, 2020
30,033 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 ______.