in Others recategorized by
611 views
0 votes
0 votes

​​​​​Consider the function computes $(X)$ whose pseudocode is given below:

computes $(X)$
$S[1] \leftarrow 1$
for $i \leftarrow 2$ to length $(X)$
$S[i] \leftarrow 1$
if $X[i-1] \leq X[i]$
$S[i] \leftarrow S[i]+S[i-1]$
end if
end for
return $S$

Which ONE of the following values is returned by the function computes $(X)$ for $X=[6,3,5,4,10]$ ?

  1. $[1,1,2,3,4]$
  2. $[1,1,2,3,3]$
  3. $[1,1,2,1,2]$
  4. $[1,1,2,1,5]$

in Others recategorized by
by
611 views

2 Answers

1 vote
1 vote

Answer is C.

$X=[6,3,5,4,10]$

Assuming that for the given pseudocode, indices start from 1.

So, initially $S[1]=1$

(1) For $i=2$:

$S[2]=1$

Since $X[1] \nleq X[2]$

So, statement in the if condition will not be executed.

(2) For $i=3$:

$S[3]=1$

Since $X[2] \leq X[3]$

So, $S[3]=1+1=2$ 

(3) For $i=4$:

$S[4]=1$

Since $X[3] \nleq X[4]$

So, statement in the if condition will not be executed.

(4) For $i=5$:

$S[5]=1$

Since $X[4] \leq X[5]$

So, $S[5]=1+1=2$ 

Python code:

indices start from zero for the list data structure.

  

0 votes
0 votes
C

Related questions