Saturday, August 27, 2011

How to insert a new element in array using C++.


//Program to insert a new element in array using C++.
#include<iostream.h>
#include<conio.h>
void main()
{
 int i, k, j, item, data[50], n;
 clrscr();
 cout<<endl<<"Enter the number of elements in the array:";
 cin>>n;
 cout<<endl<<"Enter the value of "<<n<<" elements:";
 for(i=0; i<n; i++)
 {
   cin>>data[i];
 }
 cout<<endl<<" Enter the location of item:";
 cin>>k;
 cout<<endl<<"Enter the value of item:";
 cin>>item;
 for(j=n-1; j>=k; j--)
 {
  data[j+1]=data[j];
 }
 data[k]=item;
 n=n+1;
 for(i=0;i<n;i++)
 {
  cout<<endl<<i<<" element is:"<<data[i];
 }
 getch();
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More