Introduction to Arrays
Arrays are data structures that store multiple values under a single variable name, allowing efficient management of related data. Instead of creating separate variables for each value, an array groups these values in an ordered collection.
A one-dimensional (1D) array is like a list or sequence of elements arranged in a single row. Each element can be accessed using a single index representing its position.
A two-dimensional (2D) array extends this concept into a grid or table format, with rows and columns. Each element is accessed using two indices: one for the row and one for the column.
- 1D arrays store elements in a linear sequence.
- 2D arrays store elements in rows and columns, forming a matrix-like structure.
Declaring and Initializing One-Dimensional Arrays
To create a 1D array, you specify the type of elements it will hold and the number of elements it can store. For example, to declare an array of five integers:
In many programming languages, this might look like: int numbers[5];
Initialization assigns values to the array elements, either at declaration or later. For example: numbers = [2, 4, 6, 8, 10]; or assigning individually: numbers[0] = 2; numbers[1] = 4; etc. The index typically starts at 0, so numbers[0] refers to the first element.
- Declare with a fixed size to allocate memory.
- Initialize with values at declaration or by assignment.
- Indices usually start at 0, so the first element is at index 0.
Declaring and Initializing Two-Dimensional Arrays
A 2D array requires specifying the number of rows and columns. For example, a 3x4 array of integers can be declared as: int matrix[3][4];
This creates a table with 3 rows and 4 columns. Elements are accessed by two indices: matrix[row][column].
Initialization can be done row-wise, for example: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]; where matrix[0][0] is 1 and matrix[2][3] is 12.
- Specify rows and columns when declaring 2D arrays.
- Access elements using two indices: row and column.
- Initialize with nested lists or loops assigning values row by row.
Accessing and Modifying Array Elements
To retrieve or change a value in an array, use its index or indices. For a 1D array, access the element at position i with array[i]. For example, to get the third element: value = numbers[2];
For a 2D array, access the element at row r and column c with array[r][c]. For example, to access the element in the second row, fourth column: value = matrix[1][3];
Modifying elements follows the same indexing pattern: array[i] = newValue; or array[r][c] = newValue;
- Use indices to access or modify elements.
- Remember that indices start at 0 unless otherwise specified.
- For 2D arrays, provide both row and column indices.
Practical Uses and Examples
1D arrays are useful for storing lists such as student names, scores, or sensor readings.
2D arrays are ideal for representing tables, such as a timetable, a chessboard, or pixel data in images.
Example: Storing daily temperatures for a week (1D array): temperatures = [22, 24, 19, 23, 25, 20, 21]; Access the temperature on day 4 with temperatures[3]. Example: Storing a 3x3 tic-tac-toe board (2D array): board = [['X', 'O', 'X'], [' ', 'X', ' '], ['O', ' ', 'O']]; Access the centre cell with board[1][1].
- 1D arrays store sequential data like lists.
- 2D arrays represent grids or tables.
- Use indices to efficiently retrieve or update data.
At-a-glance summary
Comparison table
| Step | Detail | Example | Exam Use |
|---|---|---|---|
| Declare array | Specify type and size | int scores[10]; | Show correct syntax for array creation |
| Initialize array | Assign values at declaration or later | scores = [5,7,8,6,9]; | Demonstrate initialization methods |
| Access element | Use index to get value | value = scores[2]; | Explain zero-based indexing |
| Modify element | Assign new value at index | scores[2] = 10; | Show element update |
| 2D array declaration | Specify rows and columns | int matrix[3][4]; | Understand multi-dimensional arrays |
| Access 2D element | Use row and column indices | value = matrix[1][3]; | Apply nested indexing |
Active recall
Quick check
1. How do you access the first element in a 1D array named 'data'?
Use data[0] because array indices start at zero.
2. What indices would you use to access the element in the third row, second column of a 2D array 'grid'?
Use grid[2][1], since rows and columns are zero-indexed.
3. How do you change the value of the fifth element in a 1D array 'scores' to 100?
Assign scores[4] = 100; to update the fifth element.
4. What is the difference between a 1D and a 2D array?
A 1D array is a single list of elements; a 2D array is a table with rows and columns, accessed by two indices.
