Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, Can someone help me with this please? I need an object to call the recursiveBinarySearch and I need to declare ret but I am

Hello,

Can someone help me with this please? I need an object to call the recursiveBinarySearch and I need to declare ret but I am unsure how to. The following errors came up:

1. 'ret' was not declared in this scope in BinarySearch_test.cpp

2. 'recursiveBinarySearch' was not declared in this scope in BinarySearch_test.cpp

Here is the code:

BinarySearch.h

#ifndef BINARYSEARCH_H #define BINARYSEARCH_H

using namespace std;

class BinarySearch {

public: char readBinaryFile(char *fileName); int recursiveBinarySearch(int start_index, int end_index, int targetValue); };

#endif

BinarySeach.cpp

#include "BinarySearch.h" #include #include #include #include using namespace std;

//vector to dynamically store large number of values... vector ret; //variable to count the number of comparisions... int n=0; //method to read file.... void readBinaryFile(char* fileName) { ifstream read(fileName); unsigned int current; if(read!=NULL) while (read>>current) { ret.push_back(current); } }

//recursive binary search method

void recursiveBinarySearch(int start_index, int end_index, int targetValue) { int mid = (start_index + end_index) / 2; if(start_index > end_index) { cout << n << ":-" << endl; return ; } if(ret.at(mid) == targetValue) { n++; cout << n << ":" << mid << endl; return ; } else if(ret.at(mid) < targetValue) { n = n + 2;//upto here two comparisions have been made..so adding two start_index = mid + 1; return recursiveBinarySearch(start_index, end_index, targetValue) ; } else { n = n + 2; end_index = mid - 1; return recursiveBinarySearch(start_index, end_index, targetValue) ; } }

BinarySeach_test.cpp

#include "BinarySearch.h" #include #include using namespace std;

int main() { BinarySearch x; char a[] = "dataarray.bin"; x.readBinaryFile(a); int targetValue; cout << "Enter a Value to search:"; cin >> targetValue; recursiveBinarySearch(0, ret.size()-1, targetValue) ; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions