0% found this document useful (0 votes)
2 views5 pages

Notes on Array

The document provides an overview of arrays in C, including their definition, syntax for declaration and initialization, and types such as one-dimensional, two-dimensional, and multi-dimensional arrays. It also discusses memory representation techniques for arrays, particularly row-major and column-major orders, and introduces sparse matrices, their characteristics, and various representation formats like triplet, CSR, and CSC. Additionally, it highlights real-time applications of sparse matrices in fields such as machine learning and scientific computing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Notes on Array

The document provides an overview of arrays in C, including their definition, syntax for declaration and initialization, and types such as one-dimensional, two-dimensional, and multi-dimensional arrays. It also discusses memory representation techniques for arrays, particularly row-major and column-major orders, and introduces sparse matrices, their characteristics, and various representation formats like triplet, CSR, and CSC. Additionally, it highlights real-time applications of sparse matrices in fields such as machine learning and scientific computing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Notes on Array

Definition: In C, an array is a data structure that stores a fixed-size sequential


collection of elements of the same data type. Arrays are used to store multiple values in
a single variable, instead of declaring separate variables for each value.

Syntax of Array Declaration: data_type array_name[array_size]

Example: int numbers[5]; // Declares an array of 5 integers

Here:
• int = data type
• numbers = name of the array
• 5 = number of elements it can store (index from 0 to 4)

Initialize an array at the time of declaration: Values to an array can be assigned during
its declaration. This process is known as initialization at the time of declaration.
Syntax for this is:

data_type array_name[size] = {value1, value2, ..., valueN};

Example: int numbers[5] = {10, 20, 30, 40, 50};

Here, the array numbers is created with a size of 5. Each value is assigned in order
to each index viz. numbers[0] = 10, numbers[1] = 20, …, numbers[4] = 50

Size determined by the compiler: If the size is omitted while initializing an array, the
compiler will automatically calculate the size based on the number of values provided.

Example: int numbers[] = {10, 20, 30, 40, 50}; // Automatically size = 5

Since there are 5 values in the initializer list, the compiler sets the size of numbers to 5.
This saves manual effort and avoids mismatch between declared size and number of
initial values.

Types of Arrays: On the basis of dimensions, there are 3 type of array in C:-

1. One-Dimensional Array (1D):


• It is a linear list of elements.
• Its syntax for declaration is : int arr[10];
• It is used for simple lists, like marks, prices, etc.

2. Two-Dimensional Array (2D):


• A table of rows and columns.
• Syntax: int matrix[3][4];
• Used for matrices, tables, grids.
3. Multi-Dimensional Array:
• Arrays with more than two dimensions.
• Syntax: int arr[3][4][2];
• Used in advanced applications like 3D graphics or tensor computations.

Memory Representation of Arrays

For 1D array

• Computers store arrays in linear memory (one-dimensional).

• suppose A be a 1D array of size N. Each element takes size bytes. Base is the
base address. You want to access A[i]
Address(A[i]) = Base + (i × size)
Ex.:- If Base = 2001, size = 2, and i = 3, then:
Address(A[3]) = 2001 + (3 × 2) = 2007

For multi-dimensional arrays (e.g., 2D arrays)

• Mapping techniques to store them in linear memory is used.


• Two standard ways to represent 2D arrays in memory:
- Row-Major Order
- Column-Major Order

Row-Major Order

• In Row-Major Order, the rows of the array are stored one after the other in
memory.
• Example:
A[3][4] =
[ [1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12] ]
• Memory Layout:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
• Address Calculation (Row-Major):
Address(A[i][j]) = Base + ((i * N) + j) * size
Where: Base = Base address of the array
i = Row index
j = Column index
N = Total number of columns
size = Size of each element in bytes

Column-Major Order

• Definition: In Column-Major Order, the columns of the array are stored


one after the other in memory.
• Example:
A[3][4] =
[ [1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12] ]
• Memory Layout:
[1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12]
• Address Calculation (Column-Major):
Address(A[i][j]) = Base + ((j * M) + i) * size
Where:
Base = Base address of the array
i = Row index
j = Column index
M = Total number of rows
size = Size of each element in bytes

Comparison Diagram
Sparse Matrices and Their Representation
Definition:

A sparse matrix is a matrix in which most of the elements are zero.


Characteristics:
- Contains few non-zero elements.
- Saves memory and improves computation time.

- Opposite to sparse, a matrix where most elements are non-zero is called Dense
Matrix.

Why Use Sparse Matrix Representation?

- To avoide wastes memory storing zeroes.


- Improves efficiency.
- Easier mathematical operations on large matrices.

Types of Sparse Matrix Representations

1. Triplet (Coordinate List) Representation

Stores only non-zero elements with row and column indices.


Structure:
[Row, Column, Value]

Example:
Matrix (4x5): Triplet Representation:
00 0 5 0 Row Col Value
00 8 0 0 0 3 5
00 0 0 0 1 2 8
06 0 0 9 3 1 6
3 4 9

2. Compressed Sparse Row (CSR) Format

Efficient for row-wise operations.


Components:
- Values: All non-zero elements
- Column Indices: Corresponding column of each non-zero value
- Row Pointers: Index in Values[] where each row starts

Example:
Original Matrix: CSR Representation: Build rowPtr Array
rowPtr[i] to Corresponds Range in Non-Zero
0 0 3 0 4 0 values[] = [3, 4, 5, 2, 6, 1] rowPtr[i+1] to Row values[] Elements
0 0 0 0 0 0 colIndex[] = [2, 4, 0, 3, 5, 1] values[0]
5 0 0 2 0 0 rowPtr[] = [0, 2, 2, 4, 5, 6] 3 at col 2,
0→2 Row 0 to
4 at col 4
0 0 0 0 0 6 values[1]
0 1 0 0 0 0 2→2 Row 1
(empty

row)
values[2] 5 at col 0,
2→4 Row 2
to [3] 2 at col 3
4→5 Row 3 values[4] 6 at col 5

5→6 Row 4 values[5] 1 at col 1

3. Compressed Sparse Column (CSC) Format

Efficient for column-wise operations.


Similar to CSR, but compresses columns instead of rows.

Real-Time Applications of Sparse Matrices

- Machine Learning: Feature vectors in NLP, image recognition


- Graph Algorithms: Sparse adjacency matrices
- Scientific Computing: Finite element methods
- Databases and Search Engines: Document-term matrices

You might also like