Bird
Raised Fist0
C++programming~10 mins

Array initialization in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array initialization
Declare array variable
↓
Allocate memory for array
↓
Assign initial values
↓
Array ready to use
This flow shows how an array is declared, memory allocated, values assigned, and then ready for use.
Execution Sample
C++
#include <iostream>
using namespace std;

int main() {
  int arr[3] = {10, 20, 30};
  for(int i=0; i<3; i++) {
    cout << arr[i] << " ";
  }
  return 0;
}
This code creates an array of 3 integers, initializes it with values, and prints each value.
Execution Table
StepActionArray StateIndex iOutput
1Declare array arr[3][?, ?, ?]--
2Initialize arr[0] = 10[10, ?, ?]--
3Initialize arr[1] = 20[10, 20, ?]--
4Initialize arr[2] = 30[10, 20, 30]--
5Start loop i=0[10, 20, 30]010
6Loop i=1[10, 20, 30]120
7Loop i=2[10, 20, 30]230
8Loop i=3 condition false[10, 20, 30]3-
💡 Loop ends when i reaches 3, condition i<3 is false
Variable Tracker
VariableStartAfter Step 5After Step 6After Step 7Final
arr[?, ?, ?][10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30]
i-0123
Key Moments - 3 Insights
Why does the array show [?, ?, ?] before initialization?
Before assigning values, the array memory is allocated but contains garbage or unknown values, shown as '?'. Initialization fills these with known values (see steps 2-4).
Why does the loop stop when i equals 3?
The loop condition is i < 3. When i becomes 3, the condition is false, so the loop stops (see step 8). This prevents accessing outside the array bounds.
Why do we print arr[i] inside the loop?
Printing arr[i] inside the loop outputs each element one by one as i changes from 0 to 2 (steps 5-7). This shows the array contents in order.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of arr after step 3?
A[10, ?, ?]
B[10, 20, ?]
C[?, ?, ?]
D[10, 20, 30]
💡 Hint
Check the 'Array State' column at step 3 in the execution table.
At which step does the loop condition become false?
AStep 8
BStep 7
CStep 5
DStep 4
💡 Hint
Look for the step where 'Loop i=3 condition false' appears in the execution table.
If we change the array size to 4 but keep the loop condition i<3, how many elements will be printed?
A4 elements
B0 elements
C3 elements
DDepends on initialization
💡 Hint
The loop runs while i<3, so it prints 3 elements regardless of array size (see variable i in variable_tracker).
Concept Snapshot
Array initialization in C++:
int arr[size] = {val1, val2, ...};
- Declares array with fixed size
- Assigns initial values in order
- Uninitialized elements (if any) hold garbage
- Use loops to access elements safely
- Loop condition must prevent out-of-bounds access
Full Transcript
This visual trace shows how an array is declared and initialized in C++. First, memory is allocated for the array with unknown values. Then each element is assigned a value in order. A loop runs from 0 to size-1 to print each element. The loop stops when the index reaches the array size, preventing out-of-bounds access. Variables arr and i change as shown in the tables. Key moments clarify common confusions about initialization and loop stopping conditions.

Practice

(1/5)
1. Which of the following correctly initializes an integer array of size 5 with all elements set to zero in C++?
easy
A. int arr[5] = {};
B. int arr[5] = {1, 2, 3, 4, 5};
C. int arr[5] = 0;
D. int arr[5];

Solution

  1. Step 1: Understand array initialization with empty braces

    Using empty braces {} initializes all elements to zero by default in C++.
  2. Step 2: Compare options

    int arr[5] = {}; uses {} which sets all elements to zero. int arr[5] = 0; is invalid syntax for array initialization. int arr[5] = {1, 2, 3, 4, 5}; initializes with specific values. int arr[5]; leaves elements uninitialized.
  3. Final Answer:

    int arr[5] = {}; -> Option A
  4. Quick Check:

    Empty braces {} zero-initialize arrays [OK]
Hint: Use empty braces {} to zero-initialize arrays quickly [OK]
Common Mistakes:
  • Thinking int arr[5] = 0; initializes the array to zero
  • Leaving array uninitialized expecting zeros
  • Confusing initialization with assignment
2. Which of the following is the correct syntax to initialize an array of 3 doubles with values 1.1, 2.2, and 3.3 in C++?
easy
A. double arr[3] = (1.1, 2.2, 3.3);
B. double arr = {1.1, 2.2, 3.3};
C. double arr[3] = {1.1, 2.2, 3.3};
D. double arr[3] = [1.1, 2.2, 3.3];

