Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Remembering the programmer\'s eternal quest to discover patterns and exploit the

ID: 3727335 • Letter: R

Question

Remembering the programmer's eternal quest to discover patterns and exploit them, we will attempt to create a loop that produces the same actions as these 8 statements:While our first impulse is to create a loop that iterates 8 times, we notice that these 8 lines alternate between use of synth1 and synth2. So, our first pattern to exploit is that there are in fact 4 pairs of calls in this sequence:

Thus, we should be able to construct a loop that iterates 4 times, each time calling fitMedia twice - once for synth1 and again for synth2. The loop will have this form, with appropriate formulas used in place of a, b, c, and d:

What values of a, b, c, and d should be used so that this loop will create the same sequence of statements as the original code? Some of these should be expressions involving the value of i, for instance i+3 or 3*i-7 (these examples are not likely to be the actual answers).

Explanation / Answer

Values:

a = 2*i+1 b=2*i+2

c= 2*i+2 d=2*i+3

frst iteration (i=0):

a=1; b=2; c=2; d=3;

Two statements are:

Second iteration (i=1):

a=3; b=4; c=4; d=5;

Two statements are:

third iteration (i=2):

a=5; b=6; c=6; d=7;

Two statements are:

Fourth iteration (i=3):

a=7; b=8; c=8; d=9;

Two statements are:

Fifth iteration:(i=4)

i<4 condition fails. So execution comes outside of for loop.

Here we got the original 8 lines of code. 2 in each iteration.