Question

The code below is already completed using C++. Could you please explain why the input is...

The code below is already completed using C++. Could you please explain why the input is

3 : 2 :
3 : 6 : 4 : -1 :
3 : 1 : 0 :? Please let me know really short explanation.

#include <iostream>

using namespace std ;

int binarySearch(const int array[], int numElems, int value)

{

int first = 0, // First array element

last = numElems - 1, // Last array element

middle, // Midpoint of search

position = -1; // Position of search value

bool found = false; // Flag

while (!found && first <= last)

{

middle = (first + last) / 2; // Calculate midpoint

cout << array[middle] << " : " ;

if (array[middle] == value) // If value is found at mid

{

found = true;

position = middle;

}

else if (array[middle] > value) // If value is in lower half

last = middle - 1;

else

first = middle + 1; // If value is in upper half

}

cout << position << " : " ;

return position;

}

int main()

{

int arr1[] = { 1 , 2 , 3 , 4 , 6 , 9 } ;

binarySearch( arr1 , 6, 3 ) ;

cout << endl ;

binarySearch( arr1 , 6, 5 ) ;

cout << endl ;

binarySearch( arr1 , 6, 1 ) ;

cout << endl ;

return ( 0 ) ;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std ;

int binarySearch(const int array[], int numElems, int value)

{

int first = 0, // First array element

last = numElems - 1, // Last array element

middle, // Midpoint of search

position = -1; // Position of search value

bool found = false; // Flag

while (!found && first <= last)

{

middle = (first + last) / 2; // Calculate midpoint

cout << array[middle] << " : " ;

if (array[middle] == value) // If value is found at mid

{

found = true;

position = middle;

}

else if (array[middle] > value) // If value is in lower half

last = middle - 1;

else

first = middle + 1; // If value is in upper half

}

cout << position << " : " ;

return position;

}

int main()

{

int arr1[] = { 1 , 2 , 3 , 4 , 6 , 9 } ;

binarySearch( arr1 , 6, 3 ) ;
//note binary search searches the list by everytime exculding half of the elements of the list
//here we are searching 3
//first =0, last =6-1=5
//the mid index is : (first+last)/2 =(0+5)/2=5/2=2 (works as floor)
//now a[2]=3 //mid value
//so 3: is printed
//since we are searching for 3 ,position is update with 2(index of 3), then while loop stops
//then position is printed
//2:
//so, 3:2: printed
cout << endl ;

binarySearch( arr1 , 6, 5 ) ;

//searching value 5
//first =0
//last=5
//mid = 5/2=2
//a[mid]=3
//3: printed
//since 5 is greater than 3
//the new mid is
//mid =(3+5)/2=8/2=4 //since first =middle +1
//the value at index four is 6
//hence 6: printed
//now 5 is less than 6
//now last=middle-1=3
//now mid =(3+3)/2=3
//value at index 3 is 4
//so 4: printed
//and 4 is less than 5
//so now first =middle+1=3+1=4
//the loop stops
//since position is not upadeted
//-1: printed
//so
//3 : 6 : 4 : -1 : printed
cout << endl ;

binarySearch( arr1 , 6, 1 ) ;

//search element 1
//first =0
//last=5
//mid =2
//value at index 2 is 3
//3: printed
//since 3 is greater than 1
//last=middle-1=2-1=1
//mid = (0+1)/2 = 0
//value at index 0 is 1
//1: printed
//since the searching value is found position is updated with index 0
//then
//0: printed
//so
//3:1:0: printed

cout << endl ;

return ( 0 ) ;

}

Add a comment
Know the answer?
Add Answer to:
The code below is already completed using C++. Could you please explain why the input is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • C++ Need the step count for this function. int binarySearch(const int array[], int size, int value)...

    C++ Need the step count for this function. int binarySearch(const int array[], int size, int value) { int first = 0, last = size − 1, middle, position = −1; bool found = false; while (!found && first <= last) { middle = (first + last) / 2; if (array[middle] == value) { found = true; position = middle; } else if (array[middle] > value) last = middle − 1; else first = middle + 1; } return position; }

  • /* * Program5 for Arrays * * This program illustrates how to use a sequential search...

    /* * Program5 for Arrays * * This program illustrates how to use a sequential search to * find the position of the first apparance of a number in an array * * TODO#6: change the name to your name and date to the current date * * Created by Li Ma, April 17 2019 */ #include <iostream> using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() {...

  • * This program illustrates how to use a sequential search to find the position of the...

    * This program illustrates how to use a sequential search to find the position of the first apparance of a number in an array TODO#6: change the name to your name and date to the current date * * Created by John Doe, April 17 2019 */ #include using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() { //TODO#1: declare an integer array named intList with size of...

  • Please help with my C++ homework! 1. The following function is supposed to perform binary search....

    Please help with my C++ homework! 1. The following function is supposed to perform binary search. It has no errors and will execute correctly. int binarySearch(int array[], int size, int value) {    int first = 0,             // First array element        last = size - 1,       // Last array element        middle,                // Mid point of search        position = -1;         // Position of search value    bool found = false;        // Flag    middle = (first + last)...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • I need to modify the code below to perform a binary search instead of a linear...

    I need to modify the code below to perform a binary search instead of a linear search. #include <iostream> using namespace std; int searchList(int[], int, int); int main() {    const int NUMS = 10;    int Picks[NUMS] = { 13579, 26791, 26792, 33445, 55555,                        62483, 77777, 79422, 85647, 93121 };    int WinNums,        Search;      cout << "Enter this week's winning five-digit number: ";    cin >> WinNums;    Search =...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • Could you please check what is wrong with this program? The output should be answer is:...

    Could you please check what is wrong with this program? The output should be answer is: 4 kSmall answer is: 4 answer is: 11 kSmall answer is: 11 ----- #include <iomanip> #include <iostream> #include <string> using namespace std; int const ARRAY_SIZE = 6; int pivotIndex; int p; int kSmall(int k, int anArray[], int first, int last) { if (k < pivotIndex - first + 1) return kSmall(k, anArray, first, pivotIndex - 1); else if (k == pivotIndex - first +...

  • Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top =...

    Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT