Insert and Delete Element in Array in C Using Switch Case

Introduction

This article will explore a program to insert and delete element in an array using switch case statement in C.

It showcases expertise and trust in the topic of inserting and deleting elements in an array in C using the switch case.

In the realm of programming, arrays serve as indispensable data structures that enable us to store and manipulate collections of elements.

Also Read: C Program To Read Two Files Simultaneously

When working with arrays in the C programming language, it often becomes necessary to efficiently insert and delete elements.

One approach to accomplish this is by employing the switch case statement, which provides a concise and structured method to handle various scenarios.

Insertion of Elements in an Array

To insert an element into an array using the switch case statement in C, you can follow these steps:

  1. Declare an array of sufficient size to accommodate the new element.
  2. Prompt the user to enter the position where the element should be inserted.
  3. Use the switch case statement to handle different cases.
  4. If the position is within the valid range, shift the elements to the right from the specified position to make space for the new element.
  5. Prompt the user to enter the value of the new element.
  6. Assign the entered value to the specified position in the array.
  7. Display a message indicating the successful insertion of the element.

Also Read: Armstrong Number in C Programming

Here’s an example implementation of the insertion process using the switch case statement:

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int array[MAX_SIZE];
    int n, pos, value;
    
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    
    printf("Enter the elements:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }
    
    printf("Enter the position where the element should be inserted: ");
    scanf("%d", &pos);
    
    switch (pos) {
        case 1:
            // Insert element at the beginning of the array
            // Shift elements to the right
            for (int i = n - 1; i >= 0; i--) {
                array[i + 1] = array[i];
            }
            
            // Prompt for the value of the new element
            printf("Enter the value of the new element: ");
            scanf("%d", &value);
            
            // Assign the value to the specified position
            array[0] = value;
            
            // Increment the size of the array
            n++;
            
            printf("Element inserted successfully.\n");
            break;
        
        case 2:
            // Insert element at the end of the array
            // Prompt for the value of the new element
            printf("Enter the value of the new element: ");
            scanf("%d", &value);
            
            // Assign the value to the specified position
            array[n] = value;
            
            // Increment the size of the array
            n++;
            
            printf("Element inserted successfully.\n");
            break;
        
        default:
            if (pos >= 3 && pos <= n + 1) {
                // Insert element at the specified position
                // Shift elements to the right
                for (int i = n - 1; i >= pos - 1; i--) {
                    array[i + 1] = array[i];
                }
                
                // Prompt for the value of the new element
                printf("Enter the value of the new element: ");
                scanf("%d", &value);
                
                // Assign the value to the specified position
                array[pos - 1] = value;
                
                // Increment the size of the array
                n++;
                
                printf("Element inserted successfully.\n");
            } else {
                printf("Invalid position.\n");
            }
            break;
    }
    
    printf("Updated array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", array[i]);
    }
    
    return 0;
}

Also Read: C Program to Find the Inverse of 2×2 Matrix

Deletion of Elements in an Array

In a similar fashion, you can delete an element from an array using the switch case statement in C.

Also Read: C Program to Copy the Contents of One File into Another File

Here’s a step-by-step guide:

  1. Prompt the user to enter the position of the element to be deleted.
  2. Use the switch case statement to handle different cases.
  3. If the position is within the valid range, shift the elements to the left from the specified position to overwrite the element.
  4. Decrement the size of the array to reflect the deletion.
  5. Display a message indicating the successful deletion of the element.

Also Read: Getchar and Putchar Function in C with Example

Consider the following code snippet that demonstrates the deletion process:

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int array[MAX_SIZE];
    int n, pos;
    
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    
    printf("Enter the elements:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }
    
    printf("Enter the position of the element to be deleted: ");
    scanf("%d", &pos);
    
    switch (pos) {
        case 1:
            // Delete element from the beginning of the array
            // Shift elements to the left
            for (int i = 0; i < n - 1; i++) {
                array[i] = array[i + 1];
            }
            
            // Decrement the size of the array
            n--;
            
            printf("Element deleted successfully.\n");
            break;
        
        case 2:
            // Delete element from the end of the array
            // Decrement the size of the array
            n--;
            
            printf("Element deleted successfully.\n");
            break;
        
        default:
            if (pos >= 3 && pos <= n) {
                // Delete element from the specified position
                // Shift elements to the left
                for (int i = pos - 1; i < n - 1; i++) {
                    array[i] = array[i + 1];
                }
                
                // Decrement the size of the array
                n--;
                
                printf("Element deleted successfully.\n");
            } else {
                printf("Invalid position.\n");
            }
            break;
    }
    
    printf("Updated array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", array[i]);
    }
    
    return 0;
}

Also Read: C Program to Find the Sum of Cubes of Elements in an Array

FAQs

1. Can I insert an element at any position in an array?

Yes, you can insert an element at any position in an array by shifting the existing elements to the right from the specified position and assigning the new element value to the desired position.

2. What happens to the elements after deletion in an array?

After deleting an element from an array, the remaining elements are shifted to the left to fill the empty position. The size of the array is also decremented accordingly.

3. Is it possible to insert and delete elements in a multidimensional array using switch case?

Yes, it is possible to insert and delete elements in a multidimensional array using switch case. You can apply similar techniques by selecting the appropriate case for the desired dimension and position within that dimension.

4. How do I handle the case when the array is full?

When the array is already full and there is no space to insert an element, you can display an appropriate error message or prompt the user to delete an existing element before performing the insertion.

What are the advantages of using the switch case statement for array manipulation?

The switch case statement provides a structured and readable way to handle different scenarios when manipulating arrays. It allows you to write concise and easily understandable code, making it easier to maintain and debug.

Also Read: 25 Tricky Questions on Pointers in C: Explained and Answered

Conclusion

In this article, we explored c program to insert and delete element in an array using switch case statement.

The process of inserting and deleting element in array using the switch case statement in the C programming language.

Also Read: C Program to Display a String in Capital Letters

By following the steps outlined and understanding the example code provided, you can efficiently manipulate arrays and perform these operations with ease.

Arrays are fundamental data structures, and mastering their manipulation techniques is crucial for any programmer.

Remember to use the switch case statement to handle different cases based on user input and ensure that you validate the positions before performing insertions or deletions.

This way, you can create robust and reliable code that efficiently manages arrays in your C programs.