2.1Array Operations
Creating an array
Every array operation starts from the same place: an array with a fixed number of cells, and a number that says how many of them are filled. In this lesson we create an array of 8 cells and write values into the first three. The insert and delete operations in the next lessons use this pair.
- How to declare an array with a capacity constant
- How to write values into cells by index
- Why the length of the filled part is kept in a separate
sizevariable
1Idea
The number of cells in an array is fixed when it is declared and does not change later. Instead of scattering that number through the code, we put it in a constant: CAPACITY. Next to the array sits a size variable. size is the number of filled cells and starts at 0. The caller declares the two like this:
int arr[CAPACITY]; // 8 cells are reservedint size = 0; // no cell is filled yetcreateArray(arr, &size);In Java you write int[] arr = new int[CAPACITY], in Python arr = [None] * CAPACITY. Java fills the cells with 0, Python with None. In C, an array declared inside a function holds random values. So we never tell which cells are valid by looking at their contents, only by looking at size: the cells from 0 to size - 1 are filled.
createArray writes 12, 7 and 3 into the first three cells and sets size to 3. In C, the address of size is passed (int* size) so the function can change it. In Java and Python the function returns the new size: size = createArray(arr). Every function in the next lessons follows this pattern.
The array parameter is not copied. The function receives the start address of the array, so the values it writes into the cells show up in the caller's array.
2Step by step
The drawing fills an empty array of 8 cells with 12, 7 and 3. The numbers under the cells are the indexes.
- The array is declared: 8 cells are reserved side by side in memory.
sizeis 0. In C the cells may hold random values; the drawing shows them empty. arr[0] = 12: the first cell has index 0.arr[1] = 7: 7 goes into the second cell.arr[2] = 3: 3 goes into the third cell. The other 5 cells are reserved but unused.sizebecomes 3. The capacity is 8 and the size is 3: the filled cells are 0, 1 and 2.
#define CAPACITY 8 void createArray(int arr[], int* size) { arr[0] = 12; // first cell, index 0 arr[1] = 7; arr[2] = 3; *size = 3; // length of the filled part}static final int CAPACITY = 8; int createArray(int[] arr) { arr[0] = 12; // first cell, index 0 arr[1] = 7; arr[2] = 3; return 3; // length of the filled part}CAPACITY = 8 def create_array(arr): arr[0] = 12 # first cell, index 0 arr[1] = 7 arr[2] = 3 return 3 # length of the filled partThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
A fixed number of cells is written. Writing k given values takes k steps; writing one cell is O(1).
The array takes as much room as its capacity, and that room is reserved up front even while the cells are empty.
A linked list reserves a separate node for each element (creating a linked list). An array reserves all its room at once, and unused cells take up space too.
4Common mistake
A common mistake is confusing the filled part of the array with its capacity:
for (int i = 0; i < CAPACITY; i++) printf("%d ", arr[i]); // empty cells are printed tooarr has 8 cells but only 3 are filled. This loop also prints the random values in the other 5 cells. The filled part is always bounded by size: i < size.
5Try it
After int arr[8]; int size = 0;, what are the capacity and the size of the array?
The answer is B. 8 cells are reserved, so the capacity is 8. But no value has been written yet, so the size, the number of filled cells, is 0.
Show the answer
The answer is B. 8 cells are reserved, so the capacity is 8. But no value has been written yet, so the size, the number of filled cells, is 0.
After writing 12, 7 and 3 in that order, at which index is 3?
The answer is B. Indexes start at 0: 12 is in cell 0, 7 in cell 1 and 3 in cell 2. The last element is always at index size - 1.
Show the answer
The answer is B. Indexes start at 0: 12 is in cell 0, 7 in cell 1 and 3 in cell 2. The last element is always at index size - 1.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.