Task: Write a menu driven program with the menu options: 1. Insert Numbers 2. Ed
ID: 3712246 • Letter: T
Question
Task: Write a menu driven program with the menu options:
1. Insert Numbers
2. Edit Numbers
3. Search Number
4. Print Numbers
5. Current No. of Records
6. Exit
Program should be implemented with arrays, pointers and functions. Maximum 50 numbers
are allowed by the user to enter. Each menu option should be implemented in separate
function.
Some more details about each menu option:
1. Insert Numbers
Keep asking user to add number into array one by one until user enters 0 as a number (which
means that 0 is not allowed to enter as a number). If user already added numbers before then
add numbers at the end of the array. When
maximum limit reaches then inform user about that
and do not allow entering any more numbers.
2. Edit Numbers
Ask position in array at which user want to edit the number and also ask which number user
want to replace with. When there is no number has been entered by the user at that location
then just replies with a message that the specified position is empty.
3. Search Number
Ask user to enter a number to search from the added numbers (means in array). If number is
found then tell the position of the number in the array. When the number exists at multiple
positions in array then tell no. of occurrences and their respective positions. When number is
not found then reply with a message that number does not exists in array.
4. Print Numbers
Print all numbers entered by user and their respective locations.
5. Current No. of Records
Display total numbers entered so far by the user.
6. Exit
Exit program
Explanation / Answer
PROGRAM
#include<iostream>
#include<cstdlib>
#include<malloc.h>
using namespace std;
#define MAX 50 // Define constant MAX=50
// Create and implement menu() function
int menu()
{
int ch; // declare integer varaible
// display menu elements
cout<<" 1. Insert Numbers 2. Edit Numbers 3. Search Number 4. Print Numbers 5. Current No. of Records 6.Exit Enter your Choice: ";
cin>>ch; // read choice
return ch; // return choice value
}
// Create and implement insertNumbers() function with pointer parameter *a
int insertNumbers(int *a)
{
// declare and initialize integer variables
int num;
int count=0,flag=0;
for(int i=0;i<MAX;i++) // create for loop until MAX elements
{
if(i==MAX-1) // check condition MAX is reached then terminate for loop
{
flag=1; break;}
cout<<" Enter Inserting Number[Type '0' to Stop]: ";
cin>>num; // reading array element
if(num==0) // check condition num equal to 0 then terminate loop
break;
a[i]=num; // assign inserting element into array a
count++; // counting number of elements are inserted
}
if(flag==1) // if MAX element reached then display following message
{
cout<<" Array Out of Bounds.... ";
}
return count; // return total number of elements in the array
}
// Create and implement for searchNumber() fuction
int searchNumber(int *a,int key,int size)
{
int flag=0,pos=0; // Initialize integer variables 0
for(int i=0;i<size;i++) // create for loop until size of array elements
{
if(a[i]==key) // check linear search of the element is true
{
flag=1; pos=i+1; // then, flag=1 and pos=i+1 value
break; // terminate for loop
}
else{
flag=0; // else, flag=0 and pos=-1 value
pos=-1;
}
}
if(flag==1) // if flag=1 then display element is found else not found
cout<<key<<" Element is Found... ";
else
cout<<key<<" Element Not Found... ";
return pos; // return position value
}
// Create and implement noOfRecords() function
int noOfRecords(int *a,int size)
{
int count=0; // declre integer variable count for counting number elements in the array
for(int i=0;i<size;i++) // create for loop until size of an array
count++; // counting number of records
return count; // return total records in the array
}
// Create and implement editNumbers() function
void editNumbers(int *a,int size)
{
int idx,flag=0,i=0; // Declare and initialize integer varaibles
cout<<" Enter Index Number to Edit Value: ";
cin>>idx; // read index value
if(idx>size||idx<0) // check condtion not in between 0 to size of an array then, display message and recusive call editNumbers()
{
cout<<" Invalid index Number ";
editNumbers(a,size-1); // recursive calling editNumbers() function
}
else {
cout<<" Enter the new value for Index Array: ";
cin>>a[idx]; // else, enter new value into specific position
cout<<" Array Edited... Successfully "<<endl;
}
}
// Create and implement printNumbers() function
void printNumbers(int *a,int size)
{
cout<<" The Given Array contains ";
for(int i=0;i<size;i++) // Create for loop until size of an array
cout<<a[i]<<" "; // display array elements
}
int main()
{
int a[MAX],search; // declare variables
int s;
while(1) // create infinity while loop
{
int ch=menu(); // calling menu() function
switch(ch)
{
case 1: s=insertNumbers(a); break; // calling insertNumbers() function and return size of an array
case 2: editNumbers(a,s); break; // calling editNumbers() fucntion
case 3:{
cout<<" Enter Searching Element: ";
cin>>search;
int k=searchNumber(a,search,s); // calling searchNumber() function and return position
cout<<" Posistion is: "<<k<<endl;
break;
}
case 4: printNumbers(a,s); break; // calling printNumbers() function
case 5: {
int count=noOfRecords(a,s); // calling noOfRecords() function and return total reocrds
cout<<" Number of Records in Array: "<<count<<endl;
break;
}
case 6: cout<<" Thank You ";exit(0);
default: cout<<" You Enter Wrong Choice... ";
}
}
return 0;
}
OUTPUT
1. Insert Numbers
2. Edit Numbers
3. Search Number
4. Print Numbers
5. Current No. of Records
6.Exit
Enter your Choice: 1
Enter Inserting Number[Type '0' to Stop]: 55
Enter Inserting Number[Type '0' to Stop]: 44
Enter Inserting Number[Type '0' to Stop]: 66
Enter Inserting Number[Type '0' to Stop]: 2
Enter Inserting Number[Type '0' to Stop]: 88
Enter Inserting Number[Type '0' to Stop]: 9
Enter Inserting Number[Type '0' to Stop]: 78
Enter Inserting Number[Type '0' to Stop]: 0
1. Insert Numbers
2. Edit Numbers
3. Search Number
4. Print Numbers
5. Current No. of Records
6.Exit
Enter your Choice: 4
The Given Array contains
55 44 66 2 88 9 78
1. Insert Numbers
2. Edit Numbers
3. Search Number
4. Print Numbers
5. Current No. of Records
6.Exit
Enter your Choice: 2
Enter Index Number to Edit Value: 3
Enter the new value for Index Array: 999
Array Edited... Successfully
1. Insert Numbers
2. Edit Numbers
3. Search Number
4. Print Numbers
5. Current No. of Records
6.Exit
Enter your Choice: 4
The Given Array contains
55 44 66 999 88 9 78
1. Insert Numbers
2. Edit Numbers
3. Search Number
4. Print Numbers
5. Current No. of Records
6.Exit
Enter your Choice: 3
Enter Searching Element: 88
88 Element is Found...
Posistion is: 5
1. Insert Numbers
2. Edit Numbers
3. Search Number
4. Print Numbers
5. Current No. of Records
6.Exit
Enter your Choice: 5
Number of Records in Array: 7
1. Insert Numbers
2. Edit Numbers
3. Search Number
4. Print Numbers
5. Current No. of Records
6.Exit
Enter your Choice: 6
Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.