C++ language Alter the code found in main.cpp  Using the file, add/change the code in the file, but only where indicating you can add or change code. main.cpp #include using namespace std; /* The function binarySearch accepts a sorted array data with no duplicates,  and the range within that array to search, defined by first and last

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

C++ language

Alter the code found in main.cpp 

Using the file, add/change the code in the file, but only where indicating you can add or change code.

main.cpp

#include <iostream>

using namespace std;

/*
The function binarySearch accepts a sorted array data with no duplicates, 
and the range within that array to search, defined by first and last. 
Finally, goal is the value that is searched for within the array. 
If the goal can be found within the array, the function returns the 
index position of the goal value in the array. If the goal value does 
not exist in the array, the function returns -1.
*/
int binarySearch(int data[], int first, int last, int goal)
{
  cout << "first: " << first << ", last: " << last << endl;
  
  // YOU CAN ONLY ADD OR CHANGE CODE BELOW THIS COMMENT
  return -1;
  // YOU CAN ONLY ADD OR CHANGE CODE ABOVE THIS COMMENT
}

int main()
{
  const int ARRAY_SIZE = 20;
  int searchValue;
  
  /* generates an array data that contains:
    0, 10, 20, 30, .... 170, 180, 190
  */
  int data[ARRAY_SIZE]; 
  for(int i = 0; i < ARRAY_SIZE; i++)
    data[i] = i * 10;
  
  cout << "Enter Search Value: ";
  cin >> searchValue;
    int answer = binarySearch(data, 0, ARRAY_SIZE-1, searchValue);
  cout << "Answer: " << answer << endl;
  return 0;

Create a function binarySearch that accepts 4 inputs:

  • data[] - A sorted (low to high) array of integers with no duplicates.
  • first - the smallest index position within the array that should be searched.
  • last - the largest index position within the array that should be searched.
  • goal - the value we are searching for within the array.

The function will return the index position of the value goal in the array. If the value goal does not exist in the array, the function returns -1.  You must use recursion to solve this problem efficiently. 

 

Sample Output:

Enter Search Value:
165
first: 0, last: 19
first: 10, last: 19
first: 15, last: 19
first: 15, last: 16
first: 16, last: 16
first: 17, last: 16
Answer: -1
[cathy]$ ./a.out
Enter Search Value: 40
first: 0, last: 19
first: 0, last: 8
Answer: 4
[cathy]$ ./a.out
Enter Search Value: 10
first: 0, last: 19
first: 0, last: 8
first: 0, last: 3
Answer: 1

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY