Question: The following C++ program has large code lines, can anyone make use short code lines instead? //BinarySearch.cpp #include #include BinarySearch.h using namespace std; int recursiveBinarySearch(int
The following C++ program has large code lines, can anyone make use short code lines instead?
//BinarySearch.cpp
#include
#include "BinarySearch.h"
using namespace std;
int recursiveBinarySearch(int array[], int start_index, int end_index, int targetValue) {
if (start_index <= end_index)
{
int mid = (start_index+end_index) /2;
if (targetValue==array[mid]) {
return mid;
} else if (targetValue { return (1 + recursiveBinarySearch(array,start_index, mid-1, targetValue)); } else { return (1 + recursiveBinarySearch(array, mid+1, end_index, targetValue)); } } return -(INT_MAX); } void selectionSort(int arr[], int n) { int i, j, min_idx; // One by one move boundary of unsorted subarray for (i = 0; i < n-1; i++) { // Find the minimum element in unsorted array min_idx = i; for (j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element int t = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = t; } } //BinarySearch.h #ifndef BINARYSEARCH_H_INCLUDED #define BINARYSEARCH_H_INCLUDED int recursiveBinarySearch(int array[], int start_index, int end_index, int targetValue); #endif // BINARYSEARCH_H_INCLUDED //BinarySearch_test.cpp #include #include #include "BinarySearch.cpp" using namespace std; void selectionSort(int arr[], int n) { int i, j, min_idx; // One by one move boundary of unsorted subarray for (i = 0; i < n-1; i++) { // Find the minimum element in unsorted array min_idx = i; for (j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element int t = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = t; } } int main () { //ifstream object ifstream inputFile; //declare size for array int size; //open binary file inputFile.open ("dataarray.bin", ios::in | ios::binary); //read memory from file inputFile.seekg(0,ios::end); //assign memory to size size=inputFile.tellg()/ sizeof(int); //begninng of binary file inputFile.seekg(0); //pointer array int *array; array = new (nothrow) int[size]; //read contents into array inputFile.read(reinterpret_cast //close file inputFile.close(); //sort??? // selectionSort(array, size); for(int i=0; i cout< cout< int value, result; //user output cout <<"Enter the number"< cin >> value; //call the function binarysearch result = recursiveBinarySearch(array,0,size-1, value); if (result < 0) { cout< } else { cout< } return 0;}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
