0
0
Cprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - One-dimensional arrays
Declare array
Allocate memory for elements
Assign values to each index
Access elements by index
Use elements in operations
End of array usage
This flow shows how a one-dimensional array is declared, filled with values, accessed by index, and used in operations.
Execution Sample
C
int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
int x = arr[1];
This code declares an array of 5 integers, assigns values to the first three elements, and reads the second element into variable x.
Execution Table
StepActionArray StateVariable ValuesOutput
1Declare array arr of size 5[_, _, _, _, _]arr declared
2Assign arr[0] = 10[10, _, _, _, _]arr[0]=10
3Assign arr[1] = 20[10, 20, _, _, _]arr[1]=20
4Assign arr[2] = 30[10, 20, 30, _, _]arr[2]=30
5Read arr[1] into x[10, 20, 30, _, _]x=20
6End of execution[10, 20, 30, _, _]x=20Program ends
💡 All assignments done and value read; program ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
arr[0]_1010101010
arr[1]__20202020
arr[2]___303030
arr[3]______
arr[4]______
x____2020
Key Moments - 3 Insights
Why does arr[3] still show '_' after all assignments?
Because we only assigned values to arr[0], arr[1], and arr[2]. The other elements remain uninitialized, shown as '_'. See execution_table rows 2-4.
Why can we access arr[1] after assigning it?
Once arr[1] is assigned a value (step 3), we can read it anytime (step 5). The variable x gets the value 20 from arr[1].
What happens if we try to access arr[5]?
Accessing arr[5] is out of bounds since array size is 5 (indices 0 to 4). This causes undefined behavior in C, so we avoid it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of arr[2]?
A20
B_
C30
D10
💡 Hint
Check the 'Array State' column at step 4 in execution_table.
At which step is the variable x assigned a value?
AStep 5
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Variable Values' column for x in execution_table.
If we assign arr[4] = 50 at step 6, what would be the new array state?
A[10, 20, 30, 50, _]
B[10, 20, 30, _, 50]
C[50, 20, 30, _, _]
D[10, 20, 30, _, _]
💡 Hint
Remember array indices start at 0; arr[4] is the fifth element.
Concept Snapshot
One-dimensional arrays in C:
- Declared as type name[size], e.g. int arr[5];
- Elements accessed by zero-based index: arr[0], arr[1], ...
- Assign values with arr[index] = value;
- Uninitialized elements hold garbage values (shown as '_');
- Accessing out-of-bounds index causes undefined behavior.
Full Transcript
This lesson shows how to use one-dimensional arrays in C. First, we declare an array with a fixed size. Then, we assign values to specific positions using their index, starting at zero. We can read values from the array by using the same index. Unassigned elements remain uninitialized and should not be used. Accessing outside the array size is unsafe. The execution table traces each step, showing how the array and variables change. The variable tracker highlights the values of each element and variable after each step. Key moments clarify common confusions like uninitialized elements and safe access. The quiz tests understanding of array states and variable assignments. The snapshot summarizes the main points for quick review.