C++ Write a menu driven program that has at least two functions (searchList and
ID: 3667798 • Letter: C
Question
C++
Write a menu driven program that has at least two functions (searchList and binarySearch) and will allow the user to:
1-insert 10 integer items into an array
2-choose a search option (1- linear, 2- binary, 0 - exit)
3-search the array for a value entered buy the user
4-includes input validation for correct menu options
5-allows the linear search to show ALL locations of the search value (this will require you to make some changes to the linear search function)
Menu-driven program: program execution controlled by user selecting from a list of actions Menu: list of choices on the screen Menus can be implemented using if/else if statements or switch statement
The output
CAUsers janias Desktop Visual Studio Projects CSDP222Program21Debug)CSDP222Program2.exe Please enter 10 integers 1 2 2 3 5 6788 8 hoose 1 For lineair hoose 2 For binary search hoose to EXIT Enter the search value8 he value 8 was found at position? he value 8 was found at position 8 he value 8 was found at position 9 hoose 1 For linear search hoose 2: For binary search hoose 0: to EXIT Enterr the search value? he position of value 7 in the array is 6 hoose 1 For lineair hoose 1:For linear search hoose 2: For binary search hoose to EXITExplanation / Answer
#include<iostream>
#include<stdio.h>
using namespace std;
int linearsearch(int a[],int key,int n);
int binarysearch(int a[],int key,int n);
int main()
{
int a[10],k,n,ch,ans,i;
n = 10;
cout<<"Please Enter 10 number : ";
for(i=0;i<n;i++){
cin>>a[i];
}
while(1){
cout<<"1. For linear search ";
cout<<"2. For binary search ";
cout<<"0. to Exit ";
cout<<"Enter your choice ";
cin>>ch;
switch(ch){
case 1:
cout<<"Enter the search value ";
cin>>k;
ans=linearsearch(a,k,n);
if(ans!=-1)
cout<<"the value "<<k<<" is foundat at "<<ans<<" position ";
else
cout<<"Element "<<k<<" is not found ";
break;
case 2:
cout<<"Enter the search value ";
cin>>k;
ans=binarysearch(a,k,n);
if(ans!=-1)
cout<<"the value "<<k<<" is foundat at "<<ans<<" position ";
else
cout<<"Element "<<k<<" not found ";
break;
case 0:
return 0;
default:
cout<<"Invalid entry ";
}
}
}
int linearsearch(int a[20],int key,int n)
{
int i;
for(i=0;i<n;i++){
if(key==a[i])
return i;
}
return -1;
}
int binarysearch(int a[20],int key,int n)
{
int high,low,mid;
low=0;
high=n-1;
while(low<=high){
mid=(low+high)/2;
if(a[mid]==key)
return mid;
else if(a[mid]<key)
low=mid+1;
else
high=mid-1;
}
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.