Recall & Review
beginner
What does
array traversal mean in programming?Array traversal means going through each element of an array one by one to read or process them.
Click to reveal answer
beginner
How do you write a basic
for loop to traverse an array in C++?You use a
for loop with an index starting at 0 up to the array size minus one, accessing each element by its index.Click to reveal answer
beginner
Why is it important to know the size of the array when traversing it?
Knowing the size prevents accessing elements outside the array, which can cause errors or crashes.
Click to reveal answer
beginner
What is the output of this code?<br>
int arr[] = {1, 2, 3};
for(int i = 0; i < 3; i++) {
std::cout << arr[i] << " ";
}The output is:
1 2 3 <br>It prints each element of the array separated by spaces.Click to reveal answer
intermediate
Can you traverse an array backwards? How?
Yes, by starting the loop index at the last element (size - 1) and decreasing it until 0.
Click to reveal answer
What is the starting index for traversing a C++ array?
✗ Incorrect
C++ arrays start at index 0, so traversal begins from 0.
Which loop is commonly used for array traversal in C++?
✗ Incorrect
The for loop is most common because it easily controls the index.
What happens if you access an array element outside its bounds?
✗ Incorrect
Accessing outside bounds leads to undefined behavior, often crashes or wrong data.
How do you know how many times to loop when traversing an array?
✗ Incorrect
Looping exactly the array size times ensures all elements are visited.
Which of these is a valid way to traverse an array backwards?
✗ Incorrect
Starting from the last index and decreasing visits elements in reverse order.
Explain how to traverse an array in C++ using a for loop.
Think about how you count from the first to the last element.
You got /4 concepts.
Why must you be careful about array size when traversing?
Imagine walking on a path that ends at a certain point.
You got /4 concepts.