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 cout flush?

563


Explain virtual class and friend class.

595


On throwing an exception by the animal constructor in p = new animalq, can memory leak occur?

673


Can we overload operator in c++?

544


What is pointer with example?

558






what are the iterator and generic algorithms.

1477


What are the main features of c++?

538


Explain the concept of friend function in c++?

597


What is a c++ object?

613


Differentiate between a copy constructor and an overloaded assignment operator.

637


Can you explicitly call a destructor on a local variable?

598


How do you initialize a string in c++?

555


What is the difference between structure and class?

548


What is stl containers in c++?

581


Does c++ have arraylist?

532