Question: C++ Hello I've attached my source code. I'm getting an output as below.... Only small portion of output thus far, how do i prevent the
C++
Hello I've attached my source code. I'm getting an output as below....
Only small portion of output thus far, how do i prevent the it from loading unto console yet keep functionality of binary search.... 526104064 366641152 1290108928 -635699200 1650065408 630292480 1636007936 986054656 -1238466560 1502838784 1721892864 -759136256 -226295808 -785448960 2124120064 -708509696
Enter the number 21 21:-
I wish for my output to only include the binary search method I've included. I believe there is something wrong with the way I'm reading the file in. I don't want the output to include the data onto the console...
below is source code....
#include
#include
#include
using namespace std;
int recursiveBinarySearch(int array[], int start_index, int end_index, int targetValue);
void selectionSort(int arr[], int n);
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;} 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); } // Selection sort function 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; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
