write a program to insert an element into an array

Answer Posted / prasoon

#include<iostream.h>
#include<conio.h>
void main()
{
int *p,n,item,pos;
clrscr();
cout<<"\nEnter the number of elements in the array: ";
cin>>n;
p=new int[n+1];
cout<<"\nEnter the elements: \n";
for(int i=0;i<n;i++)
cin>>p[i];
cout<<"\nThe entered elements are: \n\n";
for(i=0;i<n;i++)
cout<<p[i]<<"\t";
cout<<"\nEnter the item to be inserted: ";
cin>>item;
cout<<"\nEnter its position: ";
cin>>pos;
for(i=n;i>=pos;i--)
p[i]=p[i-1];
p[pos-1]=item;
n++;
cout.flush();
cout<<"\nThe modified array is:\n\n";
for(i=0;i<n;i++)
cout<<p[i]<<"\t";
delete p;
getch();
}

Is This Answer Correct ?    162 Yes 51 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is buffering in c++?

580


What are mutator methods in c++?

641


How do you establish an is-a relationship?

616


How do you print a string on the printer?

571


How to allocate memory dynamically for a reference?

542






Define 'std'.

602


How many static variables are created if you put one static member into a template class definition?

565


Can we run c program in turbo c++?

581


What is a type library?

684


What does new return if there is insufficient memory to make your new object?

580


What is the standard template library (stl)?

621


What is the difference between global variables and local variable

530


What is the advantage of c++ over c?

556


What do you mean by storage classes?

790


Can we declare a base-class destructor as virtual?

582