Bird
Raised Fist0
C++programming~10 mins

Array traversal in C++ - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print all elements of the array using a for loop.

C++
#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    for (int i = 0; i < n; i++) {
        std::cout << arr[[1]] << " ";
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Ai
Bn
C0
Darr
Attempts:
3 left
💡 Hint
Common Mistakes
Using the array name instead of the index.
Using the size variable as the index.
2fill in blank
medium

Complete the code to calculate the sum of all elements in the array.

C++
#include <iostream>

int main() {
    int arr[] = {10, 20, 30, 40};
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[[1]];
    }
    std::cout << sum << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
An
Bsum
Ci
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the size variable instead of the element.
Using the sum variable as an index.
3fill in blank
hard

Fix the error in the loop condition to correctly traverse the array.

C++
#include <iostream>

int main() {
    int arr[] = {5, 10, 15};
    int n = sizeof(arr) / sizeof(arr[0]);
    for (int i = 0; i [1] n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes out-of-bounds access.
Using > or >= causes the loop to never run.
4fill in blank
hard

Fill both blanks to create a loop that prints elements at even indices only.

C++
#include <iostream>

int main() {
    int arr[] = {2, 4, 6, 8, 10, 12};
    int n = sizeof(arr) / sizeof(arr[0]);
    for (int i = [1]; i < n; i [2]) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A0
Bi += 2
Ci++
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 instead of 0.
Using i++ increments by 1, not 2.
5fill in blank
hard

Fill all three blanks to create a loop that prints elements in reverse order.

C++
#include <iostream>

int main() {
    int arr[] = {3, 6, 9, 12, 15};
    int n = sizeof(arr) / sizeof(arr[0]);
    for (int i = [1]; i [2] 0; i [3]) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
An - 1
B>
C--i
D++i
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 instead of the last index.
Using ++i instead of --i to go backwards.

Practice

(1/5)
1. What does array traversal mean in C++ programming?
easy
A. Visiting each element of the array one by one
B. Creating a new array with the same elements
C. Sorting the array elements in order
D. Deleting elements from the array

Solution

  1. Step 1: Understand the term 'array traversal'

    Array traversal means going through each element in the array in sequence.
  2. Step 2: Match the definition to the options

    Visiting each element of the array one by one correctly describes visiting each element one by one.
  3. Final Answer:

    Visiting each element of the array one by one -> Option A
  4. Quick Check:

    Array traversal = visiting elements in order [OK]
Hint: Traversal means visiting all elements one by one [OK]
Common Mistakes:
  • Confusing traversal with sorting
  • Thinking traversal means deleting elements
  • Mixing traversal with copying arrays
2. Which of the following is the correct syntax to traverse an array arr of size n in C++?
easy
A. for(int i = 1; i <= n; i++) { /* access arr[i] */ }
B. for(int i = n; i > 0; i--) { /* access arr[i] */ }
C. for(int i = 0; i <= n; i++) { /* access arr[i] */ }
D. for(int i = 0; i < n; i++) { /* access arr[i] */ }

Solution

  1. Step 1: Identify valid index range for array traversal

    Array indices in C++ start at 0 and go up to n-1, so loop should run from 0 to less than n.
  2. Step 2: Check each option's loop condition

    for(int i = 0; i < n; i++) { /* access arr[i] */ } uses i from 0 to i < n, which is correct. Others either start at 1 or go out of bounds.
  3. Final Answer:

    for(int i = 0; i < n; i++) { /* access arr[i] */ } -> Option D
  4. Quick Check:

    Loop from 0 to n-1 = correct traversal [OK]
Hint: Array indices start at 0, loop till less than size [OK]
Common Mistakes:
  • Starting loop at 1 instead of 0
  • Using <= n causing out-of-bounds access
  • Decrementing loop without correct index
3. What is the output of the following C++ code?
int arr[] = {2, 4, 6};
for(int i = 0; i < 3; i++) {
    std::cout << arr[i] << " ";
}
medium
A. 0 2 4 6
B. 2 4 6 0
C. 2 4 6
D. 6 4 2

Solution

  1. Step 1: Understand the loop and array elements

    The array has elements 2, 4, 6 at indices 0, 1, 2. The loop runs from i=0 to i=2.
  2. Step 2: Print elements in order

    Each iteration prints arr[i] followed by a space: 2, then 4, then 6.
  3. Final Answer:

    2 4 6 -> Option C
  4. Quick Check:

    Print elements in order = 2 4 6 [OK]
Hint: Loop prints elements in order from index 0 to 2 [OK]
Common Mistakes:
  • Expecting extra zero at end
  • Printing elements in reverse order
  • Confusing array indices with values
4. Identify the error in this C++ code snippet for array traversal:
int arr[4] = {1, 2, 3, 4};
for(int i = 0; i <= 4; i++) {
    std::cout << arr[i] << " ";
}
medium
A. Loop condition should be i < 4, not i <= 4
B. Array size should be 5 instead of 4
C. Loop variable i should start from 1
D. No error, code is correct

Solution

  1. Step 1: Check loop condition against array size

    The array has size 4, valid indices are 0 to 3. Loop runs while i <= 4, which includes i=4 (out of bounds).
  2. Step 2: Correct the loop condition

    Change condition to i < 4 to avoid accessing arr[4] which is invalid.
  3. Final Answer:

    Loop condition should be i < 4, not i <= 4 -> Option A
  4. Quick Check:

    Loop must stay within 0 to size-1 [OK]
Hint: Loop index must be less than array size, not less or equal [OK]
Common Mistakes:
  • Using <= instead of < causing out-of-bounds
  • Starting loop at 1 instead of 0
  • Increasing array size unnecessarily
5. Given an integer array arr of size n, which code snippet correctly calculates the sum of all elements using array traversal?
hard
A. int sum = arr[0]; for(int i = 1; i <= n; i++) { sum += arr[i]; }
B. int sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; }
C. int sum = 0; for(int i = 1; i < n; i++) { sum += arr[i]; }
D. int sum = 0; for(int i = 0; i <= n; i++) { sum += arr[i]; }

Solution

  1. Step 1: Initialize sum and loop correctly

    Sum should start at 0. Loop must run from i=0 to i < n to include all elements.
  2. Step 2: Check each option for correctness

    int sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } initializes sum to 0 and loops correctly. Others either start sum wrong or loop out of bounds.
  3. Final Answer:

    int sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } -> Option B
  4. Quick Check:

    Sum all elements from index 0 to n-1 [OK]
Hint: Start sum at 0 and loop from 0 to less than n [OK]
Common Mistakes:
  • Starting sum with arr[0] and looping incorrectly
  • Looping with i <= n causing out-of-bounds
  • Skipping first element by starting at i=1
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard