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

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...
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 <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];...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT