0
0
Cprogramming~10 mins

Array traversal in C - Interactive Code Practice

Choose your learning style9 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.