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 use of function pointer?
Give the difference between the type casting and automatic type conversion. Also tell a suitable C++ code to illustrate both.
What is constructor and destructor in c++?
What are files in c++?
What is == in programming?
In which situation the program terminates before reaching the breakpoint set by the user at the beginning of the mainq method?
What is struct c++?
Explain how the virtual base class is different from the conventional base classes of the opps.
Can you Mention some Application of C/C++?
What are arrays c++?
What is rtti in c++?
What is an iterator class in c++?
How is modularity introduced in C++?
When does a name clash occur in c++?
What is pointer to member?