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 a breakpoint?
In which situation the program terminates before reaching the breakpoint set by the user at the beginning of the mainq method?
What is the difference between strcpy() and strncpy()?
Is multimap sorted c++?
What is virtual function? Explain with an example
Define upcasting.
By using c++ with an example describe linked list?
Is swift a good first language?
What is dev c++ used for?
What is the best c++ book?
Write a program which uses functions like strcmp(), strcpy()? etc
Which c++ operator cannot overload?
What are the advantages of early binding?
In int main(int argc, char *argv[]) what is argv[0] a) The first argument passed into the program b) The program name c) You can't define main like that
What is a syntax in c++?