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 the output of the following program? Why?
What do you mean by “this” pointer?
What is the difference between C and CPP?
What is flush () in c++?
Do you know the problem with overriding functions?
What are inline functions? What is the syntax for defining an inline function?
What is a static element?
Where is atoi defined?
what are the iterator and generic algorithms.
What is the use of namespace std in C++?
Explain how the virtual base class is different from the conventional base classes of the opps.
When the constructor of a base class calls a virtual function, why doesn't the override function of the derived class gets called?
Why do we need pointers?
Explain overriding.
What is oops in c++?