QuestionQuestion

Write a program that will use a recursive binary search to locate an integer in a sorted array. The integer values will be provided in a file named dataarray.bin. You should dynamically allocate memory sufficient to hold all elements in this array and read the contents of this file into your data structure.

Your code must implement a function called recursiveBinarySearch(int start_index, int end_index, int_targetValue) which will perform the binary search for the integer value entered at the command line.

If the value is found you should print a string "N:V" where N is the number of comparisons made before the value V is found. If the value V is not found in the array print a dash, for example "23:-"

You should test you class with a driver program named BinarySearch_test.cpp

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include "BinarySearch.h"
#include <iostream>
#include <fstream>
using namespace std;

BinarySearch::BinarySearch() {
    size = 0;
    readFile();
}

BinarySearch::~BinarySearch() {
    /// delete [] list;
}

/**
* guess size of the file
* @param name
* @return size of file in byte
*/
unsigned int BinarySearch::fileSize(const char* name) {
    unsigned int start = 0;
    unsigned int ends = 0;

    ifstream in;
    in.open(name, ios::binary);
    if (in.is_open()) {
       // mark the start of file
       start = in.tellg();
       in.seekg(0, ios::end);
       // mark the end of file
       ends = in.tellg();
       in.close();
    } else {
       cout << "Cannot open " << name << "\n";
    }

    return ends - start;
}

/**
* bubble sort
*/
void BinarySearch::sort() {
    int t;
    for (int i = size - 1; i >= 0; i--) {
       for (int j = i; j < size; j++) {
            if (list[j - 1] > list[j]) {...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$30.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats