in Algorithms edited by
6,123 views
8 votes
8 votes

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++)
        total=x*total +Y[loopIndex];
    return total;
}

Let $\textsf{Z}$ be an array of $10$ elements with $\textsf{Z}[i]=1$, for all $i$ such that $0 \leq i \leq 9$. The value returned by $\textsf{SimpleFunction(Z},10,2)$ is __________

in Algorithms edited by
by
6.1k views

4 Answers

13 votes
13 votes
Best answer
For index $i,$ we get $\text{total}_i = x*\text{total}_{i-1} + Y_i.$ We have $Y_i = 1, x = 2, \text{total}_0=1.$

So, $\text{total}_{n} = 2*\text{total}_{n-1} +1 $

$\qquad =2*(2*\text{total}_{n-2} + 1) + 1$

$\qquad =2^2*\text{total}_{n-2} + 2+ 1$

$\qquad \vdots$

$\qquad =2^2*\text{total}_{n-2} + 2+ 1$

$\qquad =2^n*\text{total}_{n-n} +(2^{n-1} + 2^{n-2} + \ldots+ 2+ 1)$

$\qquad =2^n*\text{total}_{0} +(2^{n}-1)$

$\qquad =2^{n+1}-1$

So, $\text{total}_{9} = 2^{10} – 1 = 1023.$
selected by
7 votes
7 votes
Answer is: 1023

for every i value total value will be $2^{i+1}-1$

1 comment

@sachin1410 , can u explain in detail.
0
0
1 vote
1 vote
loop index runs from 1 to 9.

pattern is  total = 2^(loopindex+1) -1

loopindex

1        total=3                   

2        total=7

3       total=15

4       total=31

5

6

7

8

9     total=2^(9+1)-1=1023
0 votes
0 votes

ans

Answer:

Related questions