Inserting Elements in C Arrays

CCBeginner
Practice Now

Introduction

In C, an array is a collection of similar data types stored in continuous memory locations. It helps to store multiple values under a single variable name, saving memory space and simplifying the code structure. In this lab, we will learn how to insert an element in an array at a specific position.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Creating an Array

In this step, we will create an array with some initial elements and display its values.

#include <stdio.h>

int main() {
    // Define variables and array
    int array[5] = {10, 20, 30, 40, 50};
    int n = 5, i;

    printf("Initial array: ");
    for(i=0; i<n; i++) {
        printf("%d ", array[i]);
    }

    return 0;
}

Taking User Input

In this step, we will take user input for the new value to insert and the index where the value will be inserted.

#include <stdio.h>

int main() {
    // Define variables and array
    int array[5] = {10, 20, 30, 40, 50};
    int n = 5, i, value, position;

    printf("Initial array: ");
    for(i=0; i<n; i++) {
        printf("%d ", array[i]);
    }

    // Take input for new value and position
    printf("\n\nEnter the value to insert: ");
    scanf("%d", &value);

    printf("Enter the position to insert: ");
    scanf("%d", &position);

    return 0;
}

Inserting the Element

In this step, we will insert the new element in the array at the given position. We will shift the existing elements to the right of the position and add the new element at the position index.

#include <stdio.h>

int main() {
    // Define variables and array
    int array[5] = {10, 20, 30, 40, 50};
    int n = 5, i, value, position;

    printf("Initial array: ");
    for(i=0; i<n; i++) {
        printf("%d ", array[i]);
    }

    // Take input for new value and position
    printf("\n\nEnter the value to insert: ");
    scanf("%d", &value);

    printf("Enter the position to insert: ");
    scanf("%d", &position);

    // Shift the existing elements to right of the position
    for(i=n-1; i>=position-1; i--) {
        array[i+1] = array[i];
    }

    // Insert the new element at the position index
    array[position-1] = value;

    return 0;
}

Displaying the Result

In this step, we will display the new array with the inserted element.

#include <stdio.h>

int main() {
    // Define variables and array
    int array[5] = {10, 20, 30, 40, 50};
    int n = 5, i, value, position;

    printf("Initial array: ");
    for(i=0; i<n; i++) {
        printf("%d ", array[i]);
    }

    // Take input for new value and position
    printf("\n\nEnter the value to insert: ");
    scanf("%d", &value);

    printf("Enter the position to insert: ");
    scanf("%d", &position);

    // Shift the existing elements to right of the position
    for(i=n-1; i>=position-1; i--) {
        array[i+1] = array[i];
    }

    // Insert the new element at the position index
    array[position-1] = value;

    // Display the new array
    printf("\n\nNew array with the inserted element: ");
    for(i=0; i<n+1; i++) {
        printf("%d ", array[i]);
    }

    return 0;
}

Final Code

Below is the final code for inserting an element in an array,

#include <stdio.h>

int main() {
    // Define variables and array
    int array[5] = {10, 20, 30, 40, 50};
    int n = 5, i, value, position;

    printf("Initial array: ");
    for(i=0; i<n; i++) {
        printf("%d ", array[i]);
    }

    // Take input for new value and position
    printf("\n\nEnter the value to insert: ");
    scanf("%d", &value);

    printf("Enter the position to insert: ");
    scanf("%d", &position);

    // Shift the existing elements to right of the position
    for(i=n-1; i>=position-1; i--) {
        array[i+1] = array[i];
    }

    // Insert the new element at the position index
    array[position-1] = value;

    // Display the new array
    printf("\n\nNew array with the inserted element: ");
    for(i=0; i<n+1; i++) {
        printf("%d ", array[i]);
    }

    return 0;
}

Summary

In this lab, we learned how to insert an element in an array at a specific position using C. We created an initial array, took user input for the new element and position, inserted the element in the array at the specified position by shifting the existing elements to the right, and displayed the new resulting array.

Other C Tutorials you may like