Bird
Raised Fist0
DSA Cprogramming

Array Traversal Patterns in DSA C

Choose your learning style10 modes available
Mental Model
Go through each item in the list one by one to see or use it.
Analogy: Like reading a book from the first page to the last page, looking at every word in order.
[0] -> [1] -> [2] -> [3] -> [4] -> null
 ↑
Dry Run Walkthrough
Input: array: [10, 20, 30, 40, 50]
Goal: Visit each element in the array from start to end and print it
Step 1: Start at index 0, read value 10
[0:10↑] -> [1:20] -> [2:30] -> [3:40] -> [4:50] -> null
Why: We begin at the first element to start traversal
Step 2: Move to index 1, read value 20
[0:10] -> [1:20↑] -> [2:30] -> [3:40] -> [4:50] -> null
Why: Move to next element to continue traversal
Step 3: Move to index 2, read value 30
[0:10] -> [1:20] -> [2:30↑] -> [3:40] -> [4:50] -> null
Why: Keep moving forward to visit all elements
Step 4: Move to index 3, read value 40
[0:10] -> [1:20] -> [2:30] -> [3:40↑] -> [4:50] -> null
Why: Continue until last element
Step 5: Move to index 4, read value 50
[0:10] -> [1:20] -> [2:30] -> [3:40] -> [4:50↑] -> null
Why: Last element visited to complete traversal
Result:
[0:10] -> [1:20] -> [2:30] -> [3:40] -> [4:50] -> null
Printed values: 10 20 30 40 50
Annotated Code
DSA C
#include <stdio.h>

void traverseArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        // print current element
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int size = sizeof(arr) / sizeof(arr[0]);
    traverseArray(arr, size);
    return 0;
}
for (int i = 0; i < size; i++) {
loop through each index from start to end
printf("%d ", arr[i]);
print the current element at index i
OutputSuccess
10 20 30 40 50
Complexity Analysis
Time: O(n) because we visit each of the n elements once
Space: O(1) because we use only a fixed amount of extra space
vs Alternative: Compared to random access, traversal is simple and direct with no extra cost
Edge Cases
empty array
loop does not run, nothing is printed
DSA C
for (int i = 0; i < size; i++) {
array with one element
prints the single element correctly
DSA C
for (int i = 0; i < size; i++) {
When to Use This Pattern
When you need to look at or use every item in a list one by one, use array traversal to visit each element in order.
Common Mistakes
Mistake: Using <= instead of < in the loop condition causing out-of-bounds access
Fix: Change loop condition to i < size to stay within array limits
Mistake: Starting loop index at 1 instead of 0, skipping first element
Fix: Start loop index at 0 to include the first element
Summary
It visits each element in an array one by one.
Use it when you want to process or read all items in order.
Remember to start at index 0 and stop before the array size.

Practice

(1/5)
1. What does the following code snippet do?
for (int i = 0; i < n; i++) { printf("%d ", arr[i]); }
easy
A. Prints only the last element of the array
B. Prints all elements of the array from end to start
C. Prints only the first element of the array
D. Prints all elements of the array from start to end

Solution

  1. Step 1: Understand the loop range

    The loop starts at 0 and runs while i is less than n, incrementing i by 1 each time.
  2. Step 2: Analyze the print statement inside the loop

    Each iteration prints the element at index i, so it prints elements from arr[0] to arr[n-1].
  3. Final Answer:

    Prints all elements of the array from start to end -> Option D
  4. Quick Check:

    Loop from 0 to n-1 prints all elements [OK]
Hint: Loop from 0 to n-1 prints array forward [OK]
Common Mistakes:
  • Confusing loop direction and thinking it prints backward
  • Assuming it prints only one element
  • Misreading the loop condition
2. Which of the following is the correct syntax to traverse an array arr of size n backwards in C?
easy
A. for (int i = 0; i < n; i++) { printf("%d", arr[i]); }
B. for (int i = n; i > 0; i++) { printf("%d", arr[i]); }
C. for (int i = n-1; i >= 0; i--) { printf("%d", arr[i]); }
D. for (int i = 1; i <= n; i++) { printf("%d", arr[i]); }

Solution

  1. Step 1: Identify correct loop start and end for backward traversal

    Backward traversal starts from index n-1 down to 0.
  2. Step 2: Check loop syntax correctness

    for (int i = n-1; i >= 0; i--) { printf("%d", arr[i]); } uses i = n-1; i >= 0; i-- which correctly traverses backward.
  3. Final Answer:

    for (int i = n-1; i >= 0; i--) { printf("%d", arr[i]); } -> Option C
  4. Quick Check:

    Backward loop uses i = n-1 to 0 with decrement [OK]
Hint: Backward loop starts at n-1 and decrements to 0 [OK]
Common Mistakes:
  • Starting loop at n instead of n-1
  • Using i++ instead of i-- for backward traversal
  • Accessing arr[i] with out-of-range indices
3. What is the output of the following code?
int arr[] = {5, 10, 15, 20};
int sum = 0;
for (int i = 1; i < 4; i++) {
    sum += arr[i];
}
printf("%d", sum);
medium
A. 50
B. 45
C. 30
D. Syntax Error

Solution

  1. Step 1: Identify loop indices and elements summed

    The loop runs i from 1 to 3, summing arr[1], arr[2], arr[3] which are 10, 15, and 20.
  2. Step 2: Calculate sum of selected elements

    sum = 10 + 15 + 20 = 45
  3. Final Answer:

    45 -> Option B
  4. Quick Check:

    Sum of arr[1..3] = 45 [OK]
Hint: Sum elements from index 1 to 3 inclusive [OK]
Common Mistakes:
  • Starting sum from arr[0] mistakenly
  • Adding wrong indices or missing last element
  • Confusing loop range boundaries
4. Find the error in the following code that attempts to print all elements of an array arr of size n:
for (int i = 0; i <= n; i++) {
    printf("%d ", arr[i]);
}
medium
A. Loop condition should be i < n, not i <= n
B. Array indexing should start from 1, not 0
C. printf statement syntax is incorrect
D. No error, code is correct

Solution

  1. Step 1: Analyze loop condition and array indexing

    Loop runs from i = 0 to i = n inclusive, accessing arr[n] which is out of bounds.
  2. Step 2: Correct loop condition to avoid out-of-bounds access

    Change condition to i < n so indices go from 0 to n-1, valid for array of size n.
  3. Final Answer:

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

    Array indices valid from 0 to n-1 [OK]
Hint: Use i < n to avoid out-of-bounds errors [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Starting index at 1 instead of 0
  • Ignoring array size limits
5. Given an integer array arr of size n, which code snippet correctly prints every second element starting from the first element?
hard
A. for (int i = 0; i < n; i += 2) { printf("%d ", arr[i]); }
B. for (int i = 1; i < n; i += 2) { printf("%d ", arr[i]); }
C. for (int i = 0; i <= n; i += 2) { printf("%d ", arr[i]); }
D. for (int i = n-1; i >= 0; i -= 2) { printf("%d ", arr[i]); }

Solution

  1. Step 1: Understand requirement to print every second element starting at first

    Start at index 0 and increment index by 2 each time to skip every other element.
  2. Step 2: Check loop correctness and bounds

    for (int i = 0; i < n; i += 2) { printf("%d ", arr[i]); } uses i = 0; i < n; i += 2 which correctly prints arr[0], arr[2], arr[4], etc.
  3. Final Answer:

    for (int i = 0; i < n; i += 2) { printf("%d ", arr[i]); } -> Option A
  4. Quick Check:

    Increment by 2 starting at 0 prints every second element [OK]
Hint: Increment index by 2 starting at 0 for every second element [OK]
Common Mistakes:
  • Starting at index 1 instead of 0
  • Using <= n causing out-of-bounds
  • Looping backward when forward is needed
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