Question

In C++: Write a C++ function binsearch that carries out the binary search algorithm on a...

In C++:

Write a C++ function binsearch that carries out the binary search algorithm on a sorted
array of integers. Your function takes as parameters an array of integers (sorted in
increasing order), the size of the array, and a target integer to search for. The function
returns the index of the target in the array, and returns -1 if the target is not in the array.

Using the code below, add your binsearch function to this C++ program. (Do not change the code that generates the array.).

Use your function to search for the elements {39, 47, 208, 288, 427, 685, 676, 972}. Your program should print out each of
these elements, and the return value of the function for that element, that is, the index
in the array a where the element was found, or the integer -1 if the element was not found.

#include <iostream>
# include <cstdlib>

using namespace std;

int mainone()
{
    int a[100];
    srand(300);
    for ( int k = 0; k < 100; ++k){
        a[k] = 10*k + rand()%10;
    }

}

....

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

I have implemented the above binary search program in c++.

there are 3 functions: binsearch(), mainone(), main().

Note: You may look the screenshot of the code to have better understanding of the indentation.

c++ code:

#include<iostream>
#include<cstdlib>

using namespace std;

//binary search function
int binsearch(int a[],int n,int x)
{
//low, high, middle variables
int l=0,h=n-1,m;

while(l<=h)
{
//find middle
m = (l+h)/2;
//if found
if(a[m] == x)
return m;
else if(x < a[m])
h = m-1;
else
l = m+1;
}
//not found
return -1;
}

//user function
int mainone()
{
int a[100];
srand(300);
for(int k=0;k<100;++k)
{
a[k] = 10*k + rand()%10;
}

//print the elements
for(int i=0;i<100;i++)
cout<<a[i]<<" ";

//call binary search function
int index = binsearch(a,100,39);
cout<<"\nElement 39: "<<index<<endl;
index = binsearch(a,100,47);
cout<<"Element 47: "<<index<<endl;
index = binsearch(a,100,208);
cout<<"Element 208: "<<index<<endl;
index = binsearch(a,100,288);
cout<<"Element 288: "<<index<<endl;
index = binsearch(a,100,427);
cout<<"Element 427: "<<index<<endl;
index = binsearch(a,100,685);
cout<<"Element 685: "<<index<<endl;
index = binsearch(a,100,676);
cout<<"Element 676: "<<index<<endl;
index = binsearch(a,100,972);
cout<<"Element 972: "<<index<<endl;
index = binsearch(a,100,771);
cout<<"Element 771: "<<index<<endl;
}

//main function
int main()
{
//call the function mainone
mainone();
return 0;
}
Output:

Screenshot of code:

Add a comment
Know the answer?
Add Answer to:
In C++: Write a C++ function binsearch that carries out the binary search algorithm on a...
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
  • Write a C++ function binsearch that carries out the binary search algorithm on a sorted array...

    Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...

  • Write and test a function in C++ that uses the binary search algorithm to search an...

    Write and test a function in C++ that uses the binary search algorithm to search an array of sorted strings – use a do..while loop to allow user to perform multiple searches w/o terminating the program – see sample output below. Use this name array: string names[SIZE] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",         "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",         "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",         "James, Jean", "Weaver, Jim", "Pore, Bob",...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE...

    PLEASE IMPLEMENT YOUR SOLUTION RECURSIVELY IN C++. IT WOULD BE GREAT IF YOU COULD ALSO PROVIDE TEST CODE FOR IT. 5) Design a recursive function to find the immediate successor of a target integer in an array o:f sorted integers. Here the immediate successor of a target is defined as the smallest number that is no smaller than the target in this sorted array int binarySuccessor(const int anArrayl, const int first, const int last, int target); In the above function...

  • C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below...

    C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...

  • The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered...

    The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered list. The algorithm (in pseudocode) is as follows: highIndex - the maximum index of the part of the list being searched lowIndex - the minimum index of the part of the list being searched target -- the item being searched for //look in the middle middleIndex = (highIndex + lowIndex) / 2 if the list element at the middleIndex is the target return the...

  • 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...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

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