# Array Data Structures

## Detail Notes on Array data structures with their representation in memory

Jan 2, 2022·

Part One:

Part Two:

Arrays: This is one of the simplest type of data structure. It falls under the linear data structures category. If we choose the name A for the array so we can represent the elements of the array in following way:

1. A1, a2, a3…..an
2. A(1), A(2), A(3)….A(N)
3. A[1], A[2], A[3]….A[N]

NAME:

2. Ali
3. Ahsan
4. Raza
5. Awais

In the above example if we want to access the name Ahsan from the array “NAME” we will write it as NAME [3] same as like this for name “Hammad” => NAME [1].

If we have 1, 2, 3…n numbers in the array, the number n is known as the size of array. We can calculate the maximum number of elements in the array with the formula.

Maximum number of elements=UB-LB+1 In the above formula LB => Lower Bound is the index of first element in the array. Where as, UB => upper bound is the index of last element in the array.

int a[5]

``````=UB-LB+1
= 4-0+1
=5
``````

So, the size of the array is 5.

NOTE: In the example above numbers from 0 to 4 are actually indexes of the array on which we can place elements.

Representation of linear array in memory: Memory of the computer is simply a sequence of addresses or say addressed locations. We already know arrays take memory in linear order so there is no need to store the memory addresses of all elements of the array. Memory address of only first element of the array is enough to store and calculate the further memory locations of the array elements.

For example: If an array A have 6 elements, length of each is 2 bytes, to find out the address of the third element of array “A” we should have the address of base element is 200. Here is the formula:

``````    A[K] = LO+C* (K-1)
``````

In the above formula; A = Name of array L = base element memory address C = length or size of each element in memory K = index of the element in array “A” Now by putting the values in formula for finding the address of 3rd element in array

``````    A [3] = 200+2* (3-1)
= 200+4
``````

## Different Array Operation Codes using C++ :

### Array Insertion Operation

``````#include<iostream>
using namespace std;
int main()
{
int A[3];
A[0] = 5;
A[1] = 55;
A[2] = 56;
A[3] = 58;
cout<<A[0];
//Method two using For Loop and taking user input
int B[3];
for(int i=0; i<=3; i++)
{
cout<<"Enter Element for the index of "<<i<<" of Array ";
cin>>B[i];
}
cout<<B[2];
}
``````

### Array Merge Operation :

``````#include<iostream>
using namespace std;
int main()
{
int a[4]; // array number one. . .
int b[4]; // array number two. . .
int c[8]; // array number three in which we merge above loops. . .
int i; // for loop variable. .
cout<<"Enter Elements in 1st Array: ";
for(i=0;i<4;i++)
{
cin>>a[i];
}
cout<<"Enter Elements in 2nd Array: ";
for(i=0;i<4;i++)
{
cin>>b[i];
}
cout<<"\nElements of Array After Merge: ";
for(i=0;i<4;i++)
{
c[i]=a[i];
c[i+4]=b[i];
}
for(i=0;i<8;i++)
{
cout<<c[i];
}
}
``````

### Array Search Operation:

``````#include<iostream>
using namespace std;
int main()
{
int myArray[4];
int Data_to_Search;
for(int i =0;i<=4;i++)
{
cout<<"Enter the data for location "<<i;
cin>>myArray[i];
}
cout<<"Enter the number which you want to search in array ";
cin>>Data_to_Search;
for(int i =0;i<=4;i++)
{
if(myArray[i]==Data_to_Search)
{
cout<<"Data "<<Data_to_Search<<" is at location "<<i;
}
``````

### Array Sorting Operation:

``````#include <iostream>
using namespace std;
int main()
{
int arr[4];
int size, i, j, temp;
cout<<"Enter elements in array: ";
for(i=0; i<4; i++)
{
cin>>arr[i];
}
//Sorting an array in ascending order
for(i=0; i<4; i++)
{
for(j=i+1; j<4; j++)
{
//Using the Swap method. . .
if(arr[j] < arr[i])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//Printing the sorted array. . .
cout<<"Elements of array in sorted ascending order:"<<endl;
for(i=0; i<4; i++)
{
cout<<arr[i]<<endl;
}
}
``````

### Array Deletion Operation:

``````#include<iostream>
using namespace std;
int main()
{
//Varrialble initializations
int myArray[4];
int location = 0;
//for loop for taking input into the array
for(int i=0;i<=4;i++)
{
cout<<"Enter number in the location "<<i<<" of array";
cin>>myArray[i];
}
//for loop to show the array data on screen.
for(int i=0; i<=4; i++)
{
cout<<"Number on location "<<i<<" is "<<myArray[i]<<endl;
}
//enter the location to delete data from that specific location.
cout<<"Enter location from where you want to delete the Data";
cin>>location;
//Listen explaination in video to understand how this loop is working.
for(int i =location; i<=4; i++)
{
myArray[location] = myArray[location+1];
location++;
}
for(int i=0; i<=4-1; i++)
{
cout<<"Number on location "<<i<<" is "<<myArray[i]<<endl;
}
}
``````