• recategorized by
172 views
0 0 votes

In the following pseudocode segment, A denotes an array and len(A) denotes the number of elements in that array. The array is indexed from $1$ to len(A). Write down the array A in each iteration of the for loop when the input array is $<20,3,9,12,70,6>$.
 

function weirdify(A):
for i from 1 to len(A):
if i % 2 == 0:
A[i] = A[i/2]*A[i]
else if i*i > len(A):
A[i] = A[i-2] + 4
else:
A[i] = A[i] - 1

1 Answer

0 0 votes
Array after 1st iteration: [19, 3, 9, 12, 70, 6]

Array after 2nd iteration: [19, 57, 9, 12, 70, 6]

Array after 3rd iteration: [19, 57, 23, 12, 70, 6]

Array after 4th iteration: [19, 57, 23, 684, 70, 6]

Array after 5th iteration: [19, 57, 23, 684, 27, 6]

Array after 6th iteration: [19, 57, 23, 684, 27, 138]
Position:
Show:

Related questions

0 0 votes
0 0 answers
151
151 views
Ay_Kay_Ay asked Dec 2, 2024
151 views
Let $\mathrm{A}, \mathrm{B}$ and C denote arrays of real numbers, where B has $\mathrm{n}-1$ entries and $\mathrm{A}, \mathrm{C}$ have n entries each. Consider the follow...
1 1 vote
1 1 answer
296
296 views
Ay_Kay_Ay asked Dec 2, 2024
296 views
Let $N=2^{150}$. How many bits are required to write $N$ in binary (base $2$)? How many digits are required to write $N$ in decimal (base 10)? You may assume that $\log _...
0 0 votes
0 0 answers
213
213 views
Ay_Kay_Ay asked Dec 2, 2024
213 views
There are $5$ friends $A, B, C, D, E$. Because of a heated argument not all of them are on speaking terms anymore. The array below describes who is talking to whom. A val...
1 1 vote
1 1 answer
237
237 views
Ay_Kay_Ay asked Dec 2, 2024
237 views
Two friends $A$ and $B$ are playing a coin flipping game with a fair coin, based on the following rules:When $A$ flips the coin - If it is heads then $A$ win...