Introduction
In this tutorial you will learn how to create a program to insert and delete
from an array.
Topic you will covered :-
- How to create an array.
- How to to print an array.
- How to insert in an array.
- How to delete from an array.
So lets start first see the theory and then we make a program applying that
theory and see what output it will give.
Theory
Here our motive is to insert an element and delete an element from array
, so as we are performing an all work on array we need an array for it , right
so we will intialize array first.
And we also required to intialize some more elements for this program like
size of element index, and some more elements. we will intialize that in
first part of program.
Then we will forward towards creating an array, for that we will require size
of an array, that we will get from user so we display a message for user so
that after reading he can enter an array size.
Afterwards we will use a for loop for creating an array and taking an input
from user for its element after entering the element we use another for loop
to print the whole array at once.
Now create a small list for the user in which we will have two options one for
the insertion of an array and second for the deletion of an array then we will
take input of the user as 1 or 2 and perform the user required task.
We use if else statement for taking the input of user that you can see in the
program.
if(1==option)
{size=size+1;
cout<<"where you want to insert an elemnt :" ;
cin>>index;
for(int i= size;i>=index;i--)
{
a[i]=a[i-1];
}
cout<<"enter the element you want to insert";
cin>>a[index];
for(int i=1;i<=size; i++)
{cout<<a[i];}
}
Insertion in an array
As you can see above how we perform an operation of insertion,
Explanation:-
As we have to insert in an array so we will increase its size one ,
Then we ask for where to enter an element ok n an array and take its input and
store it in index.
Now as we insert an array all other elments after that has to move one index
further so that a space can be created for new element.
As we we create Element we flash on screen a message for enter the new element
and we store that in our new space that we created earlier. And that how
insertion is performed.
And then we print the updated array using for loop.
Now time to look for the deletion part
Deletion in an array
else if (2==option)
{cout<<"from where you want to delete element : ";
cin>>index;
for(int i= index;i<=size;i++)
{ a[i]=a[i+1];
}
size=size-1;
for(int i=1;i<=size; i++)
{cout<<a[i];}
In this we have to delete an element so we have to get the element which we
have to ask for it from user. So again we will print a message for
it and store that index.
Now as we are deleting element we have to delete that empty space so what we
do now,
Direct deletion us not possible as array is continus ,so we will shift every
elments towards the empty space. So we remove that space in one or another
way.
As we delete element we have to decrease the size of array.
size = size - 1 ;
Write a program to perform insertion and deletion operation in an array?
Or
Write a program to perform insertion in an array?
Or
Write a program to perform deletion in an array?
Sol:-
#include<iostream.h>
#include<conio.h>
int main()
{
int a[100], size,option,index;
cout<<"enter the number of elment you want to insert: ";
cin>>size;
for(int i=1;i<=size; i++)
{
cout<<"enter elemnt number"<<i<<": ";
cin>> a[i];
}cout<<"your array is as shown below :\n";
for(int i=1;i<=size; i++)
{cout<<a[i];}
cout<<"\n choose the operation to perform on an array:- \n";
cout<<"____________________________\n";
cout<<"1. insert in an array : \n ";
cout<<"2. delete in an array : ";
cin>>option;
if(1==option)
{size=size+1;
cout<<"where you want to insert an elemnt :" ;
cin>>index;
for(int i= size;i>=index;i--)
{
a[i]=a[i-1];
}
cout<<"enter the element you want to insert";
cin>>a[index];
for(int i=1;i<=size; i++)
{cout<<a[i];}
}
else if (2==option)
{cout<<"from where you want to delete element : ";
cin>>index;
for(int i= index;i<=size;i++)
{ a[i]=a[i+1];
}
size=size-1;
for(int i=1;i<=size; i++)
{cout<<a[i];}
}
else { cout<<"not an option";}
return 0;
}
Now see the output for better understanding
Output:-( for insertion in an array)
enter the number of elment you want to insert: 5
enter elemnt number1: 1
enter elemnt number2: 2
enter elemnt number3: 3
enter elemnt number4: 4
enter elemnt number5: 5
your array is as shown below :
12345
choose the operation to perform on an array:-
____________________________
1. insert in an array :
2. delete in an array : 1
where you want to insert an elemnt :2
enter the element you want to insert0
102345
Output:-( deletion in an array)
enter the number of elment you want to insert: 5
enter elemnt number1: 1
enter elemnt number2: 2
enter elemnt number3: 3
enter elemnt number4: 4
enter elemnt number5: 5
your array is as shown below :
12345
choose the operation to perform on an array:-
____________________________
1. insert in an array :
2. delete in an array : 2
from where you want to delete element : 2
1345
I hope the program is clear to you .
Some more questions like this you can find out on our website to check more questions for practice
enjoy the coding with Bigfootcode
0 Comments
if you have any doubt let me know in comment