Program to insert an element in an array at a specific position in C++
In this tutorial, we will try to learn about the program of insert an element in an array at a specific position in C++ (C Plus Plus, CPP). Step 1: First of all, we need to enter the total number of elements we want to insert. Step 2: Enter values in elements. Step 3: Select the location where you want to insert the values in the elements. Step 4: Enter the value to insert into the element. Step 5: Display result values.Program of the insert of an element in an array at a specific position in C++ (C Plus Plus, CPP)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> using namespace std; int main() { int array[100], position, c, n, value; cout<<"Enter number of elements in array\n"<<endl; cin>>n; cout<<"Enter elements\n"<<endl; for (c = 0; c < n; c++) { cin>>array[c]; } cout<<"Enter the location where you wish to insert an element\n"<<endl; cin>>position; cout<<"Enter the value to insert\n"<<endl; cin>>value; for (c = n - 1; c >= position - 1; c--) { array[c+1] = array[c]; } array[position-1] = value; cout<<"Resultant array is\n"<<endl; for (c = 0; c <= n; c++) { cout<<array[c]; } return 0; } |











Video Lecture full of animations: Don’t Miss it for better Understanding
How it will works?
Now, let’s see one another example;Program of the insert of the element in an array at a specific position in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int main() { int array[100], position, c, n, value; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the location where you wish to insert an element\n"); scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for (c = n - 1; c >= position - 1; c--) array[c+1] = array[c]; array[position-1] = value; printf("Resultant array is\n"); for (c = 0; c <= n; c++) printf("%d\n", array[c]); return 0; } |

C Program to insert an element into the beginning of array
Algorithm of insert an element into the beginning of array
begin IF NUMBER = MAXIMUM, return ELSE NUMBER = NUMBER + 1 For All Elements in A Move to next adjacent location T4TUTORIALS[FIRST] = New_Element end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> #include <stdio.h> #define MAXIMUM 5 int main() { int T4Tutorials[MAXIMUM] = {6, 2, 1, 7}; int N = 4; // number of elements in array int i = 0; // loop variable int value = 1; // new data element to be stored in array // print array before insertion printf("Printing array before insertion -\n"); for(i = 0; i < N; i++) { printf("T4Tutorials[%d] = %d \n", i, T4Tutorials[i]); } // now shift rest of the elements downwards for(i = N; i >= 0; i--) { T4Tutorials[i+1] = T4Tutorials[i]; } // add new element at first position T4Tutorials[0] = value; // increase N to reflect number of elements N++; // print to confirm printf("Printing array after insertion -\n"); for(i = 0; i < N; i++) { printf("T4Tutorials[%d] = %d\n", i, T4Tutorials[i]); } } |
C Program to insert an Element in an Array using While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <stdio.h> int main() { int T4Tutorials[10], Location, i, TotalElements, Value; printf("\nHello! Please Enter TotalElements of elements\n"); scanf("%d", &TotalElements); printf("\nHello! Please Enter %d elements \n", TotalElements); for (i = 0; i < TotalElements; i++) { scanf("%d", &T4Tutorials[i]); } printf("\nHello! Please Enter the location of a Element you want to insert\n"); scanf("%d", &Location); printf("\nHello! Please Enter the value to insert\n"); scanf("%d", &Value); i = TotalElements - 1; while(i >= Location - 1) { T4Tutorials[i+1] = T4Tutorials[i]; i--; } T4Tutorials[Location-1] = Value; printf("\n Final after Inserting an Element is:\n"); for (i = 0; i <= TotalElements; i++) { printf("%d\t", T4Tutorials[i]); } return 0; } |
C Program to insert an element Before the Given Index of an Array
Algorithm of insert an element Before the Given Index of an Array
begin IF NUMBER = MAXIMUM, return ELSE NUMBER = NUMBER + 1 SEEK Location index For All Elements from T4TUTORIALS[index – 1] to T4TUTORIALS [NUMBER] Move to next adjacent location T4TUTORIALS [index – 1] = New_Element end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> #include <stdio.h> #define MAXIMUM 5 int main() { int T4Tutorials[MAXIMUM] = {6, 2, 1, 7}; int N = 4; // number of elements in array int i = 0; // loop variable int index = 3; // index location before which value will be inserted int value = 9; // new data element to be inserted // print array before insertion printf("Printing array before insertion -\n"); for(i = 0; i < N; i++) { printf("T4Tutorials[%d] = %d \n", i, T4Tutorials[i]); } // now shift rest of the elements downwards for(i = N; i >= index + 1; i--) { T4Tutorials[i + 1] = T4Tutorials[i]; } // add new element at first position T4Tutorials[index + 1] = value; // increase N to reflect number of elements N++; // print to confirm printf("Printing array after insertion -\n"); for(i = 0; i < N; i++) { printf("T4Tutorials[%d] = %d\n", i, T4Tutorials[i]); } } |
