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

Assume that an array z, is declared as in: Dim z(n) As Integer and that z has be

ID: 3714924 • Letter: A

Question

Assume that an array z, is declared as in:
Dim z(n) As Integer

and that z has been filled with data in all its locations, and n is an odd number.

<-- array content is integer values


Write code that uses a for loop, to calculate the average of every other number in z starting from location 0 to location n inclusive.

Since n is an odd number the for loop can start at the first location and end at the last location of z.

Think of what the step should be so we process one element then skip the next one and move on to the one after that.

After the loop show the resulting average in a messagebox. Think about the number of elements we need to divide by is half of the elements between locations 0 and n.

This is my answer was told that the first line inside the for loop is wrong the line after the Next statement should divide by length/2. Help me fix my code.

Dim arrayZ() As Integer - {1,2,3,4,5,6,7,8,9}

Dim sum As Double

For Index = 0 To arrayZ.Length - 2

Dim Avg As Double = arrayZ(index) + arrayZ(index + 1) / 2

sum += arrayZ(index)

MessageBox.Show("Avg : " + Avg.ToString())

Next

Dim TotalAvgCalc As Double = sum / (arrayZ.Length - 1)

MessageBox.Show("Total Avg : " + TotalAvgCalc.ToString())

Explanation / Answer

Dim arrayZ() As Integer = {1,2,3,4,5,6,7,8,9}

Dim sum As Double=0 'Change 1

For Index = 0 To arrayZ.Length - 2

Dim Avg As Double = (arrayZ(index) + arrayZ(index + 1) ) / 2 ' Change 2

sum += Avg 'Change 3

MessageBox.Show("Avg : " + Avg.ToString())

Next

Dim TotalAvgCalc As Double = sum / (arrayZ.Length )

MessageBox.Show("Total Avg : " + TotalAvgCalc.ToString())

Change 1 --> sum value should be initialized

Change 2 --> paranthesis specification is missed in your program

Change 3 --> average should be summed up (mistake line in your program)