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
Computers store arrays in linear memory (one-dimensional).
For 1D array 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 = 1000, size = 4, and i = 3, then:
Address(A[3]) = 1000 + (3 × 4) = 1012
For multi-dimensional arrays (e.g., 2D arrays), we use
mapping techniques to store them in linear memory.
Two standard ways to represent 2D arrays in memory:
- Row-Major Order
- Column-Major Order
Row-Major Order
Definition: 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 Table
Feature Row-Major Column-Major
Order Order
Storage Style Rows stored Columns stored
sequentially sequentially
Formula Base + ((i × N) Base + ((j × M)
+ j) × size + i) × size
Efficient Row-wise Column-wise
Traversal
Used In C, C++, Java Fortran,
MATLAB
Real-Time Code Example
#include <stdio.h>
int main() {
int A[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9,10,11,12}
};
printf("Row-Major Order Traversal:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 4; j++) {
printf("%d ", A[i][j]);
}
}
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
Triplet (Coordinate List) Representation
Stores only non-zero elements with row and column indices.
Structure:
[Row, Column, Value]
Example:
Matrix (4x5): Triplet Representation:
0 0 0 5 0 Row Col Value
0 0 8 0 0 0 3 5
0 0 0 0 0 1 2 8
0 6 0 0 9 3 1 6
3 4 9
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:
Values: [5, 8, 6, 9]
Column Indices: [3, 2, 1, 4]
Row Pointers: [0, 1, 3,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
C Code Example for Triplet Representation
#include <stdio.h>
int main() {
int rows = 4, cols = 5;
int matrix[4][5] = {
{0, 0, 0, 5, 0},
{0, 0, 8, 0, 0},
{0, 0, 0, 0, 0},
{0, 6, 0, 0, 9}
};
printf("Triplet Representation:\n");
printf("Row Col Value\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != 0) {
printf("%3d %3d %5d\n", i, j, matrix[i][j]);
}
}
}
return 0;
}
C program that demonstrates various operations on a 1D array
#include <stdio.h>
#define SIZE 100
// Function to traverse and display the array
void traverse(int arr[], int n) {
printf("Array elements: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
// Function to reverse the array
void reverse(int arr[], int n) {
printf("Reversed array: ");
for (int i = n - 1; i >= 0; i--)
printf("%d ", arr[i]);
printf("\n");
// Function to insert an element at a given position
int insert(int arr[], int n, int pos, int value) {
if (n >= SIZE) {
printf("Insertion failed: Array is full.\n");
return n;
if (pos < 0 || pos > n) {
printf("Insertion failed: Invalid position.\n");
return n;
}
for (int i = n; i > pos; i--)
arr[i] = arr[i - 1];
arr[pos] = value;
return n + 1;
// Function to delete an element from a given position
int delete(int arr[], int n, int pos) {
if (pos < 0 || pos >= n) {
printf("Deletion failed: Invalid position.\n");
return n;
for (int i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
// Function to search for an element
void search(int arr[], int n, int value) {
for (int i = 0; i < n; i++) {
if (arr[i] == value) {
printf("Element %d found at position %d\n",
value, i);
return;
printf("Element %d not found in array\n", value);
}
// Function to sort the array using bubble sort
void sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
printf("Sorted array: ");
traverse(arr, n);
int main() {
int arr[SIZE], n = 0, choice, value, pos;
printf("Enter initial number of elements (max %d): ",
SIZE);
scanf("%d", &n);
printf("Enter elements:\n");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
while (1) {
printf("\nMenu:\n");
printf("1. Traverse\n2. Reverse\n3. Insert\n4.
Delete\n5. Search\n6. Sort\n7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
traverse(arr, n);
break;
case 2:
reverse(arr, n);
break;
case 3:
printf("Enter position and value to insert: ");
scanf("%d %d", &pos, &value);
n = insert(arr, n, pos, value);
break;
case 4:
printf("Enter position to delete: ");
scanf("%d", &pos);
n = delete(arr, n, pos);
break;
case 5:
printf("Enter value to search: ");
scanf("%d", &value);
search(arr, n, value);
break;
case 6:
sort(arr, n);
break;
case 7:
return 0;
default:
printf("Invalid choice!\n");
}
Selection Sort :
#include <stdio.h>
// Selection sort implementation
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min])
min = j;
if (min != i) {
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
int main() {
int arr[] = { 2 ,6, 1, 5, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
// Perform Selection Sort
selectionSort(arr,n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;