# Array: Functions and Operations In today’s lesson, we’ll explore array functions, array operations, and some array questions.
A container called an array can store a certain number of elements, all of which must be of the same type. The majority of data structures use arrays to carry out their algorithms. The keywords to grasp the structure of an array are listed below.

• Element − An element is anything that is kept in an array.
• Index − An element in an array has a numerical index at every position that serves as the element’s identification.

## Array Representation

Different languages have different techniques for declaring arrays. Take the C array declaration as an example. In different languages, there are several ways to declare arrays. Take the declaration of a C array as an example. These are the crucial elements to think about, according to the example above.

• The index starts with 0.
• Since the array’s length is 10, it can hold 10 elements.
• The index of each element can be used to retrieve it. We can, for instance, get an element at index 6 as 9.

## Basic Array Operations

Following are the basic Array operations.

• Traverse − Print each element in the array one by one.
• Insertion − At the specified index, adds an element.
• Deletion − The element at the specified index is deleted.
• Search − Uses the provided index or the value to search for an element.
• Update − The element at the specified index is updated.

When an array in C is started with size, the elements are subsequently given default values in the manner described below.

Data Type Default Value
bool false
char 0
int 0
float 0.0
double 0.0f
void
wchar_t 0

### Traverse Array Operations

The elements of an array are traversed during this procedure.
Example
The following program iterates through an array and outputs each element:

```#include <stdio.h>
main() {
int Arr[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are:\n");
for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
}
```

The program above generates the following output when it is compiled and run:

Output

``````The original array elements are:
Arr = 1
Arr = 3
Arr = 5
Arr = 7
Arr = 8``````

### Insertion Array Operations

One or more data elements are added to an array using the insert array operations. An array can have a new element inserted at any index, including the start, end, or middle, depending on the need.
In this example, the insertion action is implemented practically by adding data to the end of the array.
Example
The implementation of the above algorithm is shown below.

```#include <stdio.h>
main() {
int Arr[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;

printf("The original array elements are:\n");

for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}

n = n + 1;

while( j >= k) {
Arr[j+1] = Arr[j];
j = j - 1;
}

Arr[k] = item;

printf("The array elements after insertion:\n");

for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
}
```

The program above generates the following output when it is compiled and run:

Output

``````The original array elements are:
Arr = 1
Arr = 3
Arr = 5
Arr = 7
Arr = 8
The array elements after insertion:
Arr = 1
Arr = 3
Arr = 5
Arr = 10
Arr = 7
Arr = 8``````

### Deletion Array Operations

Deletion is the process of eliminating an existing element from an array and rearranging all of the array’s elements.

Algorithm
Take into consideration that K is a positive integer such that K=N and Arr is a linear array with N items. The algorithm to remove one element from the Kth position of Arr is shown below.

• Start
• Set J = K
• Repeat steps 4 and 5 while J < N
• Set Arr[J] = Arr[J + 1]
• Set J = J+1
• Set N = N-1
• Stop

Example
The implementation of the above algorithm is shown below.

```#include <stdio.h>
void main() {
int Arr[] = {1,3,5,7,8};
int m = 3, n = 5;
int i, j;

printf("The original array elements are:\n");

for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}

j = m;

while( j < n) {
Arr[j-1] = Arr[j];
j = j + 1;
}

n = n -1;

printf("The array elements after deletion:\n");

for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
}
```

The program above generates the following output when it is compiled and run:

Output

``````The original array elements are :
Arr = 1
Arr = 3
Arr = 5
Arr = 7
Arr = 8
The array elements after deletion:
Arr = 1
Arr = 3
Arr = 7
Arr = 8 ``````

### Search Array Operations

An array element can be found using either its value or its index.

Algorithm
Take into consideration that K is a positive integer such that K=N and Arr is a linear array with N items. The sequential search technique to locate an element with the value of ITEM is shown below.

• Start
• Set J = 0
• Repeat steps 4 and 5 while J < N
• IF Arr[J] is equal ITEM THEN GOTO STEP 6
• Set J = J +1
• PRINT J, ITEM
• Stop

Example
The above algorithm is implemented as follows:

