Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ to implement two different searching techniques: - Linear

ID: 3814743 • Letter: W

Question

Write a program in C++ to implement two different searching techniques:

- Linear search

- Binary search

on a set of integer values stored in a single dimension array. The user will provide as input the set of the integer values and the number they did like to search for, and the option for the search technique they did like to use. Based on these three inputs provided by the user, your program will output a success prompt and the array index at which the value was found; if the search was successful. Or it should prompt a "not found" message and an option to re-try the search. Your program must be written using functions (worth 10 points).

Input case: 7,2,5,67,89,11,23,6,18,43

option: Linear Search

Search value: 16

option: Binary Search

Search Value: 11

Explanation / Answer

PROGRAM CODE:

/*
* searchTechniques.cpp
*
* Created on: 10-Apr-2017
* Author: kasturi
*/


#include <iostream>
#include <iomanip>
using namespace std;


void linearSearch(int array[], int size, int number)
{
   for(int i=0; i<size; i++)
   {
       if(array[i]==number)
       {
           cout<<endl<<number<< " was found at index "<<i<<endl;
           return;
       }
   }
   cout<<endl<<number <<" not found"<<endl;
}

void binarySearch(int array[], int size, int number)
{
   int first = 0, last = size-1, middle = (first+last)/2;
   while (first <= last)
   {
       if(array[middle] < number)
       {
           first = middle + 1;

       }
       else if(array[middle] == number)
       {
           cout<<endl<<number<< " was found at index "<<middle<<endl;
           return;
       }
       else
       {
           last = middle - 1;
       }
       middle = (first + last)/2;
   }
   if(first > last)
       cout<<endl<<number <<" not found"<<endl;
}
int main()
{
   int array[100], value, number;
   int counter = 0;
   string option;
   while(true)
   {
       cout<<"Enter the input value(-1 to stop): ";
       cin>>value;
       if(value==-1)
           break;
       else
       array[counter] = value;
       counter++;
   }
   cout<<"Enter search option: ";
   cin.ignore();
   getline( cin, option);
   cout<<"Enter the search value: ";
   cin>>number;

   cout<<" Array Values are: "<<array[0];
   for(int i=1; i<counter; i++)
       cout<<","<<array[i];
   if(option == "binary search" || option == "Binary Search")
       binarySearch(array, counter, number);
   else if(option == "linear search" || option == "Linear Search")
       linearSearch(array, counter, number);
   return 0;
}

OUTPUT:

Enter the input value(-1 to stop): 11

Enter the input value(-1 to stop): 22

Enter the input value(-1 to stop): 33

Enter the input value(-1 to stop): 44

Enter the input value(-1 to stop): 55

Enter the input value(-1 to stop): 66

Enter the input value(-1 to stop): 77

Enter the input value(-1 to stop): 88

Enter the input value(-1 to stop): 99

Enter the input value(-1 to stop): -1

Enter search option: binary search

Enter the search value: 11

Array Values are: 11,22,33,44,55,66,77,88,99

11 was found at index 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote