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

For these exercises, provide only the code needed to answer the question(s) 3) T

ID: 3578856 • Letter: F

Question

For these exercises, provide only the code needed to answer the question(s)

3) The following code snippet attempts to find the average value in an array called “numbers” that contains 10 integers, but there is at least one error with it. Describe in your own words what the logic of the code snippet is actually doing and why it is wrong. Then, describe how you would fix it. Do not show the code fix and only describe what the code changes should be int n = 1; while (n < 10) { sum = sum + numbers[n]; } System.out.println(sum / numbers.length);

Explanation / Answer

Answer:

Array locations always start from 0 index to length-1.

in our case, index represts n variable. n value starts from 1. It should start from 0.

another issue is with this code is, using while loop, we are iterating each element of array numbers and calculating the sum of all values in array. But inside while loop, we are not increasing the index i mean n value always remains same so it will cause for infinite loop. The code should be like below

int n = 0;

while (n < 10) {

sum = sum + numbers[n];

n++;

}

System.out.println(sum /(double) numbers.length);