C++ Write a menu driven program that has at least two functions (searchList and
ID: 3669246 • Letter: C
Question
C++
Write a menu driven program that has at least two functions (searchList and binarySearch) and will allow the user to:
- insert 10 integer items into an array
- choose a search option (1- linear, 2- binary, 0 - exit)
- search the array for a value entered buy the user
- include for option 2- binary search two additional submenus (b- bubble sort, and s – selection sort) that allows the user to choose the sorting method prior to binary search. The sorted array should be displayed and then the user should be allowed to choose a search value
- 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)
- includes input validation for correct menu options
Explanation / Answer
#include<stdio.h>
void lsearch(int [],int);
void bsearch(int [],int);
void sorting();
void selection_sort();
void bubble_sort();
int main()
{
int ch,ch1,m,n;
int a[10],b[10],i;
do
{
printf(" Enter your choice :");
printf(" 1. linear search");
printf(" 2. binary search");
scanf(" %d",&ch);
switch(ch)
{
case 1:printf(" Enter Array size : ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf(" Enter array element : ");
scanf("%d",&a[i]);
}
lsearch(a,m);
break;
//Binary Search
case 2:
void sorting()
{
int ch;
char ch1;
do
{
printf(" Which sorting you want to do ");
printf(" B:Bubble sort ”);
printf(" S:Selection sort ”);
printf(" Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case B:
printf(" Enter limit not more then 10: ");
scanf("%d",&c);
if(c>10)
{
printf(" Wrong Input”);
sorting();}
else
bubble_sort();
break;
case S: printf(" Enter limit not more then 10: ");
scanf("%d",&c);
if(c>10)
{
printf(" Wrong Input”);
sorting();
}
else
selection_sort();
break;
default:
printf(" You have enter wrong choice ");
}
printf(" Do you want to continue:(y/n) ");
scanf("%s",&ch1);
}while(ch1=='y');
}
/*function for bubble sort*/
void bubble_sort()
{
printf(" Enter element in array ");
for(i=0;i<=c;i++)
{
scanf("%d ",&arr[i]);
}
printf(" Tthe unsorted array is: ");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
for(i=0;i<=c;i++)
{
for(j=0;j<=c-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf(" The sorted array is: ");
for(i=0;i<=c;i++)
{
printf("%d ,",arr[i]);
}
bsearch(arr,c);
break;
default: printf(" Wrong input !!! ");
}
printf(" Want to be continue press non zero no : ");
scanf("%d",&ch1);
}
while(ch1!=0);
}
void lsearch(int a[],int m)
{
int num,i,f=0;
printf(" Enter no to be search : ");
scanf("%d",&num);
for(i=0;i<m;i++)
{
if(a[i]==num)
{
printf(" item is found at index %d !!!",i);
f=1;
break;
}
}
if(f==0)
printf(" item is not found !!!");
}
void bsearch(int b[],int n)
{
int beg=0,num,mid,last;
last=n-1;
printf(" Enter no to be search : ");
scanf("%d",&num);
mid=(beg+last)/2;
while(num != b[mid] && beg <= last)
{
if(num > b[mid])
beg=mid+1;
else
last=mid-1;
mid=(beg+last)/2;
}
if(num==b[mid])
printf("%d found at position %d ",num,mid+1);
if(beg>last)
printf("%d not found in array ",num);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.