```#include <stdio.h>
void main() {
int Arr[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;

printf("The original array elements are:\n");

for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}

while( j < n){
if( Arr[j] == item ) {
break;
}

j = j + 1;
}

printf("Found element %d at position %d\n", item, j+1);
}
```

The program above generates the following output when it is compiled and run:

Output

``````The original array elements are:
Arr = 1
Arr = 3
Arr = 5
Arr = 7
Arr = 8
Found element 5 at position 3``````

### Update Array Operations

Update array operations involve changing an existing array element at a certain index.

Algorithm
Take into consideration that K is a positive integer such that K=N and Arr is a linear array with N items. The technique to update an element that is accessible at the Kth position of Arr is shown below.

• Start
• Set Arr[K-1] = ITEM
• Stop

Example
The implementation of the above algorithm is shown below.

```#include <stdio.h>
void main() {
int Arr[] = {1,3,5,7,8};
int m = 3, n = 5, item = 10;
int i, j;

printf("The original array elements are:\n");

for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}

Arr[m-1] = item;

printf("The array elements after updation:\n");

for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
}
```

The above program generates the following output when it is compiled and run:

Output

``````The original array elements are:
Arr = 1
Arr = 3
Arr = 5
Arr = 7
Arr = 8
The array elements after updation:
Arr = 1
Arr = 3
Arr = 10
Arr = 7
Arr = 8``````

Now, we will discuss some important array questions that will help in understanding the arrays.

## FAQs Related to Array

1. Write A Program To Print All The Elements Of Array In Reverse Order.

```#include <stdio.h>

int main()
{

int arr[] = {1, 2, 3, 4, 5};

int length = sizeof(arr)/sizeof(arr);

printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}

printf("\n");

printf("Array in reverse order: \n");
for (int i = length-1; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}```

Output

``````Original array:
1 2 3 4 5
Array in reverse order:
5 4 3 2 1``````

2. Write A Program To Rotate An Array.

```#include <stdio.h>

int main()
{
//Initialize array
int arr[] = {1, 2, 3, 4, 5};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr);
//n determine the number of times an array should be rotated
int n = 3;

//Displays original array
printf("Original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}

//Rotate the given array by n times toward right
for(int i = 0; i < n; i++){
int j, last;
//Stores the last element of the array
last = arr[length-1];

for(j = length-1; j > 0; j--){
//Shift element of array by one
arr[j] = arr[j-1];
}
//Last element of array will be added to the start of array.
arr = last;
}

printf("\n");

//Displays resulting array after rotation
printf("Array after right rotation: \n");
for(int i = 0; i< length; i++){
printf("%d ", arr[i]);
}
return 0;
}```

Output

``````Original array:
1 2 3 4 5
Array after right rotation:
3 4 5 1 2 ``````

3. Write A Program To Find The Largest Element Of The Array.

```#include <stdio.h>

int main()
{
//Initialize array
int arr[] = {25, 11, 7, 75, 56};

//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr);

//Initialize max with first element of array.
int max = arr;

//Loop through the array
for (int i = 0; i < length; i++) {
//Compare elements of array with max
if(arr[i] > max)
max = arr[i];
}
printf("Largest element present in given array: %d\n", max);
return 0;
}```

Output

``Largest element present in given array: 75``

4. Write A Program To Find The Smallest Element Of The Array.

```#include <stdio.h>

int main()
{
//Initialize array
int arr[] = {25, 11, 7, 75, 56};

//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr);

//Initialize min with first element of array.
int min = arr;

//Loop through the array
for (int i = 0; i < length; i++) {
//Compare elements of array with min
if(arr[i] < min)
min = arr[i];
}
printf("Smallest element present in given array: %d\n", min);
return 0;
}```

Output

``Smallest element present in given array: 7``

5. Write A Program To Find The Sum of All The Elements Of Array.

```#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;

//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr);

//Loop through the array to calculate sum of elements
for (int i = 0; i < length; i++) {
sum = sum + arr[i];
}
printf("Sum of all the elements of an array: %d", sum);
return 0;
}```

Output

``Sum of all the elements of an array: 15``