Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print all elements of the array using a for loop.
DSA C
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
for (int i = 0; i [1] n; i++) {
printf("%d ", arr[i]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes out-of-bounds access.
Using > or >= will not enter the loop.
✗ Incorrect
The loop should run while i is less than n to avoid going out of array bounds.
2fill in blank
mediumComplete the code to traverse the array backwards and print elements.
DSA C
int arr[] = {10, 20, 30, 40, 50};
int n = 5;
for (int i = n - 1; i [1] 0; i--) {
printf("%d ", arr[i]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > excludes the first element at index 0.
Using < or <= will not work for backward traversal.
✗ Incorrect
The loop should continue while i is greater than or equal to 0 to include the first element.
3fill in blank
hardFix the error in the code to correctly print every second element starting from the first.
DSA C
int arr[] = {5, 10, 15, 20, 25, 30};
int n = 6;
for (int i = 0; i [1] n; i += 2) {
printf("%d ", arr[i]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes out-of-bounds access.
Using > or >= will not enter the loop.
✗ Incorrect
The loop should run while i is less than n to avoid accessing out-of-bounds elements.
4fill in blank
hardFill both blanks to create a loop that prints elements at odd indices only.
DSA C
int arr[] = {2, 4, 6, 8, 10, 12};
int n = 6;
for (int i = [1]; i [2] n; i += 2) {
printf("%d ", arr[i]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 prints even indices.
Using <= causes out-of-bounds access.
✗ Incorrect
Start from index 1 (first odd index) and continue while i is less than n.
5fill in blank
hardFill all three blanks to create a loop that prints elements divisible by 3.
DSA C
int arr[] = {3, 5, 6, 7, 9, 12};
int n = 6;
for (int i = 0; i [1] n; i++) {
if (arr[i] [2] 3 == 0) {
printf("%d ", arr[[3]]);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes out-of-bounds access.
Using / instead of % for divisibility.
Printing arr[i+1] or wrong index.
✗ Incorrect
Loop while i < n, check remainder with %, and print arr[i].
