C Examples

WAP in C to insert an array into another array at a position specified by user

The Program in C to insert an array into another array at a position specified by user is given below:

#include <stdio.h>

void insertArray(int array1[], int size1, int array2[], int size2, int pos) {
    int i;

    // shift elements of array1 to the right
    for (i = size1 - 1; i >= pos; i--) {
        array1[i + size2] = array1[i];
    }

    // insert elements of array2 into array1
    for (i = 0; i < size2; i++) {
        array1[pos + i] = array2[i];
    }
}

int main() {
    int array1[100], size1, array2[100], size2, pos;
    printf("Enter the size of first array : ");
    scanf("%d", &size1);
    printf("Enter the elements of first array : ");
    for (int i = 0; i < size1; i++) {
        scanf("%d", &array1[i]);
    }
    printf("Enter the size of second array : ");
    scanf("%d", &size2);
    printf("Enter the elements of second array : ");
    for (int i = 0; i < size2; i++) {
        scanf("%d", &array2[i]);
    }
    printf("Enter the position where you want to insert the second array : ");
    scanf("%d", &pos);

    insertArray(array1, size1, array2, size2, pos);

    printf("After insertion : ");
    for (int i = 0; i < size1 + size2; i++) {
        printf("%d ", array1[i]);
    }

    return 0;
}

Output:

Enter the size of first array : 2
Enter the elements of first array : 12
55
Enter the size of second array : 2
Enter the elements of second array : 55
14
Enter the position where you want to insert the second array : 2
After insertion : 12 55 55 14

Pro-Tips💡

  • This program takes input from the user for both arrays and for the position where the second array should be inserted in the first array,
  • and then calls the “insertArray” function which shifts elements of array1 to the right and inserts elements of array2 into array1 at the specified position.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X