Complete the code to print all elements of the array.
#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; }
The loop variable i must increase by 1 each time to traverse the array from start to end.
Complete the code to sum all elements in the array.
#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; }
The += operator adds the current array element to the sum.
Fix the error in the loop condition to avoid accessing out of array bounds.
#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; }
The loop should run while i is less than n to avoid accessing beyond the last element.
Fill both blanks to create a loop that prints only even numbers from the array.
#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; }
The modulus operator % checks the remainder when dividing by 2. If it equals 0, the number is even.
Fill all three blanks to create a dictionary-like structure using arrays and print keys with values greater than 10.
#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; }
The loop variable i is used consistently to index both arrays.