Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Difference between struct and class in terms of access modifier.

1185


What are smart pointers?

2252


What are the advantages of early binding?

1067


Is recursion allowed in inline functions?

1076


declare an array of structure where the members of the structure are integer variable float variable integer array char variable access all elements of the structure using dot operator and this pointer operator

2342


Which one is a preferred language C or C++? Why?

1084


How can a called function determine the number of arguments that have been passed to it?

1130


State the difference between delete and delete[].

1102


What are the advantages of inheritance in c++?

1097


which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left shifting an unsigned int or char by 1?

1176


How is computer programming useful in real life?

1054


Difference between strdup and strcpy?

1200


What is the c++ code?

1136


Explain what are single and multiple inheritances in c++?

1017


What is a literal in c++?

1092