I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.
#include <iostream>
#define MAX_SIZE 100 //Maximum size of the array
using namespace std;
int main()
{
int arr[100];
int i, num, size, position;
// Reading size and elements of array
cout<<"Enter size of the array :";
cin>>size;
cout<<"Enter elements in array : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
//Reading element to insert & position of the element
cout<<"Enter element to insert : ";
cin>>num;
cout<<"Enter the element position : ";
cin>>position;
//checking elements valis position
if(position>size+1 || position<=0)
{
cout<<"Invalid position! Please enter position between 1 to "<<num;
}
else
{
//Inserting element in an array & increasing the size of the array
for(i=size; i>=position; i--)
{
arr[i] = arr[i-1];
}
arr[position-1] = num;
size++;
// Printing new array with new element
cout<<"Array elements after insertion : ";
for(i=0; i<size; i++)
{
cout<<arr[i]<<"\t";
}
}
return 0;
}
I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.
Result:
Enter size of the array :5
Enter elements in array : 1
2
3
4
5
Enter element to insert : 20
Enter the element position : 2
Array elements after insertion : 1 20 2 3 4 5
need an explanation for this answer? contact us directly to get an explanation for this answer