A 1D array is often referred to as a linear array because it stores data in a single row or sequence.
In pseudocode, a 1D array is declared by specifying the identifier, the range of indices, and the data type: DECLARE <identifier> : ARRAY[LB:UB] OF <DataType>.
To access or assign a value to a specific position, the index is placed in square brackets following the identifier, such as MyArray[3] <- 10.
A 2D array is best visualized as a table or grid consisting of rows and columns.
Declaration requires two sets of bounds: one for the rows and one for the columns, formatted as DECLARE <identifier> : ARRAY[RowLB:RowUB, ColLB:ColUB] OF <DataType>.
When navigating or referencing a 2D array, the standard convention is to specify the row index first, followed by the column index, such as Grid[row, column].
| Feature | 1D Array | 2D Array |
|---|---|---|
| Visualization | A single line or list | A grid, table, or matrix |
| Indexing | Single index: Array[i] |
Dual indices: Array[row, col] |
| Use Case | Simple lists (e.g., names) | Complex data (e.g., game boards) |
While a 1D array represents a single dimension of data, a 2D array represents data that has two distinct attributes or coordinates.
Check the Bounds: Always verify if the array starts at index 0 or 1, as this affects the calculation of the total number of elements and loop limits.
Off-by-One Errors: When using loops to iterate through an array, ensure the loop counter does not exceed the Upper Bound, which is a common cause of runtime errors.
Data Consistency: Remember that an array cannot store mixed types; attempting to put a string into an integer array will result in a type mismatch error.
Row-Major Order: In 2D arrays, always process the row index first unless the specific logic of the problem requires column-wise traversal.