Arrays Notes
Arrays Notes
### Definition
**Array**: An array is a collection of items stored at contiguous memory locations. The idea is to
store multiple items of the same type together. This makes it easier to calculate the position of each
element by simply adding an offset to a base value, i.e., the memory location of the first element of
**Single Dimensional Array (1-D Array)**: A list of items can be given a single index number to
Example:
```c
```
**Two Dimensional Array (2-D Array)**: It is often visualized as a table (matrix) with rows and
columns.
Example:
```c
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
};
```
**Row Major Order**: In row-major order, the consecutive elements of the rows of the array are
**Column Major Order**: In column-major order, the consecutive elements of the columns of the
**1-D Array**:
For an array `arr` with base address `base` and element size `w` (in bytes), the address of the
For a 2-D array `arr` with `m` rows and `n` columns, and base address `base`, the address of the
For a 2-D array `arr` with `m` rows and `n` columns, and base address `base`, the address of the
- **Storing Data**: Arrays are used to store multiple data items of the same type.
- **Matrices**: In scientific computing and graphics, arrays are used to represent matrices.
- **Static Lookup Tables**: Arrays are used in algorithms to create static lookup tables.
**Sparse Matrix**: A sparse matrix is a matrix in which most of the elements are zero. Efficient
storage and operations on sparse matrices are crucial for performance in many applications.
1. **Array Representation**: Use a 1-D or 2-D array but this is inefficient due to many zero elements.
2. **List of Lists (LoL)**: Each row of the matrix is represented as a list of column indices and
4. **Compressed Sparse Column (CSC)**: Similar to CSR but stores columns instead of rows.
```
003
100
020
```
- `values`: [3, 1, 2]
- `col_index`: [2, 0, 1]
- `row_pointer`: [0, 1, 2, 3]
This concise representation significantly reduces the memory footprint for sparse matrices.