Solution

  1. Step 1: Check array declaration syntax

    Arrays require specifying size and initialization with braces {} containing values.
  2. Step 2: Validate each option

    double arr[3] = {1.1, 2.2, 3.3}; correctly declares a double array of size 3 with values in braces. double arr = {1.1, 2.2, 3.3}; misses size. double arr[3] = (1.1, 2.2, 3.3); uses parentheses which is invalid. double arr[3] = [1.1, 2.2, 3.3]; uses square brackets which is invalid in C++.
  3. Final Answer:

    double arr[3] = {1.1, 2.2, 3.3}; -> Option C
  4. Quick Check:

    Use braces {} with size for array initialization [OK]
Hint: Always use braces {} and specify size when initializing arrays [OK]
Common Mistakes:
  • Omitting array size in declaration
  • Using parentheses or square brackets for initialization
  • Assigning values without braces
3. What is the output of the following C++ code?
int arr[4] = {5, 10};
for(int i = 0; i < 4; i++) {
    std::cout << arr[i] << " ";
}
medium
A. 5 10 5 10
B. Compilation error
C. 5 10 garbage garbage
D. 5 10 0 0

Solution

  1. Step 1: Understand partial initialization of arrays

    When fewer elements are provided than the array size, remaining elements are zero-initialized.
  2. Step 2: Analyze the output loop

    arr[0] = 5, arr[1] = 10, arr[2] = 0, arr[3] = 0. Loop prints all four elements separated by spaces.
  3. Final Answer:

    5 10 0 0 -> Option D
  4. Quick Check:

    Partial init fills rest with zeros [OK]
Hint: Partial array init fills missing elements with zero [OK]
Common Mistakes:
  • Assuming uninitialized elements have garbage values
  • Expecting compilation error for partial init
  • Confusing output spacing
4. Identify the error in the following C++ code snippet:
int arr[3];
arr = {1, 2, 3};
medium
A. Arrays cannot be assigned values after declaration using braces
B. Array size is missing
C. Braces should be replaced with parentheses
D. No error, code is correct

Solution

  1. Step 1: Understand array assignment rules in C++

    Arrays cannot be assigned new values after declaration using brace initialization syntax.
  2. Step 2: Identify correct way to assign values

    Values must be assigned element-wise or initialized at declaration. Direct assignment with braces causes error.
  3. Final Answer:

    Arrays cannot be assigned values after declaration using braces -> Option A
  4. Quick Check:

    Arrays only initialized at declaration with braces [OK]
Hint: You can only initialize arrays at declaration, not assign with braces later [OK]
Common Mistakes:
  • Trying to assign arrays with brace syntax after declaration
  • Confusing arrays with std::vector assignment
  • Using parentheses instead of braces
5. You want to initialize a 2D array of size 2x3 with all elements set to 7 in C++. Which code snippet correctly achieves this?
hard
A. int arr[2][3] = {7};
B. int arr[2][3]; for(int i=0; i<2; i++) for(int j=0; j<3; j++) arr[i][j] = 7;
C. int arr[2][3] = {{7}, {7}};
D. int arr[2][3] = {{7,7,7}};

Solution

  1. Step 1: Analyze initialization options

    int arr[2][3] = {{7,7,7}}; sets first row to 7,7,7 and second row to 0,0,0. int arr[2][3] = {7}; sets only first element to 7, rest zero. int arr[2][3] = {{7}, {7}}; sets first element of each row to 7, rest zero. int arr[2][3]; for(int i=0; i<2; i++) for(int j=0; j<3; j++) arr[i][j] = 7; uses loops to assign 7 to every element.
  2. Step 2: Identify the best approach for uniform value

    int arr[2][3]; for(int i=0; i<2; i++) for(int j=0; j<3; j++) arr[i][j] = 7; is the most scalable and clear way to set all elements to 7 regardless of array size.
  3. Final Answer:

    int arr[2][3]; for(int i=0; i<2; i++) for(int j=0; j<3; j++) arr[i][j] = 7; -> Option B
  4. Quick Check:

    Use loops to fill multi-dimensional arrays uniformly [OK]
Hint: Use nested loops to fill all elements with the same value [OK]
Common Mistakes:
  • Assuming partial brace init sets all elements
  • Using single brace sets only first element
  • Not using loops for uniform initialization
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard