Bird
Raised Fist0
C++programming~10 mins

One-dimensional arrays in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - One-dimensional arrays
Declare array with size
↓
Initialize elements
↓
Access elements by index
↓
Modify elements by index
↓
Use elements in loops or expressions
↓
End of array usage
This flow shows how a one-dimensional array is declared, initialized, accessed, modified, and used in a program.
Execution Sample
C++
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
    arr[i] = arr[i] + 5;
}
This code creates an array of 5 integers, then adds 5 to each element.
Execution Table
Stepi (loop index)Condition (i < 5)ActionArray StateOutput
10Truearr[0] = 10 + 5[15, 20, 30, 40, 50]
21Truearr[1] = 20 + 5[15, 25, 30, 40, 50]
32Truearr[2] = 30 + 5[15, 25, 35, 40, 50]
43Truearr[3] = 40 + 5[15, 25, 35, 45, 50]
54Truearr[4] = 50 + 5[15, 25, 35, 45, 55]
65FalseLoop ends[15, 25, 35, 45, 55]
💡 Loop ends when i reaches 5 and condition i < 5 is false
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
iundefined012345
arr[10, 20, 30, 40, 50][15, 20, 30, 40, 50][15, 25, 30, 40, 50][15, 25, 35, 40, 50][15, 25, 35, 45, 50][15, 25, 35, 45, 55][15, 25, 35, 45, 55]
Key Moments - 3 Insights
Why does the loop stop when i equals 5 and not when i equals 4?
The loop condition is i < 5, so it runs while i is less than 5. When i becomes 5, the condition is false, so the loop stops. This is shown in execution_table row 6.
Why do we start accessing array elements from index 0?
In C++, arrays start at index 0, so the first element is arr[0]. This is why the loop starts with i = 0 and accesses arr[0] first, as seen in execution_table row 1.
What happens if we try to access arr[5] in this example?
Accessing arr[5] is out of bounds because the array size is 5 (indices 0 to 4). This causes undefined behavior and should be avoided. The loop stops before i reaches 5 to prevent this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of arr after step 3?
A[15, 20, 30, 40, 50]
B[15, 25, 35, 40, 50]
C[10, 20, 30, 40, 50]
D[15, 25, 30, 40, 50]
💡 Hint
Check the 'Array State' column at step 3 in the execution_table.
At which step does the loop condition become false?
AStep 5
BStep 4
CStep 6
DStep 3
💡 Hint
Look at the 'Condition (i < 5)' column in the execution_table to find when it becomes false.
If we change the loop condition to i <= 5, what will happen?
AThe loop will run one extra time and access arr[5], causing an error.
BThe loop will run the same number of times.
CThe loop will not run at all.
DThe loop will run fewer times.
💡 Hint
Consider array indices and the loop condition in the execution_table and variable_tracker.
Concept Snapshot
One-dimensional arrays in C++:
- Declared with fixed size: int arr[5];
- Elements accessed by index starting at 0
- Use loops to read or modify elements
- Accessing out-of-bounds index causes errors
- Commonly used for storing sequences of values
Full Transcript
This visual execution trace shows how one-dimensional arrays work in C++. We start by declaring an array of 5 integers with initial values. Then, a loop runs from index 0 to 4, adding 5 to each element. The execution table tracks the loop index, condition, actions, and array state after each step. The variable tracker shows how the loop index and array elements change over time. Key moments clarify why the loop stops at i=5, why indexing starts at 0, and the dangers of out-of-bounds access. The quiz tests understanding of array state at specific steps, loop termination, and effects of changing the loop condition. The snapshot summarizes the main points about one-dimensional arrays in C++.

Practice

(1/5)
1. What is the starting index of a one-dimensional array in C++?
easy
A. -1
B. 1
C. 0
D. Depends on the array size

Solution

  1. Step 1: Understand array indexing in C++

    Arrays in C++ always start indexing from 0, meaning the first element is at index 0.
  2. Step 2: Confirm the index range

    Indexes go from 0 up to size-1, so the first index is always 0 regardless of array size.
  3. Final Answer:

    0 -> Option C
  4. Quick Check:

    Array indexing starts at 0 [OK]
Hint: Remember: C++ arrays always start at zero index [OK]
Common Mistakes:
  • Thinking arrays start at 1
  • Assuming negative indexes are valid
  • Confusing with other languages
2. Which of the following is the correct way to declare an integer array of size 5 in C++?
easy
A. array<int> arr(5);
B. int arr(5);
C. int arr = new int[5];
D. int arr[5];

Solution

  1. Step 1: Recall C++ array declaration syntax

    In C++, a fixed-size array is declared using square brackets with size: int arr[5];
  2. Step 2: Check other options for correctness

    int arr(5); uses parentheses which is invalid for arrays. array<int> arr(5); uses incorrect syntax (not standard array). int arr = new int[5]; uses dynamic allocation syntax but is incorrect without pointer.
  3. Final Answer:

    int arr[5]; -> Option D
  4. Quick Check:

    Fixed-size arrays use square brackets [OK]
Hint: Use square brackets [] to declare fixed-size arrays [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Confusing with vector syntax
  • Incorrect dynamic allocation syntax
3. What will be the output of the following code?
int arr[4] = {10, 20, 30, 40};
std::cout << arr[2];
medium
A. 30
B. 20
C. 40
D. Compilation error

Solution

  1. Step 1: Identify the array elements and indexes

    The array arr has elements: arr[0]=10, arr[1]=20, arr[2]=30, arr[3]=40.
  2. Step 2: Access the element at index 2

    arr[2] is 30, so std::cout will print 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    arr[2] = 30 [OK]
Hint: Remember array index starts at 0, so arr[2] is third element [OK]
Common Mistakes:
  • Confusing index 2 with 1 or 3
  • Expecting output 20 or 40
  • Thinking code causes error
4. Identify the error in the following code snippet:
int arr[3] = {1, 2, 3};
for(int i = 0; i <= 3; i++) {
    std::cout << arr[i] << " ";
}
medium
A. Missing semicolon after array declaration
B. Loop runs one time too many, causing out-of-bounds access
C. Syntax error in for loop declaration
D. Array size is incorrect, should be 4

Solution

  1. Step 1: Analyze the loop range and array size

    The array has size 3 with valid indexes 0,1,2. The loop runs from i=0 to i=3 inclusive, which means 4 iterations.
  2. Step 2: Identify out-of-bounds access

    When i=3, arr[3] is accessed, which is outside the array bounds and causes undefined behavior.
  3. Final Answer:

    Loop runs one time too many, causing out-of-bounds access -> Option B
  4. Quick Check:

    Loop index must be less than array size [OK]
Hint: Loop index must be less than array size, not less or equal [OK]
Common Mistakes:
  • Using <= instead of <
  • Assuming array size is 4
  • Ignoring out-of-bounds errors
5. Given an integer array arr of size n, which code snippet correctly calculates the sum of all elements?
hard
A. int sum = 0; for(int i = 0; i < n; i++) sum += arr[i];
B. int sum = 0; for(int i = 1; i < n; i++) sum += arr[i];
C. int sum = 0; for(int i = 0; i <= n; i++) sum += arr[i];
D. int sum = arr[0]; for(int i = 1; i < n; i++) sum = arr[i];

Solution

  1. Step 1: Understand correct loop bounds for array traversal

    Array indexes go from 0 to n-1, so loop must run i from 0 to less than n.
  2. Step 2: Check each option for correctness

    int sum = 0; for(int i = 1; i < n; i++) sum += arr[i]; starts from 1, missing arr[0]. int sum = 0; for(int i = 0; i <= n; i++) sum += arr[i]; uses <= n, causing out-of-bounds access. int sum = 0; for(int i = 0; i < n; i++) sum += arr[i]; correctly sums all elements from 0 to n-1. int sum = arr[0]; for(int i = 1; i < n; i++) sum = arr[i]; overwrites sum instead of adding.
  3. Final Answer:

    int sum = 0; for(int i = 0; i < n; i++) sum += arr[i]; -> Option A
  4. Quick Check:

    Loop from 0 to n-1 sums all elements [OK]
Hint: Loop from 0 to less than n to cover all elements [OK]
Common Mistakes:
  • Starting loop at 1
  • Using <= n causing errors
  • Overwriting sum instead of adding
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