Monday, 7 April 2014

Data Structure C++ Code of Array Insertion of Elements

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,item,loc,j;

cout<<"\n------------ INSERT ELEMENT INTO ARRAY ------------ \n\n";

cout<<"Enter No. of Elements : ";
cin>>n;

cout<<"\nEnter Elements : \n";
for(i=0;i<n;i++)
{
cin>>a[i];
}

cout<<"\nEnter Item you want to Insert : ";
cin>>item;

cout<<"\nEnter Location where you want to Insert Item : ";
cin>>loc;

j=n;                             // Initialize Counter

while(j>=loc)
{
a[j+1]=a[j];                     // Move Element Downward
j--;
}

a[loc]=item;                     //Insert Element
n=n+1;

cout<<"\nArray after Insertion : \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
getch();
}

No comments:

Post a Comment