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
What is cout flush?
Explain virtual class and friend class.
On throwing an exception by the animal constructor in p = new animalq, can memory leak occur?
Can we overload operator in c++?
What is pointer with example?
what are the iterator and generic algorithms.
What are the main features of c++?
Explain the concept of friend function in c++?
What is a c++ object?
Differentiate between a copy constructor and an overloaded assignment operator.
Can you explicitly call a destructor on a local variable?
How do you initialize a string in c++?
What is the difference between structure and class?
What is stl containers in c++?
Does c++ have arraylist?