Basic Array Operations in C: Insertion, Deletion, and Traversal

Arrays are a fundamental data structure in programming, and mastering their operations is essential for any beginner. This blog will guide you through the basic operations of insertion, deletion, and traversal in C using a simple example. Let’s dive in!

1. Inserting an Element into an Array

Insertion is the process of adding a new element at a specified position in the array. Here’s how we can achieve this in C.

Example Code

#include <stdio.h>

void insert(int arr[], int *n, int pos, int value) {
    for (int i = *n; i > pos; i--) {
        arr[i] = arr[i - 1];
    }
    arr[pos] = value;
    (*n)++;
}

int main() {
    int arr[10] = {1, 2, 4, 5};
    int n = 4;
    int pos = 2;
    int value = 3;

    insert(arr, &n, pos, value);

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

    return 0;
}

Explanation

  1. Header File Inclusion
   #include <stdio.h>

This line includes the standard input-output header file necessary for using printf and other standard I/O functions.

  1. Insert Function Definition
   void insert(int arr[], int *n, int pos, int value) {
       for (int i = *n; i > pos; i--) {
           arr[i] = arr[i - 1];
       }
       arr[pos] = value;
       (*n)++;
   }
  • The insert function takes four parameters:
    • arr[]: The array where the new value will be inserted.
    • *n: A pointer to an integer representing the current size of the array.
    • pos: The position at which the new value should be inserted.
    • value: The value to be inserted into the array.
  • Inside the function:
    • A loop shifts elements to the right to make space for the new value.
    • The new value is inserted at the specified position.
    • The size of the array is incremented.
  1. Main Function
   int main() {
       int arr[10] = {1, 2, 4, 5};
       int n = 4;
       int pos = 2;
       int value = 3;

       insert(arr, &n, pos, value);

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

       return 0;
   }
  • An array arr of size 10 is initialized with four elements: 1, 2, 4, 5.
  • Variables n, pos, and value are initialized to 4, 2, and 3, respectively.
  • The insert function is called to insert the value 3 at index 2 of the array.
  • A loop prints each element of the array after insertion.

2. Deleting an Element from an Array

Deletion is the process of removing an element from a specified position in the array.

Example Code

#include <stdio.h>

void delete(int arr[], int *n, int pos) {
    for (int i = pos; i < *n - 1; i++) {
        arr[i] = arr[i + 1];
    }
    (*n)--;
}

int main() {
    int arr[10] = {1, 2, 3, 4, 5};
    int n = 5;
    int pos = 2;

    delete(arr, &n, pos);

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

    return 0;
}

Explanation

  1. Delete Function Definition
   void delete(int arr[], int *n, int pos) {
       for (int i = pos; i < *n - 1; i++) {
           arr[i] = arr[i + 1];
       }
       (*n)--;
   }
  • The delete function takes three parameters:
    • arr[]: The array from which the value will be deleted.
    • *n: A pointer to an integer representing the current size of the array.
    • pos: The position of the value to be deleted.
  • Inside the function:
    • A loop shifts elements to the left to fill the gap left by the deleted value.
    • The size of the array is decremented.
  1. Main Function
   int main() {
       int arr[10] = {1, 2, 3, 4, 5};
       int n = 5;
       int pos = 2;

       delete(arr, &n, pos);

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

       return 0;
   }
  • An array arr of size 10 is initialized with five elements: 1, 2, 3, 4, 5.
  • Variables n and pos are initialized to 5 and 2, respectively.
  • The delete function is called to delete the value at index 2 of the array.
  • A loop prints each element of the array after deletion.

3. Traversing an Array

Traversal is the process of accessing each element of the array exactly once.

Example Code

#include <stdio.h>

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

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int n = 5;

    traverse(arr, n);

    return 0;
}

Explanation

  1. Traverse Function Definition
   void traverse(int arr[], int n) {
       for (int i = 0; i < n; i++) {
           printf("%d ", arr[i]);
       }
       printf("\n");
   }
  • The traverse function takes two parameters:
    • arr[]: The array to be traversed.
    • n: The size of the array.
  • Inside the function:
    • A loop prints each element of the array.
  1. Main Function
   int main() {
       int arr[5] = {1, 2, 3, 4, 5};
       int n = 5;

       traverse(arr, n);

       return 0;
   }
  • An array arr of size 5 is initialized with five elements: 1, 2, 3, 4, 5.
  • Variable n is initialized to 5.
  • The traverse function is called to print each element of the array.

Conclusion

By understanding these basic operations, you can efficiently manipulate arrays in C. Practice these operations with different examples to gain confidence. Happy coding!

Leave a Comment

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

Scroll to Top