Doubt with algorithm and vector

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 - Elements of type 3i for i
  b - Elements of type 2i for i
  c - Elements of type i2 for i
  d - Elements of type ii for i

Could you explain the correct answer to me?

    
asked by anonymous 30.03.2017 / 16:43

1 answer

0

Well, it's easy, let's break it down.

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

After that it's just normal. The condition, with% that is "execute this while i < A.lenght is less than the length of the array." And the i which is the% in 1 every loop .

The loop ( i++ ) code is as follows.

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

30.03.2017 / 17:06