Doubt with algorithm and vector

Asked

Viewed 76 times

0

help me solve the following algorithm. In this case, it is a vector of 20 positions. My doubt is what values would be stored in vector A?

for (i=1, A[0]=1; i< A.lenght; i++)
    A[i] = A[i-1]*2;

Correct answer:

a - Type 3i to i elements
b - Type 2i elements for i
c - Type I2 elements for i
d - Type ii elements for i

Could you explain to me what the correct answer is?

  • 1

    B and C are not the same answer?

  • this is a question that fell in a past contest.

  • The second and third are the same, and the two are the right ones. There’s something wrong there

  • But B and C are the same.

  • Unless the C is Elementos do tipo i² para i, which is something completely different from what it is now.

1 answer

0

Well, it’s easy, let’s go in pieces.

Dismembering the for i = 1, A[0] = 1. This instruction initializes i as 0 and the first position of A as 1.

After that is only one is normal. The condition, i < A.lenght which is "run this while i is less than the length of the array A. And the i++ which is the increment of i in 1 each loop.

The code of loop (A[i] = A[i-1]*2) is as follows.

The position i of array will receive the value of the previous position of the array multiplied by 2. That is, answers B and C are correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.