Arrays:
An array is a linear data structure that stores elements of the same data type in
contiguous memory locations. Each element can be accessed using an index.
Array Element: Elements are items stored in an array.
Array Index: Elements are accessed by their indexes. Indexes in most of the
programming languages start from 0.
Memory representation of Array
In an array, all the elements or their references are stored in contiguous memory
locations. This allows for efficient access and manipulation of elements.
Declaration of Array
Arrays can be declared in various ways in different languages. For better illustration,
below are some language-specific array declarations:
datatype array_name[size];
Ex: int arr[5];
char arr[10];
Initialization of Array
Arrays can be initialized in different ways in different languages. Below are some
language-specific array initialization:
int arr[] = { 1, 2, 3, 4, 5 };
char arr[5] = { 'a', 'b', 'c', 'd', 'e' };
Types of Arrays
Arrays can be classified in two ways:
On the basis of Size - Alter or update the size of the array is not possible once it is
fixed because memory will be allocated based on the size declared.
On the basis of Dimensions
Types of Arrays on the basis of Dimensions
1. One-dimensional Array(1-D Array): You can imagine a 1d array as a row, where
elements are stored one after another.
2. Two-Dimensional Array(2-D Array or Matrix): 2-D Multidimensional arrays can be
considered as an array of arrays or as a matrix consisting of rows and columns.
int mat[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
3. Three-Dimensional Array(3-D Array): A 3-D Multidimensional array contains three
dimensions, so it can be considered an array of two-dimensional arrays.
Basic Operations in Arrays
Traverse − print all the array elements one by one.
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Search − Searches an element using the given index or by the value.
Update − Updates an element at the given index.
Display − Displays the contents of the array.
Traversing an Array
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Insertion in an Array
Insert an element at a specific position.
#include <stdio.h>
int main() {
int arr[10] = {10, 20, 30, 40, 50};
int n = 5, pos = 3, val = 25;
for(int i = n; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = val;
n++;
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Deletion from an Array
Delete an element from a given position.
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int n = 5, pos = 3;
for(int i = pos - 1; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Searching in an Array
int linearSearch(int arr[], int n, int key) {
for(int i = 0; i < n; i++) {
if(arr[i] == key)
return i;
}
return -1;
}
Advantages of Arrays
Fast access using index
Easy to implement
Efficient memory usage
Disadvantages of Arrays
Fixed size
Insertion and deletion are costly
Memory wastage if not fully used
Applications of Arrays
Searching and sorting algorithms
Matrices and tables
Implementing stacks and queues
Image processing and numerical computation