Question

In: Computer Science

// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int...

  1. // This program demonstrates a Binary Search

    //PLACE YOUR NAME HERE

    #include<iostream>

    using namespace std;

    int binarySearch(int [], int, int);  // function prototype

    const int SIZE = 16;

    int main()

    {

    int found, value;

    int array[] = {34,19,19,18,17,13,12,12,12,11,9,5,3,2,2,0}; // array to be searched

    cout << "Enter an integer to search for:" << endl;

    cin >> value;

    found = binarySearch(array, SIZE, value); //function call to perform the binary search

      //on array looking for an occurrence of value

    if (found == -1)

    cout << "The value " << value << " is not in the list" << endl;

    else

    {

    cout << "The value " << value << " is in position number "

      << found + 1 << " of the list" << endl;

    }

    return 0;

    }

    //*******************************************************************

    //     binarySearch

    //

    // task:   This searches an array for a particular value

    // data in:     List of values in an orderd array, the number of

    //   elements in the array, and the value searched for

    //   in the array

    // data returned: Position in the array of the value or -1 if value

    //   not found

    //

    //*******************************************************************

    int binarySearch(int array[],int numElems,int value) //function heading

    {

    int first = 0;     // First element of list

    int last = numElems - 1;     // last element of the list

    int middle; 0   // variable containing the current

      // middle value of the list

    while (first <= last)

    {

    middle = first + (last - first) / 2;

      // write statement to determine if value is in the middle, then we are done

    else if (array[middle]<value)

    last = //what would the last be?   // toss out the second remaining half of

      // the array and search the first

    else

    first = //what would the first be?   // toss out the first remaining half of

      // the array and search the second

    }

    return -1;   // indicates that value is not in the array

    }

Solutions

Expert Solution

Source Code:

Output:

Code in text format (See above images of code for indentation):

/*his program demonstrates a Binary Search*/
/*PLACE YOUR NAME HERE*/
#include<iostream>
using namespace std;
/*function prototype*/
int binarySearch(int [], int, int);
/*size is fixed*/
const int SIZE=16;
/*main function*/
int main()
{
   /*variables*/
   int found, value;
   /*array to be searched*/
   int array[]={34,19,19,18,17,13,12,12,12,11,9,5,3,2,2,0};
   /*read search key from the user*/
   cout << "Enter an integer to search for:" << endl;
   cin >> value;
   /*function call to perform the binary search
   on array looking for an occurrence of value*/
   found = binarySearch(array, SIZE, value);
   /*if value is not found*/
   if (found == -1)
   cout << "The value " << value << " is not in the list" << endl;
   /*if value is found*/
   else
   {
       /*print index*/
   cout << "The value " << value << " is in position number "
   << found + 1 << " of the list" << endl;
   }
   return 0;
}
/*function definition*/
int binarySearch(int array[],int numElems,int value)
{
   /* First element of list*/
   int first=0;
   /* last element of the list*/
   int last=numElems-1;
   /*variable containing the current middle value of the list*/
   int middle=0;   
   while (first<=last)
   {
       /*find middle*/
       middle=first + (last - first) / 2;
       /*if found*/
       if(array[middle]==value)
       {
           /*return index*/
           return middle;
           break;
       }
       /*if value is greater than search first half*/
       else if (array[middle]<value)
           last=middle-1;
       /*else search for last half*/
       else
           first=middle+1;
   }
   /*if not found return -1*/
   return -1;
}


Related Solutions

complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int...
Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int main() { int ind_square(int &); int x, *p; x=15; p = &x; ind_square(*p); } int ind_square(int &p) { *p = *p **p; } Question 1 options: C. No return for non-void function A. Indirection for the variables in ind_square requires pointer operand A and B B. Invalided use of address (&) symbol as a parameter for ind_squared A and C Question 2 Which statement...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int...
How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int binaryToOctal(char digits[], int a) { int number; if (digits[0] == '0') { if (digits[1] == '1') if (digits[2] == '0') return 2; //found "010" else return 3; //found "011" else if (digits[1] == '0') if (digits[2] == '1') return 1; // found "001" else return 0; //found "000" } else { if (digits[0] == '1') if (digits[1] == '1') if (digits[2] == '0') return...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT