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 buffering in c++?
What are mutator methods in c++?
How do you establish an is-a relationship?
How do you print a string on the printer?
How to allocate memory dynamically for a reference?
Define 'std'.
How many static variables are created if you put one static member into a template class definition?
Can we run c program in turbo c++?
What is a type library?
What does new return if there is insufficient memory to make your new object?
What is the standard template library (stl)?
What is the difference between global variables and local variable
What is the advantage of c++ over c?
What do you mean by storage classes?
Can we declare a base-class destructor as virtual?