// I'm having this problem with HandleQueries function. Everything else in the
// in the program is completely right. Please do provide any refernces if you guys can . THnks!
// REMARKS: This program illustrates the use of sorting and searching.
// This program reads a collection of words from a file and stores them
// in an array. The name of the file is given on the UNIX command-line.
// No more than 50 words will ever be in the file. After the words are
// read into an array, the array is sorted in ascending order and
// the sorted words are printed out, one per line. Next a word is read
// from a "query file". The name of this file is given second on the UNIX
// command-line. The array is searched for the desired word. The word is
// printed, followed by a blank, followed by: if found, the subscript of the
// containing array element or, if not found, a message saying the word is
// "NOT IN THE ARRAY" is printed. This query/search cycle is repeated for
// every word in the query file.
// Global symbolic constants:
const int MAXWORDS = 50; // Maximum number of words to store.
const int NOT_FOUND = -1; // "Word not found" sentinel value.
// Function prototypes:
void LoadArray(string word[], int& numberOfWords, char wordsFileName[]);
void Sort(string word[], int numberOfWords);
void PrintArray(const string word[], int numberOfWords);
void HandleQueries(string word[], int numberOfWords, char queryFileName[]);
int BinarySearch(string word[], int lower, int upper, string searchFor);
int LocateMinimum(string word[], int lower, int upper);
int main( int argc, char * argv[] )
// IN IN
{
string word[MAXWORDS]; // Word storage array.
int numberOfWords; // Number of words stored in "word" array.
// Exit if wrong number of arguments supplied.
if (argc != 3)
{
cout << "Error: wrong number of arguments.\n";
return 1;
}
// Read words from word input file (argv[1]) into the "word" array.
LoadArray(word, numberOfWords, argv[1]);
// Sort the "word" array in ascending alphabetical order.
Sort(word, numberOfWords);
// Print the contents of the "word" array, one per line.
PrintArray(word, numberOfWords);
// Using words from the query file, see if they are in the "word" array.
HandleQueries(word, numberOfWords, argv[2]);
return 0;
} // end main
// Read entries from "wordsFileName" file and store in the "word" array.
void LoadArray(string word[], int& numberOfWords, char wordsFileName[])
// OUT OUT IN
{
ifstream input; // Input file containing words.
// Open specified input file.
input.open( wordsFileName );
// One by one, store each word in input file into "word" array.
numberOfWords = 0;
input >> word[numberOfWords];
while ( input )
{
numberOfWords++;
input >> word[numberOfWords];
}
// Close the input file.
input.close();
} // end LoadArray
// Selection Sort the "word" array in nondescending alphabetical order.
void Sort(string word[], int numberOfWords)
// INOUT IN
{
for (int top = 0; top < numberOfWords - 1; top++)
{
int smallPos;
smallPos = LocateMinimum(word, top, numberOfWords - 1);
swap(word[top], word[smallPos]);
}
} // end Sort
// Print the contents of the "word" array, one per line.
void PrintArray(const string word[], int numberOfWords)
// IN IN
{
for (int i=0; i < numberOfWords; i++)
cout << word[i] << endl;
} // end PrintArray
// Read words from queryFileName file and see if they are in the "word" array.
void HandleQueries(string word[], int numberOfWords, char queryFileName[])
// IN IN IN
{
ifstream query;
query.open("sose.query");
query >> queryFileName;
while (query)
{
int x;
x = BinarySearch(word, lower, upper, queryFileName);
cout << queryFileName;
if (found)
cout << " " << x << endl;
else
cout << " " << "NOT IN THE ARRAY" << endl;
}
}
} // end HandleQueries
// Binary Search "word" array for target "searchFor"
int BinarySearch(string word[], int lower, int upper, string searchFor)
// IN IN IN IN
{
while(lower <= upper)
{
int mid;
mid = (lower+upper)/2;
if (searchFor == word[mid])
return mid;
else if (searchFor < word[mid])
upper = mid - 1;
else // searchFor > word[mid]
lower = mid + 1;
}
return -1;
} // end BinarySearch
int LocateMinimum (string word[], int lower, int upper)
{
string small = word[lower];
int pos = lower;
for (int k = lower+1; k <= upper; ++k)
{
if (word[k] < small)
{
small = word[k];
pos = k;
}
}
return pos;
}
For me and others who would try to answer the question, here is the function indented in code tags.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void HandleQueries(string word[], int numberOfWords, char queryFileName[]) {
ifstream query;
query.open("sose.query");
query >> queryFileName;
while (query) {
int x;
x = BinarySearch(word, lower, upper, queryFileName);
cout << queryFileName;
if (found)
cout << " " << x << endl;
else
cout << " " << "NOT IN THE ARRAY" << endl;
}
}
Looks like in the bold line you are calling the binary search function using lower and upper without them being defined. If you get any errors, please post them.
@Zeillinger . Thanks a bunch for indenting the code. Let me explain the Purpose of the HandleQueries function. The function is supposed to read in a word from a file (sose.query), then Search the array "word[]" for that word. Print the word, followed by a blank followed by: if found, the subscript of the array element that contains the word or, if not found, print a message saying the word is "NOT IN THE ARRAY". Repeat this query/search cycle until the query file has been completely processed.
I do not know how to implement the lower and upper in the handle queries function, but they must be passed into Binary Search. the lower indicates the least index number of the array and upper indicates the end index number of that array.
Hope this helps... I'll post the entire problem statement below
THe problem Statement - TASK: Write a C++ program to read a collection of words from a file and store them in an array. The name of the file is given on the UNIX command-line. No more than 50 words will ever be in the file. After the words are read into the array, the array is sorted in ascending order and the sorted words are printed out, one per line. Next read a word from a "query file". The name of this file is given second on the UNIX command-line. Search the array for that word. Print the word, followed by a blank followed by: if found, the subscript of the array element that contains the word or, if not found, print a message saying the word is "NOT IN THE ARRAY". Repeat this query/search cycle until the query file has been completely processed.
IMPLEMENTATION NOTES: Use the Selection Sort to perform the sorting. Use the Binary Search to do the searching. Do not worry about overflowing the array limits while reading words into the array. Declare the array and the count of elements stored in the array in the main routine. Your array elements should be C++ strings. Do not use any global variables in this program, use parameters where needed. You MUST have the following procedures in your program: LoadArray, Sort, PrintArray, HandleQueries, BinarySearch. You are free to create other procedures as needed. The PrintArray routine should print out each word in the array, one per line. Your code for Sort must use C++'s built-in swap().
Like I said, EVERYTHING IS TAKEN CARE OF, EXCEPT HANDLE QUERIES.