Menu-Driven Program for Array Operations in C++

Here is a menu-driven program for the following array operations in c++ which are Traversal, insertion, deletion, and linear search.
In this program, we are going to make a menu-driven program that will perform some important array operations. We will make a different function for each of the array operations.

 Here is a menu-driven program for the following array operations in c++:

  1. Traversing of the linear array.
  2. Insertion into a linear array
  3. Deletion of the element from a linear array.
  4. Linear Search in c++.

Menu-Driven Program for Array Operations in C++

#include<iostream.h>
#include<conio.h>
void traversal(int A[],int N);
void insertion(int A[],int &N,int Pos,int ITEM);
int lsearch(int A[],int N,int item);
void deletee(int A[],int &N,int Pos,int ITEM );
void main()
{
clrscr();
int a[10],n,pos,item,choice,delitem;
char ch;
cout<<"\n Enter size:";
cin>>n;
cout<<"\n Enter Array:";
for(int i=0;i<n;i++)
cin>>a[i];
do
{
clrscr();
cout<<"\n \t\t Menu Driven Program";
cout<<"\n 1. Traversal \n 2. Insertion \n 3. Deletion \n 4. Searching";
cout<<"\n Enter your choice:";
cin>>choice;
switch(choice)
{
        case 1: traversal(a,n);
                break;
        case 2: //Insertion
                cout<<"\n Enter Position:";
                cin>>pos;
                cout<<"\nEnter Element:";
                cin>>item;
                insertion(a,n,pos,item);
                break;
        case 3: cout<<"\n Enter Element to be deleted:";
                cin>>delitem;
                pos=lsearch(a,n,delitem);
                if(pos==-1)
                {
                cout<<"\n Element not found";
                }
                deletee(a,n,pos,delitem);
                break;
        case 4: cout<<"\n Enter the element to be searched";
                cin>>item;
                pos=lsearch(a,n,item);
                cout<<"\n Element found at index:"<<pos;
                cout<<"\n Element found at position"<<pos+1;
                break;
        default:cout<<"wrong choice";
        }
        cout<<"\n Do you want to continue?";
        cout<<"(Press y/n)";
        cin>>ch;
        }while(ch=='y');
getch();
}
void traversal(int b[],int N)
{
for(int i=0;i<N;i++)
cout<<b[i]<<" ";
}
// Insertion of element into a linear array with functions in c++
void insertion(int A[],int &N,int Pos,int ITEM)
{
for(int i=N;i>=Pos;i--)
A[i+1]=A[i];
A[Pos]=ITEM;
N=N+1;
}
// Deletion of element from linear array with functions in c++
void deletee(int A[],int &N,int Pos,int ITEM)
{
for(int i=Pos;i<N;i++)
A[i]=A[i+1];
N=N-1;
cout<<ITEM<<" is deleted from the array.";
}
int lsearch(int A[],int N,int ITEM)
{
int index=-1;
for(int i=0;i<N;i++)
{
if(A[i]==ITEM)
{
index=i;
}
}
return index;
}

Related post:
Palindrome Program in C++ using for loop

Output for array operations in c++:

Enter size: 5

Enter array: 1 2 3 4 5 

 Menu Driven Program

1. Traversal 

2. Insertion 

3. Deletion 

4. Searching

Enter your choice: 1

1 2 3 4 5

Do you want to continue? (Press y/n)

n


Pre-requisite:

  1. do-while loop
  2. switch-case
  3. functions

Menu-driven program in c++ using array

In this program, I made a Menu-Driven Program for array operations in c++ together with switch case and do-while loop. I have used functions for each array operations like traversal, insertion, deletion, and linear search

Traversal: It is the process of printing(or visiting) all the elements of the array. I display all the elements of the array using for loop.

Insertion: It is the process of putting a new element in an array. In the insertion process, I asked the user for the position and value of the element to be inserted in the array.

Deletion: It is the process of removing an element from the array.  In deletion, I asked the user about the value of the element to be deleted from the array. Then, I get the position of that element for the purpose of deletion using linear search and then I performed the deletion process on it.

In a linear search, the element to be searched in the array is entered by the user. By comparing that value from the first, second, and all the other elements present in the array. when they are the same, it will return the position of that element.

Don’t forget to use the call by reference method to pass the size of the array else the actual size of the array will not be updated.

“In call by reference method, actual parameters are passed”

About Menu-Driven Program for array operations in c++

This post is written by Siddharth Jha. If you like this post, let me know in the comment section. Please subscribe to our newsletter to stay connected with us and share this post with your friends. 

Related posts:

2 comments

  1. I like your menu driven programs in c++ for 1D array operations.
  2. Thanks
Please do not enter any spam link in the comment box.