# Single Dimensional Array

Single dimensional array are also known as 1-D array. In single Dimensional array all store in sequential way. In Single Dimensional Array access the data is easy due to there sequential allocation in the memory.

Single Dimensional array the basic Data structure which is used to perform many Data structure application lime stack, queue, and other data structure.

## Array ADT (Abstract Data type)

ADT ( Abstract Data Type) is a class that has a defined set of operation and values. Array are define ADT because they are capable to holding contiguous element in the same order. Abstract Data Type of array are:

1. Display()
3. insert(index, n)
4. Delete(n)
5. Max()/Min()
6. Reverse()
7. shift()/Rotate()

### Display()

Display function is used to print the element of the array.

``````void Display(){
int length = 5;
for (int i=0; i< length; i++){
printf("%d",array[i]);
}
}``````

Append means insert the element in last position of the array. while append the array we check it extra space are present in the array or not. if the space is present append the else print no space.

After append with element 5 at position 4.

``````void Append (n){
//n element
int length = 5;
array{length-1]=n;
for (int i=0; i<length; i++){
printf("%d",array[i]);
}
}

``````

### Insert(index, n)

Insert function is used to insert element in single dimensional array at particular index. For insertion first we check that the space are present in the array or not.

1. Get the element value which needs to be inserted.

2. Get the position value.

3. Check whether the position value is valid or not.

4. If it is valid,

Shift all the elements from the last index to position index by 1 position to the right.

insert the new element in arr[position]

5. Otherwise,

`````` void insert(int index ,int x)
{
if (index >= 0 && index <=length)
{
for (int i =length-1; i >= index; i--)
{
A[i+1] = A[i];
}
A[index]=x;
length++;

}

} ``````

### Delete(n)

Delete operation is used to delete the element in the array. we only pass that element which we want to delete.

1. Find the given element in the given array and note the index.

2. If the element found,

Shift all the elements from index + 1 by 1 position to the left.

Reduce the array size by 1.

``````int Delete(int index)
{
int x=0;
if (index >0 && index <=length)
{
x=A[index];
for (int i =index ;i <=length-1 ;i++)
{
A[i]=A[i+1];
}
length--;
}
cout<<endl;
return x;
}``````

### Max(n)/Min(n)

Max and Min function are used to find the maximum and Minimum element in Single Dimensional array.

``````int( main()
{
int a[]={12,34,2,4,5,5,634,5};
int length=sizeof(a)/sizeof(a);
int max=a;
int min=a;
for (int i=0;i<length-1;i++)
{
if (a[i]<max)
max=a[i];
if (a[i]>min)
min =a[i];

}
cout<<min<<endl<<max;
}  ``````

### Reverse()

Reverse function is used to reverse the array.

``````void Reverse(int array,int lenght){
for (int i=0; i<length -1; i++){
int temp=array[i];
array[i]=array[length-i-1];
array[length-i-1]=temp;
}
}``````

### Shift()/Rotate()

shift and Rotate function are used to rotate the element in the array.

1. Initialize a temporary array(temp[n]) of length same as the original array
2. Initialize an integer(k) to keep a track of the current index
3. Store the elements from the position d to n-1 in the temporary array
4. Now, store 0 to d-1 elements of the original array in the temporary array
5. Lastly, copy back the temporary array to the original array
``````void Rotate(int arr[], int d, int n)
{
int temp[n];
int k = 0;

for (int i = d; i < n; i++) {
temp[k] = arr[i];
k++;
}

for (int i = 0; i < d; i++) {
temp[k] = arr[i];
k++;
}

for (int i = 0; i < n; i++) {
arr[i] = temp[i];
}
}``````

Practice Question