Arrays help us store many values in one place. Common operations let us add, find, or change these values easily.
Common array operations
/* Define an array and its size */ #define SIZE 100 int array[SIZE]; int size = 0; // current number of elements /* Add element at the end */ if (size < SIZE) { array[size] = new_value; size++; } /* Find element by value */ int find_index = -1; for (int i = 0; i < size; i++) { if (array[i] == value_to_find) { find_index = i; break; } } /* Update element at index */ if (index >= 0 && index < size) { array[index] = new_value; } /* Print all elements */ for (int i = 0; i < size; i++) { printf("%d ", array[i]); } printf("\n");
Arrays have fixed size in C, so you must check you don't add beyond capacity.
Indexing starts at 0, so first element is array[0].
int array[5]; int size = 0; // Add elements array[size++] = 10; array[size++] = 20; // Find 20 int find_index = -1; for (int i = 0; i < size; i++) { if (array[i] == 20) { find_index = i; break; } } // Update element at found index if (find_index != -1) { array[find_index] = 25; } // Print all for (int i = 0; i < size; i++) { printf("%d ", array[i]); } printf("\n");
int array[3]; int size = 0; // Try to add 4 elements (one more than capacity) for (int i = 1; i <= 4; i++) { if (size < 3) { array[size++] = i * 10; } else { printf("Cannot add %d, array full\n", i * 10); } }
int array[0]; int size = 0; // Trying to find in empty array int find_index = -1; for (int i = 0; i < size; i++) { if (array[i] == 10) { find_index = i; break; } } printf("Find index: %d\n", find_index);
This program adds three numbers to an array, prints them, finds the number 10, updates it to 20, and prints the array again.
#include <stdio.h> int main() { int array[5]; int size = 0; // Add elements array[size++] = 5; array[size++] = 10; array[size++] = 15; printf("Array before update: "); for (int i = 0; i < size; i++) { printf("%d ", array[i]); } printf("\n"); // Find element 10 int find_index = -1; for (int i = 0; i < size; i++) { if (array[i] == 10) { find_index = i; break; } } // Update element if found if (find_index != -1) { array[find_index] = 20; } printf("Array after update: "); for (int i = 0; i < size; i++) { printf("%d ", array[i]); } printf("\n"); return 0; }
Adding elements is O(1) if you add at the end and have space.
Finding elements is O(n) because you may check each element.
Updating is O(1) once you know the index.
Common mistake: forgetting to check array size before adding causes bugs.
Use arrays when you know max size and want fast access by index.
Arrays store multiple values in order.
Common operations: add, find, update, print.
Always check array size to avoid errors.
Practice
int arr[5] = {1, 2, 3, 4, 5};Solution
Step 1: Understand the declaration
The code declares an integer array named arr with size 5.Step 2: Check the initialization
The array is initialized with values 1, 2, 3, 4, and 5 in order.Final Answer:
Creates an array of 5 integers with values 1 to 5 -> Option AQuick Check:
Array declaration with initialization = Creates an array of 5 integers with values 1 to 5 [OK]
- Confusing array with pointer declaration
- Thinking it's a single variable, not an array
- Assuming it's a 2D array
arr?Solution
Step 1: Recall array indexing in C
Arrays in C use zero-based indexing, so the first element is arr[0].Step 2: Identify the third element index
The third element is at index 2, so arr[2] accesses it.Final Answer:
arr[2] -> Option BQuick Check:
Third element index = 2 [OK]
- Using parentheses instead of brackets
- Using 1-based indexing (arr[3])
- Using curly braces for access
int arr[4] = {10, 20, 30, 40};
int sum = 0;
for(int i = 0; i < 4; i++) {
sum += arr[i];
}
printf("%d", sum);Solution
Step 1: Sum all elements in the array
The loop adds arr[0]=10, arr[1]=20, arr[2]=30, arr[3]=40 to sum.Step 2: Calculate the total sum
10 + 20 + 30 + 40 = 100.Final Answer:
100 -> Option AQuick Check:
Sum of array elements = 100 [OK]
- Off-by-one error in loop causing wrong sum
- Confusing sum with last element value
- Syntax errors from missing semicolons
int arr[3] = {1, 2, 3};
for(int i = 0; i <= 3; i++) {
printf("%d ", arr[i]);
}Solution
Step 1: Analyze loop condition
The loop runs from i=0 to i=3 inclusive, so it accesses arr[3].Step 2: Check array bounds
Array arr has size 3, valid indices are 0,1,2. Accessing arr[3] causes out-of-bounds error.Final Answer:
Loop condition should be i < 3, not i <= 3 -> Option CQuick Check:
Array index must be less than size [OK]
- Using <= instead of < in loop condition
- Assuming array size auto-expands
- Misunderstanding printf format specifier
arr of size n. Which code snippet correctly does this?Solution
Step 1: Initialize max with first element
Set max = arr[0] to start comparison from first element.Step 2: Loop from second element to end
Loop i from 1 to n-1, compare arr[i] with max, update max if arr[i] is larger.Step 3: Check other options for errors
int max = 0; for(int i = 0; i <= n; i++) { if(arr[i] > max) max = arr[i]; } uses max=0 which fails if all numbers are negative; loop condition i <= n causes out-of-bounds; int max = arr[n]; for(int i = 0; i < n; i++) { if(arr[i] > max) max = arr[i]; } initializes max with arr[n] which is invalid index; int max = arr[0]; for(int i = 1; i < n; i++) { if(arr[i] < max) max = arr[i]; } finds minimum instead of maximum.Final Answer:
int max = arr[0]; for(int i = 1; i < n; i++) { if(arr[i] > max) max = arr[i]; } -> Option DQuick Check:
Initialize max with first element and compare correctly [OK]
- Using wrong loop bounds causing out-of-bounds access
- Initializing max with 0 ignoring negative values
- Confusing max with min logic
Ready to prove it?
Pass the test and this pattern moves unknown
