// ==================================================================== // BinarySearch // In class assignment using file input, sorting and searching methods // ==================================================================== #include #include using namespace std; // ==================================================================== // Global Function Prototypes (Global Function Declarations) // ==================================================================== void InsertionSort(float* anArray, int size); bool BinSearchFor(float findMe, float* theArray, int size); void OutputArray( XXXXXX ); // ==================================================================== // main function - it all begins here // argc = number of arguments on the command line when program was run // argv = an array of strings, argv[0] = executable name // argv[1] = first parameter to the program // argv[2] = second parameter // : // argv[argc-1] = last parameter // // argc = argument count, argv = argument vector (of strings) // ==================================================================== int main(int argc, char * argv[]) { // --------------------------------------------------------------- // --------------------------------------------------------------- // Read in the Data from a File // --------------------------------------------------------------- // --------------------------------------------------------------- cout << "Reading data file...\n"; // open the data.txt file for input std::fstream inputFile( XXXXX ); // Read all the numbers... // keep count of how many // display them just so we can see what is happening int numCount = XXXXXX float number; while (inputFile >> number) { cout << number << " "; numCount++; } cout << endl; // Allocate enough memory to hold the entire file // contents in an array of floats float *theData = new float[ XXXXX ]; // Go back to beginning of the file inputFile.clear(); inputFile.seekg(0, ios::beg); // Read in all the data into the daa array // Display the data again to verify it was read int index = 0; while (inputFile >> theData[index]) { cout << theData[ XXXXX ] << " "; XXXXXXX } cout << endl; cout << "...end reading data file\n\n"; // --------------------------------------------------------------- // --------------------------------------------------------------- // Sort theData and output it to show it is sorted // --------------------------------------------------------------- // --------------------------------------------------------------- cout << "Sorting...\n"; InsertionSort(XXXXXX, XXXXXXX); OutputArray(theData, numCount); cout << "... end sorting\n\n"; // --------------------------------------------------------------- // --------------------------------------------------------------- // Search for a value in theData... use Binary Search // --------------------------------------------------------------- // --------------------------------------------------------------- cout << "Searching...\n"; float valueToFind = 999.999; if (true == BinSearchFor(valueToFind, XXXXXX, XXXXXX)) { cout << " Value = " << valueToFind << " was found.\n"; } else { cout << " Value = " << valueToFind << " NOT found.\n"; } cout << "...end Searching\n\n"; // --------------------------------------------------------------- // --------------------------------------------------------------- // Perform clean up operations // Free the allocated memory, close the input file // --------------------------------------------------------------- // --------------------------------------------------------------- cout << "\nCleaning up...\n"; delete [] XXXXXX; theData = NULL; inputFile.close(); cout << "\nProgram ends.\n"; return 0; } // ==================================================================== // ==================================================================== void InsertionSort(float* anArray, int size) { Implement Insertion Sort as shown in class } // ==================================================================== // ==================================================================== bool BinSearchFor(float findMe, float* theArray, int size) { Implement Binary Search as shown in class } // ==================================================================== // ==================================================================== void OutputArray(float *theArray, int size) { for (int i=0; i < size; i++) { cout << "\t" << theArray[i]; if ((i+1) % 5 == 0) { cout << endl; } } cout << endl; } // ==================================================================== // ==================================================================== // end of file