write a program to insert an element into an array
Answer Posted / kingkill
// The Process of adding new element into an array is known as insertion. Array can be inserted in the beginning or the end if the array is unsorted.
//Insertion can only be performed in an array if the memory space initially allocated is not full, i.e the number of elements in the array is less then the size of the array.
#include <iostream>
using namespace std;
#define MAX 20
int main ()
{
int A[MAX],i,pos,j,size,item;
cout<<" Enter the number of elements in the array : ";
cin>>size;
cout<<" Array size defined is "<<size<<"\n";
if(size>MAX) // Checks the array size ( defined )
{
cout<<" The Maximum Size is 20 \n";
}
cout<< " Enter the elements in sorted order: \n";
for(i=0;i<size;i++)// Elements inserted equal to size
{
cin>>A[i];
}
cout<<" Enter the element to be inserted : ";
cin>>item;
if(size==MAX) // Checks if free array space is free
{
cout<<"The Aray is Full \n";
}
for(i=0;i<size;i++)
{
if(item<A[i])
{
pos=i;
break;
}
}
if(i==size)
pos=size;
for(j=size;j>pos;j--)
A[j]=A[j-1];
A[j]=item;
size++;
cout<<" Array elements after insertion : ";
for(i=0;i<size;i++)
{
cout<<A[i];
}
cout<<"\n";
return 0;
| Is This Answer Correct ? | 1 Yes | 4 No |
Post New Answer View All Answers
What is a manipulative person?
What is the use of object in c++?
What is c++ vb?
Discuss the possibilities related to the termination of a program before entering the mainq method?
What is the use of setprecision in c++?
How does a C++ structure differ from a C++ class?
What is & in c++ function?
What is constructor c++?
What are mutator methods in c++?
Are vectors passed by reference c++?
What do you mean by overhead in c++?
Explain the volatile and mutable keywords.
Ask to write virtual base class code?
On throwing an exception by the animal constructor in p = new animalq, can memory leak occur?
Write a corrected statement in c++ so that the statement will work properly. if (4 < x < 11) y=2*x;