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


Please Help Members By Posting Answers For Below Questions

What is difference between rand () and srand ()?

596


If dog is a friend of boy, is boy a friend of dog?

575


Do you know the use of vtable?

637


an operation between an integer and real always yeilds a) integer result b) real result c) float result

712


What is c++ and its uses?

624






Write about the role of c++ in the tradeoff of safety vs. Usability?

600


write a function signature with various number of parameters.

567


What does the ios::ate argument do?

665


What is the difference between a type-specific template friend class and a general template friend class?

562


What is abstraction with real time example?

621


Would you rather wait for quicksort, linear search, or bubble sort on a 200000 element array? (Or go to lunch...) a) Quicksort b) Linear Search c) Bubble Sort

641


Tell me difference between constant pointer and pointer to a constant.

632


How is c++ different from java?

561


Which bitwise operator is used to check whether a particular bit is on or off?

592


What is the most useful programming language?

608