0
0
C++programming~10 mins

Common array operations in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common array operations
Start
Initialize array
Access elements by index
Modify elements
Traverse array with loop
Search for element
Insert or update elements
End
This flow shows how we start with an array, access and change elements, loop through them, search, and update values.
Execution Sample
C++
#include <iostream>
using namespace std;

int main() {
  int arr[5] = {10, 20, 30, 40, 50};
  arr[2] = 35;
  for(int i = 0; i < 5; i++) {
    cout << arr[i] << " ";
  }
  return 0;
}
This code creates an array, changes one element, then prints all elements.
Execution Table
StepOperationIndex/VariableValue/ActionArray StateOutput
1Initialize array-{10, 20, 30, 40, 50}{10, 20, 30, 40, 50}
2Modify elementarr[2]35{10, 20, 35, 40, 50}
3Loop starti=0-{10, 20, 35, 40, 50}
4Print elementarr[0]10{10, 20, 35, 40, 50}10
5Increment ii=1-{10, 20, 35, 40, 50}10
6Print elementarr[1]20{10, 20, 35, 40, 50}10 20
7Increment ii=2-{10, 20, 35, 40, 50}10 20
8Print elementarr[2]35{10, 20, 35, 40, 50}10 20 35
9Increment ii=3-{10, 20, 35, 40, 50}10 20 35
10Print elementarr[3]40{10, 20, 35, 40, 50}10 20 35 40
11Increment ii=4-{10, 20, 35, 40, 50}10 20 35 40
12Print elementarr[4]50{10, 20, 35, 40, 50}10 20 35 40 50
13Increment ii=5-{10, 20, 35, 40, 50}10 20 35 40 50
14Check loop conditioni < 5False{10, 20, 35, 40, 50}10 20 35 40 50
💡 Loop ends when i reaches 5, condition i < 5 is False
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 7After Step 9After Step 11Final
arr{10,20,30,40,50}{10,20,35,40,50}{10,20,35,40,50}{10,20,35,40,50}{10,20,35,40,50}{10,20,35,40,50}{10,20,35,40,50}
i--12345
Key Moments - 3 Insights
Why does arr[2] change from 30 to 35?
Because at step 2 we assign arr[2] = 35, which updates the third element in the array as shown in the execution_table row 2.
Why does the loop stop after i reaches 5?
At step 14, the condition i < 5 becomes False, so the loop ends. This is shown clearly in the execution_table row 14.
Why do we start counting i from 0 in the loop?
Arrays in C++ start at index 0, so to access all elements from first to last, we loop i from 0 up to 4 (less than 5). This is shown in the loop steps 3 to 13.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of arr[2] after step 2?
A30
B20
C35
D40
💡 Hint
Check the 'Modify element' operation at step 2 in the execution_table.
At which step does the loop condition become false?
AStep 14
BStep 13
CStep 12
DStep 11
💡 Hint
Look at the 'Check loop condition' row in the execution_table.
If we changed the loop to i < 3, how many elements would be printed?
A2
B3
C5
D4
💡 Hint
Refer to the variable_tracker for i values and loop condition in execution_table.
Concept Snapshot
Common array operations in C++:
- Declare: int arr[5] = {10,20,30,40,50};
- Access: arr[index], index starts at 0
- Modify: arr[2] = 35;
- Loop: for(int i=0; i<5; i++) to traverse
- Arrays have fixed size
- Index out of bounds causes errors
Full Transcript
This visual trace shows common array operations in C++. We start by creating an array with 5 elements. Then we change the third element from 30 to 35. Next, we use a for loop to print all elements from index 0 to 4. The loop stops when the index reaches 5 because the condition i < 5 becomes false. Variables like the array and loop index change step by step, which is tracked in the tables. This helps beginners see exactly how arrays work in C++.