C++ DSA Arrays
C++ DSA Arrays
Home My courses CONT_23CSH-103 :: ELEMENTARY DATA STRUCTURES USING C++ Chapter 2.2
CO3 - Analyse and explain the behaviour of linear data structure operations using the programming addressed in
the course.
Arrays
Array is a type of linear data structure that is defined as a collection of elements with same or different data types.
They exist in both single dimension and multiple dimensions. These data structures come into picture when there is a
necessity to store multiple elements of similar nature together at one place.
The difference between an array index and a memory address is that the array index acts like a key value to label the
elements in the array. However, a memory address is the starting address of free memory available.
Index − Each location of an element in an array has a numerical index, which is used to identify the element.
Syntax
Creating an array in C++ programming language-
Arrays provide O(1) random access lookup time. That means, accessing the 1st index of the array and the 1000th index of
the array will both take the same time. This is due to the fact that array comes with a pointer and an offset value. The
pointer points to the right location of the memory and the offset value shows how far to look in the said memory.
array_name[index]
| |
Pointer Offset
Therefore, in an array with 6 elements, to access the 1st element, array is pointed towards the 0th index. Similarly, to
access the 6th element, array is pointed towards the 5th index.
Array Representation
Arrays are represented as a collection of buckets where each bucket stores one element. These buckets are indexed
from '0' to 'n-1', where n is the size of that particular array. For example, an array with size 10 will have buckets indexed
from 0 to 9.
This indexing will be similar for the multidimensional arrays as well. If it is a 2-dimensional array, it will have sub-
buckets in each bucket. Then it will be indexed as array_name[m][n], where m and n are the sizes of each level in the
array.
As per the above illustration, following are the important points to be considered.
Each element can be accessed via its index. For example, we can fetch an element at index 6 as 23.
Example
Here, we see a practical implementation of insertion operation, where we add data at the end of the array −
#include <iostream>
using namespace std;
int main(){
int LA[3] = {}, i;
cout << "Array Before Insertion:" << endl;
for(i = 0; i < 3; i++)
cout << "LA[" << i <<"] = " << LA[i] << endl;
Output
Array Before Insertion:
LA[0] = 0
LA[1] = 0
LA[2] = 0
Inserting elements..
Array After Insertion:
LA[0] = 2
LA[1] = 3
LA[2] = 4
LA[3] = 5
LA[4] = 6
Deletion Operation
In this array operation, we delete an element from the particular index of an array. This deletion operation takes place
as we assign the value in the consequent index to the current index.
Example
Following are the implementations of this operation in various programming languages −
#include <iostream>
using namespace std;
int main(){
int LA[] = {1,3,5};
int i, n = 3;
cout << "The original array elements are :"<<endl;
for(i = 0; i<n; i++) {
cout << "LA[" << i << "] = " << LA[i] << endl;
}
for(i = 1; i<n; i++) {
LA[i] = LA[i+1];
n = n - 1;
}
cout << "The array elements after deletion :"<<endl;
for(i = 0; i<n; i++) {
cout << "LA[" << i << "] = " << LA[i] <<endl;
}
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
The array elements after deletion :
LA[0] = 1
LA[1] = 5
Search Operation
Searching an element in the array using a key; The key element sequentially compares every value in the array to
check if the key is present in the array or not.
Example
Following are the implementations of this operation in various programming languages −
#include <iostream>
using namespace std;
int main(){
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0;
cout << "The original array elements are : " <<endl;
for(i = 0; i<n; i++) {
cout << "LA[" << i << "] = " << LA[i] << endl;
}
for(i = 0; i<n; i++) {
if( LA[i] == item ) {
cout << "Found element " << item << " at position " << i+1 <<endl;
}
}
return 0;
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Found element 5 at position 3
Traversal Operation
This operation traverses through all the elements of an array. We use loop statements to carry this out.
#include <iostream>
using namespace std;
int main(){
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
cout << "The original array elements are:\n";
for(i = 0; i<n; i++)
cout << "LA[" << i << "] = " << LA[i] << endl;
return 0;
}
Output
The original array elements are:
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Update Operation
Update operation refers to updating an existing element from the array at a given index.
#include <iostream>
using namespace std;
int main(){
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
cout << "The original array elements are :\n";
for(i = 0; i<n; i++)
cout << "LA[" << i << "] = " << LA[i] << endl;
LA[2] = item;
cout << "The array elements after updation are :\n";
for(i = 0; i<n; i++)
cout << "LA[" << i << "] = " << LA[i] << endl;
return 0;
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
The array elements after updation are :
LA[0] = 1
LA[1] = 3
LA[2] = 10
LA[3] = 7
LA[4] = 8
Display Operation
This operation displays all the elements in the entire array using a print statement.
Example
Following are the implementations of this operation in various programming languages −
#include <iostream>
using namespace std;
int main(){
int LA[] = {1,3,5,7,8};
int n = 5;
int i;
cout << "The original array elements are :\n";
for(i = 0; i<n; i++)
cout << "LA[" << i << "] = " << LA[i] << endl;
return 0;
}
Output
The original array elements are :
LA[0] = 1
LA[1] = 3
LA[2] = 5
LA[3] = 7
LA[4] = 8
Previous activity
Jump to...
Next activity