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

C++ no pointers. must print somewhat close to example output Create a program th

ID: 3828082 • Letter: C

Question

C++ no pointers. must print somewhat close to example output

Create a program that has two methods. Each method will pass in an array, the number of elements in the array,
the search item, a number representing the number of times the array was searched and the index of the found element with
the array. One method will employ either a linear search, another method will employ a binary search.   
Each method will keep a count of the number of times the array had to be searched. In main, after calling each method,
the program will print:

Type of Search (linear or Binary), the number of times the array was search, and the index of the element you search for in
the array or a -1 (indicating the element was not in the array)

exmaple output:

Linear search The array was searched 10 times and the element (55) was found at the array index 7.

Binary search - the array was searched 5 times and the element (66) was not in the array.

Prototype for each method

void LinearSearch (int array1[ ], int numelements, int searchitem,int &timesearched, int &arrayindex);

void BinSearch (int array2[ ], int numelements,int searchitem, int &timesearched, int &arrayindex);

Explanation / Answer

/*
#Creating single array in main function
#First enrter its size
#second enter elements of array
#same array will passed on to both the functions depending upon user's choice
#switch condition is applied within do while loop to provide user the ability search as many elements as many times they want to, using any function
*/

#include <iostream>
#include <conio.h>
using namespace std;

void LinearSearch(int array1[], int numelements, int searchitem, int &timesearched, int &arrayindex);
void BinSearch(int array2[], int numelements, int searchitem, int &timesearched, int &arrayindex);

int main()
{
   int array[100], i, size, j, choice, item, linearTime, binaryTime, lIndex, bIndex;
   linearTime = 0;       // number of times array searched, Linear Search
   binaryTime = 0;       // number of times array searched, Binary Search
   lIndex = 0;           // Index of element, Linear Search
   bIndex = 0;           // Index of element, Binary Search

   cout << " Enter size of the array containing all the elements ";
   cin >> size;
   cout << " Enter the elemets of the array ";
   for (i = 0; i < size; i++)
   {
       cin >> array[i];
       cout << " ";
   }

   j = 1;
   do {
       cout << " Select technique you want search element with 1. Linear Search 2. Binary Search ";
       cin >> choice;
       cout << " Enter the element to be searched ";
       cin >> item;

       switch (choice)
       {
       case 1: LinearSearch(array, size, item, linearTime, lIndex);
           break;
       case 2: BinSearch(array, size, item, binaryTime, bIndex);
           break;
       default: cout << " Wrong input!!! ";
           break;
       }

       cout << " Do you want to search more items -- YES (press 1), No (Press 2) ";
       cin >> j;
   } while (j == 1);

   _getch();
return 0;
}


void LinearSearch(int array1[], int numelements, int searchitem, int &timesearched, int &arrayindex)
{  
   int flag = 0;
   timesearched++;       //Keeping count how many times array1 was accessed, from beginning of program
   int i;
   for (i = 0; i < numelements; i++)
   {
       if (array1[i] == searchitem)
       {
           arrayindex = i;
           cout << " Linear Search The array was searched " << timesearched << " times and the element ( " << searchitem << " )was found at the array index " << arrayindex <<". ";
           flag = 1;
           break;
       }
   }
   if (flag == 0)
       cout << " element not found ";
}

void BinSearch(int array2[], int numelements, int searchitem, int &timesearched, int &arrayindex)
{
   int i, first, last, middle, flag = 0;
   timesearched++;       ////Keeping count how many times array2 was accessed, from beginning of program

   first = 0;
   last = numelements - 1;
   middle = (first + last) / 2;
   while (first <= last)
   {
       if (array2[middle] < searchitem)
       {
           first = middle + 1;

       }
       else if (array2[middle] == searchitem)
       { arrayindex = middle; // this line was missing, sorry while pasting answer earlier it was left.
           cout << "Binary search - the array was searched " << timesearched <<" times and the element ( "<< searchitem << " ) was found at the array index " << arrayindex << ". ";
           break;
       }
       else
       {
           last = middle - 1;
       }
       middle = (first + last) / 2;
   }
   if (first > last)
   {
       cout << "Element Not found! ";
   }
}

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