write a program to insert an element into an array

Answer Posted / harini

#include<iostream>
#include<conio.h>
main()
{
int a[5],b,n,e,i,j;
std::cout<<"enter the no of elements:";
std::cin>>n;
std::cout<<"enter the array elements:";
for(i=0;i<n;i++)
{
std::cin>>a[i];
}
std::cout<<"enter the position of insertion:";
std::cin>>b;
std::cout<<"enter the element to be inserted:";
std::cin>>e;
for(j=n;j>b;j--)
{
a[j]=a[j-1];
}
a[j]=e;
std::cout<<"the new array elements are:\n";
for(j=0;j<=n;j++)
{
std::cout<<a[j]<<"\n";
}
getch();
}

Is This Answer Correct ?    5 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

When does a name clash occur in c++?

711


What is insertion sorting?

659


How do you add an element to a set in c++?

549


Explain the different access specifiers for the class member in c++.

544


What is std namespace in c++?

715






What are the classes in c++?

636


What do c++ programmers do?

551


What do you mean by vtable and vptr in c++?

619


What are the advantages of using pointers in a program?

677


What is encapsulation in c++ with example?

582


What is type of 'this' pointer? Explain when it is get created?

586


How const int *ourpointer differs from int const *ourpointer?

606


How would you implement a substr() function that extracts a sub string from a given string?

561


What is the extraction operator and what does it do?

608


What is the use of main function in c++?

522