1. Arrays and For Loops¶
1.1. Objectives¶
- Utilitize for loops to iterate through an array
1.2. Time Goal¶
- 20 minutes on this section
1.3. Key Terms¶
1.4. Exercises¶
Note
You can use a for
loop to loop through all the elements of an array. Just start the index at 0 and loop while the index is less than the length of the array.
Q-6: Q-5: The following method has the correct code to subtract amt from all the values in the array values (a field of the current object), but the code is mixed up. Drag the blocks from the left into the correct order on the right and indent them correctly. You will be told if any of the blocks are in the wrong order or not indented correctly.public void subAll(int amt)
{
---
for (int i = 0;
i < values.length;
i++)
{
---
values[i] = values[i] - amt;
---
} // end for loop
---
} // end method
Note
You don’t have to loop through an array from the front to the back. You can loop by starting at the back of the array and move toward the front during each time through the loop. This can be handy when you are looping through a sorted array and want to find the index of the last number that is less than some given number.
- -1
- The method will only return -1 if no value in the array is less than the passed value.
- -15
- The method returns the index of the first item in the array that is less than the value, not the value.
- 1
- Since the method loops from the back towards the front -15 is the last value in the array that is less than -13 and it is at index 1.
- You will get an out of bounds error.
- No, the method correctly starts the index at values.length - 1 and continues as long as i is greater than or equal to 0.
Q-7: Given the following code segment what will be returned when you execute: getIndexLastSmaller(-13);
private int[ ] values = {-20, -15, 2, 8, 16, 33};
public int getIndexLastSmaller(int compare)
{
for (int i = values.length - 1; i >=0; i--)
{
if (values[i] < compare) return i;
}
return -1; // to show none found
}
- -1
- The method will only return -1 if no value in the array is less than the passed value.
- 1
- Check the starting index. Is it correct?
- 2
- Check the starting index. Is it correct?
- You will get an out of bounds error.
- You can not start the index at the length of the array. You must start at the length of the array minus one. This is a common mistake.
Q-8: Given the following code segment what will be returned when you execute: getIndexLastSmaller(7);
private int[ ] values = {-20, -15, 2, 8, 16, 33};
public int getIndexLastSmaller(int compare)
{
for (int i = values.length; i >=0; i--)
{
if (values[i] < compare) return i;
}
return -1; // to show none found
}
Note
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. You can even start in the middle and loop through the rest of the array.
- {-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.
Q-9: 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 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?
Q-10: 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;
}
}
Mixed up programs
Q-12: Q-11: 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
Q-14: Q-13: 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
return total / 3;
---
} // end method
- 17.5
- This would be true if the loop stopped at
arr.length - 1
. - 30.0
- This would be true if the loop started at 1 instead of 0.
- 130
- 32
- 32.5
- This sums all the values in the array and then returns the sum divided by the number of items in the array. This is the average.
Q-15: What is printed to console for this code block?
int[] arr = {10, 30, 30, 60};
double output = 0;
for (int i = 0; i < arr.length; i++)
{
output = output + arr[i];
}
return output / arr.length;
}
- {-20, -10, 2, 8, 16, 60}
- This would true if it looped through the whole array. Does it?
- {-20, -10, 2, 4, 8, 30}
- This would be true if it looped from the beginning to the middle. Does it?
- {-10, -5, 1, 8, 16, 60}
- It loops from the middle to the end doubling each value. Since there are 6 elements it will start at index 3.
- {-10, -5, 1, 4, 8, 30}
- This would be true if array elements didn't change, but they do.
Q-16: Given the following values of a
, what will the values of a
be after you execute the following
code?
int[ ] a = {-10, -5, 1, 4, 8, 30};
for (int i = a.length / 2; i < a.length; i++)
{
a[i] = a[i] * 2;
}
- whenever the first element in
a
is equal toval
- It is the last value in
a
that controls the final state oftemp
, as the loop is progressing through the array from 0 to the end. - Whenever
a
contains any element which equalsval
- Because
temp
is reset every time through the loop, only the last element controls whether the final value is true or false. - Whenever the last element in
a
is equal toval
- Because each time through the loop
temp
is reset, it will only be returned as true if the last value ina
is equal toval
. - Whenever more than 1 element in
a
is equal toval
- Because
temp
is reset every time through the loop, only the last element controls whether the final value is true or false, so it is possible for just the last value to be equal toval
. - Whenever exactly 1 element in
a
is equal toval
- Because
temp
is reset every time through the loop, only the last element controls whether the final value is true or false, so it is possible for several elements to be equal toval
.
Q-17: Consider the following code segment. Which of the following statements best describes the condition when it returns true?
boolean temp = false;
for (int i = 0; i < a.length; i++) {
temp = (a[i] == val);
}
return temp;