Question

C++ Sequential Search and Binary Seach Write a set of code that will include Sequential and...

C++ Sequential Search and Binary Seach

Write a set of code that will include Sequential and Binary Search that will have a set of 25. This solution must include:

seqArray [] = {25 unsorted #s}
binArray [] = {25 sort #s}
while loop repeat = y
{
switch
case 1 seq
case 2 bin

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.

CODE

#include<iostream>

using namespace std;

//function for sequential search
void seq(int array[25],int length)
{
   //variables
   int search,i;
  
   //prompt for search number
   cout<<"Enter a number to search"<<endl;
   cin>>search;
  
   //performing linear search
   for(i=0;i<length;i++)
   {
       //if element found
       if(array[i]==search)
       {
           cout<<search<<" is found at "<<(i+1)<<endl;
           break;
       }
   }
   //if element not found
   if(i==length)
   {
       cout<<search<<" is not found"<<endl;
   }
}

void bin(int array[25],int length)
{
   //variables
   int search,first,last,middle;
  
   //prompt for search number
   cout<<"Enter a number to search"<<endl;
   cin>>search;
  
   //assigns first index as zero
   first=0;
  
   //assigns last index as last position
   last=length-1;
  
   //finding middle index
   middle=(first+last)/2;
  
   //performing binary search
   while(first<=last)
   {
       //if middle element is less than search
       if(array[middle]<search)
       {
           //assigns first index as middle index+1
           first=middle+1;
       }
       //if element found
       else if(array[middle]==search)
       {
           cout<<search<<" is found at "<<(middle+1)<<endl;
           break;
       }
       //if middle element is greater than search
       else
       {
           //assigns last index as middle index-1
           last=middle-1;
       }
       middle=(first+last)/2;
   }
   //if element not found
   if(first>last)
   {
       cout<<search<<" is not found"<<endl;
   }
}

int main()
{
   //unsorted array for sequential search
   int seqArray[]={2,4,3,6,8,9,7,10,12,34,22,21,5,1,25,33,23,24,54,43,26,27,19,17,15};
  
   //sorted array for binary search
   int binArray[]={1,2,3,4,5,6,7,8,9,10,12,15,17,19,21,22,23,24,25,26,27,33,34,43,54};
   char repeat='y';
   int choice,length=25;
  
   //repeat until repeat is y
   while(repeat=='y')
   {
       //menu
       cout<<"1.Sequential Search"<<endl;
       cout<<"2.Binary Search"<<endl;
      
       //inputs choice
       cout<<"Enter your choice :";
       cin>>choice;
       switch(choice)
       {
           //if sequential search selected
           case 1:
               seq(seqArray,length);
               break;
           //if binary search selected
           case 2:
               bin(binArray,length);
               break;
           //if wrong choice selected
           default:
               cout<<"Wrong choice"<<endl;
               break;
       }
       //prompt for continue or not
       cout<<"Do you want to continue? (y/n)"<<endl;
       cin>>repeat;
   }
}

OUTPUT

CODE SCREEN SHOT

Add a comment
Know the answer?
Add Answer to:
C++ Sequential Search and Binary Seach Write a set of code that will include Sequential and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ***PLEASE USE C++ LANGUAGE*** Binary Search of Strings 1. Write a version of the selection sort...

    ***PLEASE USE C++ LANGUAGE*** Binary Search of Strings 1. Write a version of the selection sort algorithm presented in the unit, which is used to search a list of strings. 2. Write a version of the binary search algorithm presented in the unit, which is used to search a list of strings. (Use the selection sort that you designed above to sort the list of strings.) 3. Create a test program that primes the list with a set of strings,...

  • Language = c++ Write a program to find the number of comparisons using the binary search...

    Language = c++ Write a program to find the number of comparisons using the binary search and sequential search algorithms as follows: o Suppose list is an array of 1000 elements. o Use a random number generator to fill the list. o Use the function insertOrd to initially insert all the elements in the list. o You may use the following function to fill the list: void fill(orderedArrayListType& list) {       int seed = 47; int multiplier = 2743;                                ...

  • write a c++ program that conducts a binary search using a template in: a set of...

    write a c++ program that conducts a binary search using a template in: a set of values of type int, a set of values of type float, a set of values of type double and a set of values of type char. Assume that the array may have up to 10 values.

  • In c++ language write the code that moves binary search tree elements into a stack in...

    In c++ language write the code that moves binary search tree elements into a stack in post order but without printing it

  • c++ Let's say sub_root is a node in a given binary search tree. Write a code...

    c++ Let's say sub_root is a node in a given binary search tree. Write a code segment to find the immediate successor of the sub_root

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Write and test a function in C++ that uses the binary search algorithm to search an...

    Write and test a function in C++ that uses the binary search algorithm to search an array of sorted strings – use a do..while loop to allow user to perform multiple searches w/o terminating the program – see sample output below. Use this name array: string names[SIZE] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",         "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",         "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",         "James, Jean", "Weaver, Jim", "Pore, Bob",...

  • pseudo code only no coding in necessary (a) Design a variant "binary" search algorithm which splits...

    pseudo code only no coding in necessary (a) Design a variant "binary" search algorithm which splits the set not into 2 sets of equal sizes (½ and ½), but into 2 sets of sizes one quarter (14) and three quarters (3/4) (b) Give the recurrence relations of your algorithm. (c) How does this algorithm compare with the original binary search in both the best case complexity and the worst case complexity?

  • C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...

    C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT