0% found this document useful (0 votes)
2 views

Arrays Notes

The document provides an overview of arrays, defining them as collections of items stored in contiguous memory locations, and differentiates between single and multidimensional arrays. It explains the representation of arrays in row-major and column-major order, along with index formulae for accessing elements. Additionally, it discusses applications of arrays and various representations for sparse matrices, including Compressed Sparse Row (CSR) and Compressed Sparse Column (CSC).

Uploaded by

userdumb709
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays Notes

The document provides an overview of arrays, defining them as collections of items stored in contiguous memory locations, and differentiates between single and multidimensional arrays. It explains the representation of arrays in row-major and column-major order, along with index formulae for accessing elements. Additionally, it discusses applications of arrays and various representations for sparse matrices, including Compressed Sparse Row (CSR) and Compressed Sparse Column (CSC).

Uploaded by

userdumb709
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Notes on Arrays

### 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

the array (generally denoted by the name of the array).

### Single and Multidimensional Arrays

**Single Dimensional Array (1-D Array)**: A list of items can be given a single index number to

identify an element within the array.

Example:

```c

int arr[5] = {1, 2, 3, 4, 5};

```

**Multidimensional Array**: An array of arrays. For example, a two-dimensional array in C can be

defined as an array of arrays.

**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},

{9, 10, 11, 12}

};

```

### Representation of Arrays

**Row Major Order**: In row-major order, the consecutive elements of the rows of the array are

contiguous in memory. This is the default in languages like C and Python.

**Column Major Order**: In column-major order, the consecutive elements of the columns of the

array are contiguous in memory. This is common in languages like Fortran.

### Derivation of Index Formulae

**1-D Array**:

For an array `arr` with base address `base` and element size `w` (in bytes), the address of the

element `arr[i]` is calculated as:

\[ ext{Address} = ext{base} + i imes w \]

**2-D Array (Row Major Order)**:

For a 2-D array `arr` with `m` rows and `n` columns, and base address `base`, the address of the

element `arr[i][j]` is calculated as:


\[ ext{Address} = ext{base} + (i imes n + j) imes w \]

**2-D Array (Column Major Order)**:

For a 2-D array `arr` with `m` rows and `n` columns, and base address `base`, the address of the

element `arr[i][j]` is calculated as:

\[ ext{Address} = ext{base} + (j imes m + i) imes w \]

### Applications of Arrays

- **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.

- **Database Records**: Arrays are used to store records in databases.

- **Static Lookup Tables**: Arrays are used in algorithms to create static lookup tables.

### Sparse Matrices and Their Representations

**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.

**Representations of Sparse Matrices**:

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

values. This is a more space-efficient representation.


3. **Compressed Sparse Row (CSR)**: This representation uses three arrays to store non-zero

elements and their row and column indices.

- `values`: Stores non-zero elements.

- `col_index`: Stores the column index of each non-zero element.

- `row_pointer`: Stores the index in `values` where the row starts.

4. **Compressed Sparse Column (CSC)**: Similar to CSR but stores columns instead of rows.

- `values`: Stores non-zero elements.

- `row_index`: Stores the row index of each non-zero element.

- `col_pointer`: Stores the index in `values` where the column starts.

#### Example of CSR Representation:

Consider the matrix:

```

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.

You might also like