How to alter code reading string of arrays from file?
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
// Function prototype
int binarySearch(constint [], int, int);
constint MAX_SIZE = 1000;
int main(int argc, char* argv[])
{
int values[MAX_SIZE];
int results,
count,
searchKey;
string fileName;
ifstream fin;
if (argc > 1)
fileName = argv[1];
else
{
cout << "Name of File: ";
cin >> fileName;
}
fin.open(fileName);
if (!fin)
{
perror(fileName.data());
exit(EXIT_FAILURE);
}
count = 0;
while (count < MAX_SIZE && fin >> values[count])
count++;
cout << "Processing " << count << " numbers" << endl;
cout << "Enter Search Key, ctrl-Z to end: ";
while ( cin >> searchKey )
{
results = binarySearch(values, count, searchKey);
// If searchList returned -1, then searchKey was not found.
if (results == -1)
cout << searchKey << " not found" << endl;
else
// Otherwise results contains the subscript of
// the searchKey in the array.
cout << searchKey << " found at position " << results << endl;
cout << "Search Key: ";
}
return EXIT_SUCCESS;
}
// The binarySearch function performs a binary search on an *
// integer array. array, which has a maximum of size elements, *
// is searched for the number stored in value. If the number is *
// found, its array subscript is returned. Otherwise, -1 is *
// returned indicating the value was not in the array. *
int binarySearch(constint array[], int size, int value)
{
int first = 0, // First array element
last = size - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2; Calculate mid point
if (array[middle] == value) If value is found at mid
{
found = true;
position = middle;
}
elseif (array[middle] > value) If value is in lower half
last = middle - 1;
else
first = middle + 1; If value is in upper half
}
return position;
}
Is this correct? it will be reading a list of 400 names.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Function prototype
int binarySearch(const string [], int, int);
const int MAX_SIZE = 400;
Here is how the output should appear:
Name of File: names.txt
Processing 400 strings
Enter Search Key, ctrl-Z to end: Steven
Steven found at position 372
Search Key: Stanley
Stanley not found
Search Key: Natalie
Natalie found at position 314
Search Key: Nancy
Nancy not found
Search Key: Zoey
Zoey found at position 399
Search Key: Aaliyah
Aaliyah found at position 0
Search Key: Zoroaster
Zoroaster not found
Search Key: ^Z
using datatype = std::string;
int main(int argc, datatype argv[])
{
string values[MAX_SIZE];
int results,
count;
string searchKey;
string fileName;
ifstream fin;
if (argc > 1)
fileName = argv[1];
else
{
cout << "Name of File: ";
cin >> fileName;
}
fin.open(fileName);
if (!fin)
{
perror(fileName.data());
exit(EXIT_FAILURE);
}
count = 0;
while (count < MAX_SIZE && fin >> values[count])
count++;
cout << "Processing 400 strings " << endl;
cout << "Enter Search Key, ctrl-Z to end: ";
while ( cin >> searchKey )
{
results = binarySearch(values, count, searchKey);
// If searchList returned -1, then searchKey was not found.
if (results == -1)
cout << searchKey << " not found" << endl;
else
// Otherwise results contains the subscript of
// the searchKey in the array.
cout << searchKey << " found at position " << results << endl;
cout << "Search Key: ";
}
return EXIT_SUCCESS;
}
//***************************************************************
// The binarySearch function performs a binary search on an *
// integer array. array, which has a maximum of size elements, *
// is searched for the number stored in value. If the number is *
// found, its array subscript is returned. Otherwise, -1 is *
// returned indicating the value was not in the array. *
//***************************************************************
int binarySearch(const data type array[], int size, int value)
{
int first = 0, // First array element
last = size - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (array[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
}