edited by
300 views
0 votes
0 votes

Given the following definition of the function $foo$, what does $foo(1037,2)$ return? Note that $a//b$ denotes the quotient (integer part) of $a\div b$, for integers $a$ and $b$. For instance $7//3$ is $2$.

function foo(n,d)
{
    x:=0;
    while(n>=1)
    {
        x:=x+1;
        n:=n//d;
    }
    return(x);
}

 

edited by

1 Answer

0 votes
0 votes
$foo(1037,2)$: Current value of $n=1037$, $d=2$

inside the function $foo(1037,2)$ $x$ is a variable initialized with $0$

Next there is a while loop which is run till $(n\geq 1)$,loop get terminated when $n$ value is smaller then $1$.

Working of while loop is as follows:

$1^{st}$ iteration: $(1037\geq1)=T$,

$x$ is incremented by $1$ that is $x=1$.

Current value of $n=518$

$2^{nd}$ iteration: $(518 \geq1)=T$,

Current value of $x=2$,

Current value of $n=259$

$3^{rd}$ iteration: $(259 \geq1)=T$,

Current value of $x=3$,

Current value of $n=129$

$4^{th}$ iteration: $(129 \geq1)=T$,

Current value of $x=4$,

Current value of $n=64$

$5^{th}$ iteration: $(64 \geq1)=T$,

Current value of $x=5$,

Current value of $n=32$

$5^{th}$ iteration: $(64 \geq1)=T$,

Current value of $x=5$,

Current value of $n=32$

$6^{th}$ iteration: $(32 \geq1)=T$,

Current value of $x=6$,

Current value of $n=16$

$7^{th}$ iteration: $(16 \geq1)=T$,

Current value of $x=7$,

Current value of $n=8$

$8^{th}$ iteration: $(8 \geq1)=T$,

Current value of $x=8$,

Current value of $n=4$

$9^{th}$ iteration: $(4 \geq1)=T$,

Current value of $x=9$,

Current value of $n=2$

$10^{th}$ iteration: $(2 \geq1)=T$,

Current value of $x=10$,

Current value of $n=1$

$11^{th}$ iteration: $(1 \geq1)=T$,

Current value of $x=11$,

Current value of $n=0$

$12^{th}$ iteration: $(0 \geq1)=F$,

exit from while loop. So current value of $x=11$

So $x=11$ is correct answer.
edited by

Related questions

2 votes
2 votes
1 answer
2
soujanyareddy13 asked Jan 29, 2021
445 views
If $P$ is an invertible matrix and $A=PBP^{-1},$ then which of the following statements are necessarily true?$B=P^{-1}AP$$|A|=|B|$$A$ is invertible if and only if $B$ is ...
0 votes
0 votes
3 answers
3
soujanyareddy13 asked Jan 29, 2021
604 views
Let $A=\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}, B=\begin{bmatrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{bmatrix}, C=\begin{bmatrix} 4 & 5 & 6...
2 votes
2 votes
2 answers
4
soujanyareddy13 asked Jan 29, 2021
374 views
Let $x=\begin{bmatrix} 3& 1 & 2 \end{bmatrix}$. Which of the following statements are true?$x^Tx$ is a $3\times 3$ matrix$xx^T$ is a $3\times 3$ matrix$xx^T$ is a $1\time...