Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Array: Functions and Operations

Last Updated on August 29, 2023 by Mayank Dham

In the present session, we will delve into array functions, array operations, and address some array-related inquiries. An entity referred to as an "array" can house a specific quantity of elements, all sharing a uniform data type. Arrays find widespread application in numerous data structures to execute their algorithms. The key concepts essential for comprehending array structure are provided 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

Distinct programming languages employ diverse methodologies when it comes to declaring arrays. Let’s consider the declaration of arrays in the context of the C programming language as an illustrative 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[0] = 1 
Arr[1] = 3 
Arr[2] = 5 
Arr[3] = 7 
Arr[4] = 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[0] = 1 
Arr[1] = 3 
Arr[2] = 5 
Arr[3] = 7 
Arr[4] = 8 
The array elements after insertion:
Arr[0] = 1 
Arr[1] = 3 
Arr[2] = 5 
Arr[3] = 10 
Arr[4] = 7 
Arr[5] = 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[0] = 1 
Arr[1] = 3 
Arr[2] = 5 
Arr[3] = 7 
Arr[4] = 8 
The array elements after deletion:
Arr[0] = 1 
Arr[1] = 3 
Arr[2] = 7 
Arr[3] = 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[0] = 1 
Arr[1] = 3 
Arr[2] = 5 
Arr[3] = 7 
Arr[4] = 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[0] = 1 
Arr[1] = 3 
Arr[2] = 5 
Arr[3] = 7 
Arr[4] = 8 
The array elements after updation:
Arr[0] = 1 
Arr[1] = 3 
Arr[2] = 10 
Arr[3] = 7 
Arr[4] = 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[0]);    
        
    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[0]);    
    //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[0] = 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[0]);    
        
    //Initialize max with first element of array.    
    int max = arr[0];    
        
    //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[0]);    
        
    //Initialize min with first element of array.    
    int min = arr[0];    
        
    //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[0]);    
        
    //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

Conclusion:
In conclusion, arrays serve as fundamental data structures capable of storing elements of the same data type in a sequential manner. They offer versatility in terms of element insertion, removal, and retrieval. Understanding array functions and operations is essential for efficient data manipulation and algorithm implementation.
Now, we will discuss some important array questions that will help in understanding the arrays.

Frequently Asked Questions Related to Array Operations:

Here are some of the FAQs related to Array Operations and Array Function:

1. What is the primary purpose of arrays?
Arrays are used to store a collection of elements, such as integers, characters, or objects, in a contiguous memory block for efficient access and manipulation.

2. Can arrays store elements of different data types?
In most programming languages, arrays can only store elements of the same data type. However, some languages provide advanced data structures like lists or tuples to handle elements of mixed types.

3. How can I insert an element into an array at a specific position?
You can insert an element into an array by shifting the existing elements to accommodate the new element’s position. This process can involve copying elements and updating indices.

4. What is the time complexity for inserting an element into an array?
The time complexity depends on the position of insertion. For inserting at the end (appending), it’s generally O(1). For inserting at the beginning or middle, it can be O(n) due to shifting elements.

5. What are some common array operations?
Common array operations include element insertion, removal, retrieval by index, finding the length of the array, and iteration through its elements.

Leave a Reply

Your email address will not be published. Required fields are marked *