0
0
C++programming~10 mins

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

Choose your learning style9 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.