Tuesday, December 8, 2020

Insertion in an array for beginner in c++.

 Insertion in an array.

For example:

Before insertion:  A[] = {10,20,30,40,50,60};

Insert an element = 100 at position index = 3.

After insertion: A[] = {10,20,30,100,40,50,60};


Program:


#include<iostream>
using namespace std;
int main()
{
int A[10] = {10,20,30,40,50,60},position = 3,element = 100;
for(int i=5;i>= position;i--)
{
A[i+1] = A[i];
}
A[position] = element;
for(int i=0;i<7;i++)
{
cout<<A[i]<<endl;
}
return 0;
}

Share this

0 Comment to "Insertion in an array for beginner in c++."

Post a Comment

If you have any doubts then let me know.