Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Array - Insertion of an element in linear array at specific position

This program shows how to insert a element in a linear array list at specified position. User needs to enter some values like position, array list elements, new element which will be inserted. After insertion all values will be shown with updated output with new element inserted at specific position.

				
# include<iostream.h>


class insertion
{
private:


public:
int insert_array(char *, int, int, char);
void input(char *, int );
void display(char *, int );
};


// Definition of the function
// number : shows the items in array
// Position : shows the specified position where the element will be insert
// element : element is the value which will be inserted in array


int insertion :: insert_array(char array[], int number,
int position, char element)
{
int temp = number;
while( temp >= position)
{
array[temp+1] = array[temp];
temp --;
}
array[position] = element;
number = number +1 ;
return(number);
}


// For reading the data

void insertion :: input(char array[], int number)
{
for(int i = 1; i<= number ; i++)
{
cout<<"\n Enter value for : "<<i<<" : ";
cin>>array[i];
}
}

// function to Show


void insertion :: display(char array[], int number)
{
for(int i = 1; i<= number; i++)
{
cout<<"\n Element at the position: "<<i<<" : "<<array[i];
}
}





// main function



void main()
{
insertion insert;
int number;
char array[100];
int position;
char element;
cout<<"\n Enter the number of elements in list:";
cin>>number;
insert.input(array, number);
cout<<"\n List elements are:\n";
insert.display(array,number);
cout<<"\n Specify position where you want insert new element:";
cin>>position;
cout<<"\n Enter the element:";
cin>>element;
number = insert.insert_array(array,number,position,element);
insert.display(array,number);
}

Share this article   |    Print    |    Article read by 22664 times
Author:
Rohit kakria
I am software developer, moderator of xpode.com
Related Articles:
Related Interview Questions: No related interview question