What are the contents of the array numbing after the followi

What are the contents of the array numbing after the following code segment has been executed? int [] nums = new int [8]; nums [0] = 0; int n = 1; while (n

Solution

Answer: 0 1 1 2 1 2 2 3

Initially all index position of the array num contains 0

while(n < num.length) //Loops from 1 to 8

{

for(k = n; k < 2 * n; k++) //Loops from 1

num[k] = num[k - n] + 1;

       n = k;

}

Initially num[0] = 0

num.length = 8

Initially n = 1 and k = 1

k will move up to 2 * n i.e., 2

num[1] = num[1]+1 è num[1] position contain 0 + 1 = 1

So num[1] = 1

K++ = 2, for loop is false, and n = k è n = 2

n < num.length is true

k = n è k = 2 and will loop up to 4 (k < 2 * n)

now k = 2 and n = 2

num[2] = num[0]+1 è num[0] position contain 0 + 1 = 1

so num[2] = 1

now k = 3 and n = 2

num[3] = num[1]+1 è num[1] position contain 1 + 1 = 2

so num[3] = 2

now k = 4 and n = 2 for loop is false and n = k è n = 4

n < num.length is true (n is 4)

k = n è k = 4 and will loop up to 8 (k < 2 * n)

now k = 4 and n = 4

num[4] = num[0]+1 è num[0] position contain 0 + 1 = 1

so num[4] = 1

now k = 5 and n = 4

num[5] = num[1]+1 è num[1] position contain 1 + 1 = 2

so num[5] = 2

now k = 6 and n = 4

num[6] = num[2]+1 è num[2] position contain 1 + 1 = 2

so num[6] = 2

now k = 7 and n = 4

num[7] = num[3]+1 è num[3] position contain 2 + 1 = 3

so num[7] = 3

now k = 8 and for loop is false

n = k and n is 8

while(n < num.length) is false

 What are the contents of the array numbing after the following code segment has been executed? int [] nums = new int [8]; nums [0] = 0; int n = 1; while (n Sol
 What are the contents of the array numbing after the following code segment has been executed? int [] nums = new int [8]; nums [0] = 0; int n = 1; while (n Sol

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site