8.5. Looping through Part of an Array¶

You don’t have to loop through all of the elements of an array. You can loop through just some of the elements of an array using a for loop. The following code doubles the first five elements in an array. Notice that it uses a complex conditional (`&&`) on line 12 to make sure that the loop doesn’t go beyond the bounds of the array.

You can even start in the middle and loop through the rest of the array. Does this work for arrays that have an even number of elements? Does it work for arrays that have an odd number of elements? Modify the main code below to test with both arrays with an even number of items and an odd number.

7-5-1: Given the following values of a and the method doubleLast what will the values of a be after you execute: doubleLast()?

```private int[ ] a = {-20, -15, 2, 8, 16, 33};

public void doubleLast()
{

for (int i = a.length / 2; i < a.length; i++)
{
a[i] = a[i] * 2;
}
}
```
• {-40, -30, 4, 16, 32, 66}
• This would true if it looped through the whole array. Does it?
• {-40, -30, 4, 8, 16, 32}
• This would be true if it looped from the beginning to the middle. Does it?
• {-20, -15, 2, 16, 32, 66}
• It loops from the middle to the end doubling each value. Since there are 6 elements it will start at index 3.
• {-20, -15, 2, 8, 16, 33}
• This would be true if array elements didn't change, but they do.

7-5-2: Given the following values of a and the method mystery what will the values of a be after you execute: mystery()?

```private int[ ] a = {-20, -15, 2, 8, 16, 33};

public void mystery()
{

for (int i = 0; i < a.length/2; i+=2)
{
a[i] = a[i] * 2;
}
}
```
• {-40, -30, 4, 16, 32, 66}
• This would true if it looped through the whole array and doubled each. Does it?
• {-40, -30, 4, 8, 16, 33}
• This would be true if it looped from the beginning to the middle and doubled each. Does it?
• {-20, -15, 2, 16, 32, 66}
• This would be true if it looped from the middle to the end and doubled each. Does it?
• {-40, -15, 4, 8, 16, 33}
• This loops from the beginning to the middle and doubles every other element (i+=2 is the same as i = i + 2).
• {-40, -15, 4, 8, 32, 33}
• This would be true if it looped through the whole array and doubled every other element. Does it?

Mixed up programs

```        7-5-3: The following program has the correct code to reverse the elements in an array, a,  but the code is mixed up.  Drag the blocks from the left into the correct order on the right. You will be told if any of the blocks are in the wrong order or are indented incorrectly.public void reverse()
{
---
int temp = 0;
int half = a.length / 2;
int max = a.length - 1;
for (int i = 0;
i < half;
i++)
{
---
temp = a[i];
---
a[i] = a[max - i];
---
a[max - i] = temp;
---
} // end for
---
} // end method
```
```        7-5-4: The following program has the correct code to return the average of the first 3 items in the array a, but the code is mixed up.  Drag the blocks from the left into the correct order on the right. You will be told if any of the blocks are in the wrong order or are indented incorrectly.public double avg3()
{
---
double total = 0;
for (int i = 0;
i < a.length && i < 3;
i++)
{
---
total = total + a[i];
---
} // end for