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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123269{{"`Inserting Elements in C Arrays`"}} c/comments -.-> lab-123269{{"`Inserting Elements in C Arrays`"}} c/variables -.-> lab-123269{{"`Inserting Elements in C Arrays`"}} c/data_types -.-> lab-123269{{"`Inserting Elements in C Arrays`"}} c/operators -.-> lab-123269{{"`Inserting Elements in C Arrays`"}} c/for_loop -.-> lab-123269{{"`Inserting Elements in C Arrays`"}} c/user_input -.-> lab-123269{{"`Inserting Elements in C Arrays`"}} c/memory_address -.-> lab-123269{{"`Inserting Elements in C Arrays`"}} c/function_parameters -.-> lab-123269{{"`Inserting Elements in C Arrays`"}} c/function_declaration -.-> lab-123269{{"`Inserting Elements in C Arrays`"}} end

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