Introduction to Arrays
An array is a collection of items of the same data type stored at contiguous memory locations. Elements are identified by an index or a key.
Dimensions of Arrays
One-Dimensional Array
A linear sequence of elements.
Two-Dimensional Array
A grid of elements in rows and columns.
Multi-Dimensional Array
An array of arrays (e.g., a cube).
2D Array Memory Representation
In memory, a 2D array is flattened into a 1D sequence. This can be done in two ways: Row-Major or Column-Major order. Use the calculator below to see how the memory address of an element is calculated.
Address Calculator
Results
Row-Major Order
Address = base + (i * num_cols + j) * size
Column-Major Order
Address = base + (j * num_rows + i) * size
Application: Polynomials
Arrays (especially arrays of structures) are an efficient way to represent polynomials, storing only the non-zero terms.
Polynomial to Array Representation
Structured Array Representation
Application: Sparse Matrices
A sparse matrix is a matrix with very few non-zero elements. Storing it as a full matrix is inefficient. Instead, we store only the non-zero elements in a "triplet" format (row, column, value).
Interactive Matrix (5x5)
Click cells to toggle non-zero values.