What is Array in C++ ? Data structures and algorithm
Last modified - 24-04-2025
Author - Krishna Shinde
An array is the collection of elements of the same data type stored in a contiguous memory location, an array is a linear data structure that is used to maintain a large number of data under a single name or variable to avoid confusion. In this article we will discuss arrays in detail also we will solve a few questions that will strengthen your logic building.
Why use arrays?
Imagine if you have been assigned a task of storing the age of 50 people in your locality, so you thought of creating a different variable for each person, in this case, you have to create 50 variables which is possible, but if the number increased to 500, will you create 450 more variables? You may create them but there is a simple way to store them in a single variable that is using an array. But how to store them? It is discussed below.
Declaration of an Array in C++
An array can be declared by writing the data type, the name, and the square brackets which determine the size. After the equal to sign the values are written inside the braces in a comma-separated manner.
Here we will use an integer data type as age is a numeric value.
int age[50] = {20, 32, 24, 45, 43};
Accessing the Elements of an Array in C++
Elements of an array can be accessed using the index of the element. This index starts from 0 to size – 1, for example, if the size is 10 then the index will start from 0 to 9, if you want to access the third element you have to write age[2], and for the first element age[0].
cout<<arr[2];
cout<<arr[0];
Updating an Element in an Array in C++
You can update the element in an array using its index you just have to write arrayName[index] = newValue;
arr[3] = 48;
How array is stored in the memory
An array is stored in contiguous memory locations, for an integer array if the memory address of the first element is 4 then the address of the second element will be 8 and the address of the last element will be 20, this is the visual representation.

Traversal in Arrays
Traversal of an array means going through the entire array and reading the values at each index.
for (int i = 0; i < size; i++) {
cout<<arr[i];
}

Linear Search Algorithm
Linear search also called sequential search is a searching algorithm used to find an element in an array. It starts from the first element, and compares each value with the key or target value, if a match is found it returns its index of not, and reaches the end it returns something like -1.
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Return index if found
}
}
return -1; // Return -1 if not found
}

Time complexity of Linear search algorithm : O(n)
Reversing an Array
An array can be reversed by swapping the first and last element, second and second last, and continuing it until it reaches the mid, creating two variables start and end, and initialize it two the first and last index respectively swap them and then increase start by one and decrease end by 1, continue it until the value of start become greater than end.
void reverseArray(int arr[], int size) {
int start = 0;
int end = size - 1;
while (start < end) {
// Swap the elements
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
Time complexity of reversing : O(n)
Swapping alternative
An array can be swapped alternatively by swapping the first and second element, then the third and fourth, and continuing until it reaches the end, you can take two variables first and second which contain the indices of the first and second element respectively, after swapping them increment both by two so that first and second reaches third and fourth.
void swapAlternates(int arr[], int size) {
int first = 0;
int second = 1;
while (second < size) {
// Swap arr[first] and arr[second]
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
// Move to next alternate pair
first += 2;
second += 2;
}
}
Time complexity of swapping alternatively : O(n)
Conclusion
So, this was the detailed description of the array with code in C++ and a visual representation of the algorithms. Much more to come regarding data structures and algorithms, follow Algoflame of social media to stay updated in the field of programming.