Bird
Raised Fist0
Cprogramming~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.

C
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = 5;
    for (int i = 0; i < n; [1]) {
        printf("%d\n", arr[i]);
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Ai--
B++i
Ci++
Di+=2
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator causes the loop to never run or run incorrectly.
Skipping elements by increasing by 2 misses some array elements.
2fill in blank
medium

Complete the code to sum all elements in the array.

C
#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40};
    int n = 4;
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum [1] arr[i];
    }
    printf("Sum is %d\n", sum);
    return 0;
}
Drag options to blanks, or click blank then click option'
A+=
B-=
C*=
D/=
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication operators changes the sum incorrectly.
Using division operator causes runtime errors or wrong results.
3fill in blank
hard

Fix the error in the loop condition to avoid accessing out of array bounds.

C
#include <stdio.h>

int main() {
    int arr[] = {5, 10, 15};
    int n = 3;
    for (int i = 0; i [1] n; i++) {
        printf("%d\n", 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 the loop to access arr[n], which is out of bounds.
Using > or >= causes the loop to never run or run incorrectly.
4fill in blank
hard

Fill both blanks to create a loop that prints only even numbers from the array.

C
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6};
    int n = 6;
    for (int i = 0; i < n; i++) {
        if (arr[i] [1] 2 [2] 0) {
            printf("%d\n", 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 '!=' instead of '==' selects odd numbers instead of even.
Using '>' does not correctly check for even numbers.
5fill in blank
hard

Fill all three blanks to create a dictionary-like structure using arrays and print keys with values greater than 10.

C
#include <stdio.h>

int main() {
    char *keys[] = {"a", "b", "c", "d"};
    int values[] = {5, 15, 8, 20};
    int n = 4;
    for (int [1] = 0; [2] < n; [3]++) {
        if (values[i] > 10) {
            printf("%s: %d\n", keys[i], values[i]);
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Ai
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes compilation errors.
Using a variable not declared causes errors.

Practice

(1/5)
1. What does array traversal mean in C programming?
easy
A. Adding elements to the array
B. Deleting elements from the array
C. Visiting each element of the array one by one
D. Sorting the array elements

Solution

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

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

    Only Visiting each element of the array one by one describes visiting each element one by one, which is the correct meaning.
  3. Final Answer:

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

    Array traversal = visiting elements [OK]
Hint: Traversal means visiting all elements one by one [OK]
Common Mistakes:
  • Confusing traversal with adding or deleting elements
  • Thinking traversal means sorting
  • Assuming traversal skips elements
2. Which of the following is the correct syntax to traverse an array arr of size n in C?
easy
A. for(int i = 0; i < n; i++)
B. for(int i = 1; i <= n; i++)
C. for(int i = 1; i < n; i++)
D. for(int i = 0; i <= n; i++)

Solution

  1. Step 1: Understand array indexing in C

    Arrays start at index 0 and go up to n-1 for size n.
  2. Step 2: Check loop conditions

    The loop must start at 0 and run while i is less than n (not equal), so for(int i = 0; i < n; i++) is correct.
  3. Final Answer:

    for(int i = 0; i < n; i++) -> Option A
  4. Quick Check:

    Loop from 0 to n-1 = correct traversal [OK]
Hint: Loop from 0 to less than n for array traversal [OK]
Common Mistakes:
  • Using <= n causes out-of-bounds error
  • Starting loop at 1 misses first element
  • Using wrong loop condition
3. What will be the output of the following code?
int arr[] = {5, 10, 15};
for(int i = 0; i < 3; i++) {
    printf("%d ", arr[i]);
}
medium
A. 5 10 15 0
B. 15 10 5
C. 10 15 5
D. 5 10 15

Solution

  1. Step 1: Understand the array elements

    The array contains 5, 10, and 15 in order at indices 0, 1, and 2.
  2. Step 2: Trace the loop output

    The loop prints elements from index 0 to 2 in order, so output is "5 10 15 "
  3. Final Answer:

    5 10 15 -> Option D
  4. Quick Check:

    Print elements in order = 5 10 15 [OK]
Hint: Prints elements in order from index 0 to n-1 [OK]
Common Mistakes:
  • Reversing order of output
  • Printing extra zero
  • Starting loop at wrong index
4. Identify the error in the following code snippet for traversing an array arr of size 5:
for(int i = 0; i <= 5; i++) {
    printf("%d ", arr[i]);
}
medium
A. Loop condition should be i < 5 to avoid out-of-bounds access
B. Loop variable should start from 1 instead of 0
C. Array size should be 6 instead of 5
D. No error, code is correct

Solution

  1. Step 1: Check loop condition

    The loop runs while i <= 5, so i goes from 0 to 5 inclusive (6 iterations).
  2. Step 2: Analyze array indexing

    Array indices go from 0 to 4 for size 5. Accessing arr[5] is out-of-bounds and causes error.
  3. Final Answer:

    Loop condition should be i < 5 to avoid out-of-bounds access -> Option A
  4. Quick Check:

    Use i < size to avoid errors [OK]
Hint: Use i < size, not i <= size, to avoid out-of-bounds [OK]
Common Mistakes:
  • Using <= instead of <
  • Starting loop at 1
  • Ignoring array size limits
5. Given an integer array arr of size n, write a code snippet to find the sum of all elements using array traversal. Which of the following is correct?
hard
A. int sum = 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 = 0; i <= n; i++) { sum += arr[i]; }
D. int sum = 0; for(int i = n - 1; i >= 0; i--) { sum += arr[i]; }

Solution

  1. Step 1: Understand array indexing and loop range

    Array indices go from 0 to n-1, so loop must run i from 0 to less than n.
  2. Step 2: Check each option for correctness

    int sum = 0; for(int i = 0; i <= n; i++) { sum += arr[i]; } uses i <= n, which accesses arr[n] (out-of-bounds). int sum = 0; for(int i = 1; i < n; i++) { sum += arr[i]; } starts at 1, missing arr[0]. int sum = 0; for(int i = n - 1; i >= 0; i--) { sum += arr[i]; } correctly sums all elements in reverse order, but the question asks for traversal in forward order. int sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } correctly sums all elements from 0 to n-1 in forward order.
  3. Final Answer:

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

    Loop from 0 to n-1 sums all elements [OK]
Hint: Sum elements by looping i from 0 to less than n [OK]
Common Mistakes:
  • Using i <= n causing out-of-bounds
  • Starting loop at 1 missing first element
  • Looping backwards with wrong indices
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