1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int ARRAY_SIZE = 100;
//x is the search value. If x is in the array, return the index of x in the array; otherwise return -1.
int binarySearch(int array[], int first, int last, int x)
{
}
int main()
{
int A[ARRAY_SIZE], B[ARRAY_SIZE], C[ARRAY_SIZE];// array C is used to hold the search results temporarily
int n = 0;// the number of elements stored in A
int m = 0;// the number of elements stored in B
string dataFile = "hw1_Q6_data.txt"; // the name of the data file
string searchFile = "hw1_Q6_search.txt"; // the name of the search file
string outputFile = "hw1_Q6_output.txt"; // the name of the output file
//these files are several random numbers.
input(A, n, dataFile);
input(B, m, searchFile);
//for (int i = 0; i< n; i++)
// cout << A[i] << " ";
//cout << endl;
//print the search results on the screen
for (int i = 0; i < m; i++)
{
C[i] = binarySearch(A, 0, n-1, B[i]);
cout << "The position of " << B[i] << " is " << C[i] << endl;
}
//output the search results to the output file
output(C, B, m, outputFile);
cout << endl;
}
|