Arrays
Arrays
Introduction
● Arrays are defined as the collection of similar types of data items stored at
contiguous memory locations.
● It is a linear data structures where each data element can be randomly accessed by
using its index number.
● In C++, they are the derived data types that can store the primitive type of data such
as int, char, double, float, etc. For example, if we want to store the marks of a student
in 6 subjects, then we don't need to define a different variable for the marks in
different subjects. Instead, we can define an array that can store the marks in each
subject at the contiguous memory locations.
● In array, space complexity is O(n).
Properties of Array
● There are some of the properties of an array that are listed as follows -
○ Each element in an array is of the same data type and carries the same size
depending on the data type.
○ Elements in the array are stored at contiguous memory locations from which the
first element is stored at the smallest memory location.
○ Elements of the array can be randomly accessed.
Representation of an Array
Are there any other indexing techniques of array other than zero based indexing?
Algorithm: C++:
#include <iostream>
Given an array of size n using namespace std;
Loop from i=0 to n: int main() {
print array element at index i int arr[5] = {18, 30, 15, 70, 12};
cout<<"Elements of the array are:\n";
Time Complexity: O(n) for(i = 0; i<5; i++) {
cout<<arr[i]<<" ";
}
}
Insertion Operation - Single Element
C++:
Algorithm:
#include <iostream>
using namespace std;
Given an index an array element
int main() {
Insert element at array[index]
int arr[5] = {18, 30, 15, 70, 12};
arr[ 2 ] = 10;
Time Complexity: O(1)
cout<<arr[ 2 ];
}
Insertion Operation - Rearranging
Algorithm
● Given an array of maximum size k, element, position, current array size n
● Loop from i=n to position
Assign array[i]=arr[i-1]
● array[position]=element
● Increment the value of n by 1
C++:
#include <iostream> n++;
using namespace std; cout<<"Array elements after insertion";
int main() for (i = 0; i < n; i++)
{ cout<<arr[i]<<" ";
int arr[20] = { 18, 30, 15, 70, 12 }; return 0;
int i, x, pos, n; }
n = 5; //current array size
x = 50; // element to be inserted
pos = 3; //position
for (i = n; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = x;
Insertion Operation - All Elements
C++:
Algorithm:
#include <iostream>
using namespace std;
Declare an array of size n
int main() {
Loop from i=0 to n
int arr[5];
Insert element at index i
for(int i=0;i<5;i++){
cout<<"Enter Array Elements:";
cin>>arr[i];
Time Complexity: O(n)
}
}
Deletion Operation
Algorithm
● Given an array of maximum size k, element, position, current array size n
● Loop from i=position-1 to n
Assign array[i]=arr[i+1]
● Decrement the value of n by 1
C++ n--;
#include <iostream> cout<<"Array elements after insertion\n";
using namespace std; for (i = 0; i < n; i++)
int main() cout<<arr[i]<<" ";
{ return 0;
int arr[20] = { 18, 30, 15, 70, 12 }; }
int i, x, pos, n;
n = 5; //current array size
pos = 3; //position
for (i = pos-1; i < n; i++)
arr[i] = arr[i + 1];
Searching Operation
Algorithm
● Given an array of size n, element
● Loop from i=0 to n
Check array[i]==element
Show index i
C++
#include <iostream>
using namespace std;
int main()
{
int arr[20] = { 18, 30, 15, 70, 12 };
int x=15;
for (i = 0; i < n; i++){
if(arr[i]==x)
cout<<"Element found at index:"<<i;
}
return 0;
}
2D Array
Store a single list of the element of a similar Store a ‘list of lists’ of the element of a similar data
Definition
data type. type.
The declaration varies for C++ The declaration varies for different programming
Declaration programming language: language:,
datatype variable_name[row]; datatype variable_name[row][column];
1D Array vs 2D Array
size of(datatype of the variable of the array) size of(datatype of the variable of the array)* the
Size(bytes)
* size of the array number of rows* the number of columns.
int arr[2][5]; //an array with two rows and five columns
int arr[5]; //an array with one row and five will be created.
Example columns will be created.
a b c d e
{a , b , c , d , e}
f g h i j