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

Ask#include <iostream> #include <fstream> #include <string> #include <cstring> #

ID: 3659039 • Letter: A

Question

Ask#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>

#include "lab_23_head.h"

using namespace std;

int main( )
{
const int SIZE=20; //list or array size
int list[SIZE]={101, 142, 147, 189, 199, 207, 222, 234, 289, 296,
310, 319, 388, 394, 417, 429, 447, 521, 536, 600};
int value; //target value
int count; //comparisons of performance metric
int pos; //position of the target if found or -1
char choice; //flag for yes or no choice

//show list
showList(list, SIZE);

//loop to play search again and again
do
{
//get a target value
cout<<"Enter a target value to for linear search: ";
cin>>value;

//linear search
pos = binarySearch(list, value, count, SIZE);

if (pos == -1)
cout<< "The target "<<value <<" is not in the list"<<endl;
else
cout<< "The target "<<value <<" is in the list at position "<<pos<<endl;
cout<<" The number of comparions is " <<count <<endl;

//get a choice
cout<<"Want to play again (y/n)? => ";
cin>>choice;
}while (toupper(choice) == 'Y');


/******************************************************************
This part is for you: You shall follow the above approaches to
implement linear search for a list of words. That is, you need to
implement the following two functions:
(1) binaryWordSearch(...)
(2) showWordList(...)
and have a similar loop to test your implementation.
******************************************************************/

***********Need help completing the code here*************

//well done and exit
return 0;
}

#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

#ifndef LAB_23_HEAD_H
#define LAB_23_HEAD_H



/***********************************************************************
This function does linear search to see if the target vaule is in the
list or not. If yes, it returns the position of the target in the list
otherwise it returns -1.
Note: count is an output paramter to record the number of comparisons
during the search
***********************************************************************/
int binarySearch(const int list[], const int value, int & count, const int SIZE)
{
//working vars
int first =0, //beginning of the search range
last = SIZE-1, //end of the search range;
middle, //middle of the search range
pos = -1; //position for the target or -1 if not in the list
bool found =false; //flag for being found

//set initial value for count
count = 0;

while (!found && first <=last)
{
count++; //count comparisons

middle =(first + last)/2; //middle range for binary division
if(list[middle]==value) //found
{
found = true;
pos = middle;
}
else if (list[middle] > value) //search the left half
last = middle - 1;
else //search the right half
first = middle + 1;
}

return pos;
}

/************************************************************************
This function print all numbers in the list.
************************************************************************/
void showList(const int list[], const int SIZE)
{
cout<<"The list is ..... " <<endl;
for (int i=0; i<SIZE; i++)
{
cout<<setw(8)<<right<<list[i];
if ((i+1)==0)
cout<<endl;
}
cout<<endl;

}

#endif

Explanation / Answer

Sorry there is simple mistake in your code here.......for example you did not mention what type of variable "middle" is !!

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