function(list) // index of a list starts at zero //
len = list.length() // finds the length of list //
answer = 0
for (i = 0, i <= len − 1, i = i + 1)
answer = answer + list[i]
answer = answer / len
print(answer)
function left parenthesis list right parenthesis slash slash index of a list starts at zero slash slash
len equals list.length left parenthesis right parenthesis slash slash finds the length of list slash slash
answer equals 0
for left parenthesis i equals 0, i is less than or equal to len minus 1, i equals i plus 1 right parenthesis
answer equals answer plus list left bracket i right bracket
answer eqals answer slash len
print left parenthesis answer right parenthesis
B. There are 3 elements in the list, so len = 3len equals 3 and the for loop will iterate when i = 0, 1, 2i equals 0, 1, 2. At each iteration, the list element is added to the value of answer, which is initially assigned the value of 0. When the loop is complete, the quotient of answer and len is computed. The trace table for the loop is shown below.
|
i |
answer+ list[i]answer plus list left bracket i right bracket |
answer |
Initial |
0 |
0 |
0 |
Loop 1 |
0 |
0+30 plus 3 |
3 |
Loop 2 |
1 |
3+43 plus 4 |
7 |
Loop 3 |
2 |
7+57 plus 5 |
12 |
When the loop terminates, answer = 12answer equals 12 and len = 3len equals 3, therefore 12 / 3 = 412 slash 3 equals 4