Array initialization in C - Time & Space Complexity
When we create and fill an array, the time it takes depends on how many items we add.
We want to know how the work grows as the array size grows.
Analyze the time complexity of the following code snippet.
int arr[1000];
for (int i = 0; i < 1000; i++) {
arr[i] = 0;
}
This code sets each element of an array with 1000 items to zero.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Setting each array element to zero inside the loop.
- How many times: Exactly 1000 times, once for each element.
As the array size grows, the number of steps grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: Doubling the array size doubles the work needed.
Time Complexity: O(n)
This means the time to initialize grows directly with the number of elements.
[X] Wrong: "Initializing an array is instant no matter the size."
[OK] Correct: Each element must be set one by one, so bigger arrays take more time.
Understanding how array initialization scales helps you explain performance in real code.
"What if we initialize the array with a fixed value using a library function? How would the time complexity change?"
Practice
Solution
Step 1: Understand array declaration syntax
In C, arrays are declared with a type, name, and size in square brackets, e.g., int arr[3].Step 2: Check initialization format
Initialization uses curly braces with comma-separated values, e.g., {1, 2, 3}.Final Answer:
int arr[3] = {1, 2, 3}; -> Option BQuick Check:
Correct array declaration and initialization [OK]
- Missing square brackets for size
- Using parentheses instead of braces
- Assigning array without braces
Solution
Step 1: Check array size and initializer count
Array size is 3 but initializer has 4 values, which is invalid in C.Step 2: Verify other options
int nums[4] = {10, 20, 30, 40}; matches size and values, C infers size from initializer, B zero-initializes first element and rest are zero.Final Answer:
int nums[3] = {1, 2, 3, 4}; -> Option DQuick Check:
Initializer count > array size causes syntax error [OK]
- Providing more initial values than array size
- Omitting size but giving initializer with extra values
- Using wrong braces
int arr[5] = {1, 2};
printf("%d %d %d %d %d", arr[0], arr[1], arr[2], arr[3], arr[4]);Solution
Step 1: Understand partial initialization
When fewer values are given, remaining elements are set to zero automatically.Step 2: Check printed values
arr[0] = 1, arr[1] = 2, arr[2], arr[3], arr[4] = 0 each.Final Answer:
1 2 0 0 0 -> Option AQuick Check:
Partial init fills rest with zero [OK]
- Assuming uninitialized elements have garbage values
- Expecting compilation error for partial init
- Confusing array size with initializer count
int data[3];
data = {1, 2, 3};Solution
Step 1: Understand array assignment rules
In C, arrays cannot be assigned values after declaration using = and braces.Step 2: Check other options
Semicolon is present, size can be specified, braces are correct for initialization but not assignment.Final Answer:
Arrays cannot be assigned values after declaration like this. -> Option CQuick Check:
Array assignment after declaration is invalid [OK]
- Trying to assign array with braces after declaration
- Confusing array assignment with pointer assignment
- Using parentheses instead of braces
Solution
Step 1: Analyze partial initialization
int arr[6] = {10, 20}; partially initializes first two elements; rest are zero by default, so it works.Step 2: Compare with explicit zero initialization and assignment
int arr[6] = {0}; arr[0] = 10; arr[1] = 20; explicitly zeroes all elements then assigns first two, ensuring clarity and no garbage.Step 3: Evaluate best practice
int arr[6] = {0}; arr[0] = 10; arr[1] = 20; is best for clarity and safety; int arr[6] = {10, 20}; works but less explicit; int arr[6] = {10, 20, 0, 0, 0, 0}; is verbose; int arr[6]; arr[0] = 10; arr[1] = 20; leaves uninitialized elements.Final Answer:
int arr[6] = {0}; arr[0] = 10; arr[1] = 20; -> Option AQuick Check:
Explicit zero init then assign first elements [OK]
- Assuming partial init always safe without explicit zero
- Not initializing array before assignment
- Overwriting zeros with uninitialized values
Ready to prove it?
Pass the test and this pattern moves unknown
