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,
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
| 0 | 1 | 2 | 3 | n | <-- index or location |
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())
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
