}
os<<" ";
}
Driver.cpp
#include
#include
#include
#include
#include "ArrayUtilities.h"
#include "SearchUtilities.h"
using namespace std;
int main()
{
const int ARRAYSIZE=20;
int actualSize;
string fileNameI, fileNameO;
string info;
int a[ARRAYSIZE];
cout << "Enter the input and output file names: ";
cin >> fileNameI >> fileNameO;
ifstream inData;
ofstream outData;
inData.open(fileNameI.c_str());
outData.open(fileNameO.c_str());
int searchItem;
inData >> info;
inData >> searchItem;
fillArray (inData, a, ARRAYSIZE,actualSize);
cout << "Search in array ";
outData << "Search in array ";
outputArray(outData, a, actualSize, info);
outputArray(cout, a, actualSize, info);
//sequential search
int position;
int count;
position = seqSearch(a,actualSize,searchItem, count);
if (position >= 0)
{
cout << "Sequential Search: SearchItem "
<< searchItem
<< " is on position "
<< position
<< " Number of item comparisons = "
<< count
<< " ";
outData << "Sequential Search: SearchItem "
<< searchItem
<< " is on position "
<< position
<< " Number of item comparisons = "
<< count
<< " ";
}
else
{
cout << "Sequential Search: SearchItem "
<< searchItem
<< " is not in the list "
<< " Number of item comparisons = "
<< count
<< " ";
outData << "Sequential Search: SearchItem "
<< searchItem
<< " is not in the list"
<< " Number of item comparisons = "
<< count
<< " ";
}
//ToDo Recursive Sequential Search : change input file also
inData>>info;
inData>>searchItem;
fillArray (inData, a, ARRAYSIZE,actualSize);
cout <<"Search in array ";
outData<<"Search in array ";
outputArray(outData, a, actualSize, info);
outputArray(cout, a, actualSize, info);
position=recSeqSearch(a,actualSize,searchItem, count);
if (position >= 0)
{
cout << "Recursive Sequential Search: SearchItem "
<< searchItem
<< " is on position "
<< position
<< " Number of item comparisons = "
<< count
<< " ";
outData << "Recursive Sequential Search: SearchItem "
<< searchItem
<< " is on position "
<< position
<< " Number of item comparisons = "
<< count
<< " ";
}
else
{
cout << "Recursive Sequential Search: SearchItem "
<< searchItem
<< " is not in the list"
<< " Number of item comparisons = "
<< count
<< " ";
outData << "Recursive Sequential Search: SearchItem "
<< searchItem
<< " is not in the list, "
<< " Number of item comparisons = "
<< count
<< " ";
}
//BinarySearch
//int binarySearch(const int list[], int listLength, int searchItem);
inData>>info;
inData>>searchItem;
fillArray (inData, a, ARRAYSIZE,actualSize);
cout << "Array before bubbleSort ";
outData << "Array before bubbleSort ";
outputArray(outData, a, actualSize, info);
outputArray(cout, a, actualSize, info);
bubbleSort(a,actualSize, count);
cout << "Array after bubbleSort and before binary search ";
outData << "Array after bubbleSort and before binary search ";
cout << "Number of item comparisons in bubble sort = "
<< count
<< " ";
outData << "Number of item comparisons in bubble sort = "
<< count
<< " ";
outputArray(outData, a, actualSize, info);
outputArray(cout, a, actualSize, info);
count = 0;
position=binarySearch(a,actualSize,searchItem, count);
if (position >= 0)
{
cout << "Binary Search: SearchItem "
<< searchItem
<< " is on position "
<< position
<< " Number of item comparisons = "
<< count
<< " ";
outData << "Binary Search: SearchItem "
<< searchItem
<< " is on position "
<< position
<< " Number of item comparisons = "
<< count
<< " ";
}
else
{
cout << "Binary Search: SearchItem "
<< searchItem
<< " is not in the list"
<< " Number of item comparisons = "
<< count
<< " ";
outData << "Binary Search: SearchItem "
<< searchItem
<< " is not in the list"
<< " Number of item comparisons = "
<< count
<< " ";;
}
//ToDo Recursive Binary Search
//BinarySearch
int binarySearch(const int list[], int listLength, int searchItem);
inData>>info;
inData>>searchItem;
fillArray (inData, a, ARRAYSIZE,actualSize);
cout << "Array before bubbleSort ";
outData << "Array before bubbleSort ";
outputArray(outData, a, actualSize, info);
outputArray(cout, a, actualSize, info);
bubbleSort(a,actualSize, count);
cout << "Array after bubbleSort and before recursive binary search ";
outData << "Array after bubbleSort and before recursive binary search ";
outputArray(outData, a, actualSize, info);
outputArray(cout, a, actualSize, info);
position = recBinarySearch(a,actualSize,searchItem,0, actualSize-1, count);
if (position >= 0)
{
cout << "Recursive Binary Search: SearchItem "
<< searchItem
<< " is on position "
<< position
<< " Number of item comparisons = "
<< count
<< " ";
outData << "Recursive Binary Search: SearchItem "
<< searchItem
<< " is on position "
<< position
<< " Number of item comparisons = "
<< count
<< " ";
}
else
{
cout << "Recursive Binary Search: SearchItem "
<< searchItem
<< " is not in the list"
<< " Number of item comparisons = "
<< count
<< " ";
outData << "Recursive Binary Search: SearchItem "
<< searchItem
<< " is not in the list"
<< " Number of item comparisons = "
<< count
<< " ";
}
inData.close();
outData.close();
system("pause");
}
SearchUtilities.cpp
int seqSearch(const int list[], int listLength, int searchItem, int &count)
{
count = 0;
int loc;
bool found = false;
for (loc = 0; loc {
++count;
if (list[loc]==searchItem)
{
found = true;
break;
}
}
if (found)
{
return loc;
}
else
{
return -1;
}
}
int recSeqSearchActual(const int list[], int listLength, int searchItem, int &count)
{
if (listLength==0) return -1;
++count;
if (list[listLength]==searchItem)
{
return listLength;
}
else
{
return recSeqSearchActual(list, listLength-1, searchItem, count);
}
}
int recSeqSearch(const int list[], int listLength, int searchItem, int &count)
{
return recSeqSearchActual(list,listLength,searchItem,(count=0));
}
int binarySearch(const int list[], int listLength, int searchItem, int &count)
{
//ToDo
return -10;
}
int recBinarySearchActual(const int list[], int listLength, int searchItem, int first, int last, int &count)
{
//ToDo
// call itself with different parameters
return -10;
}
int recBinarySearch(const int list[], int listLength, int searchItem, int first, int last, int &count)
{
//ToDo
// call recBinarySearchActual()
return -5;
}
void bubbleSort(int list[], int listLength, int &count)
{
//ToDo
}
SampleOutput:
Enter the input and output file names: in.txt out.txt Search in array SeqSearch 1 6 4 3 Sequential Search: SearchItem 4 is on position 2 number of item comparisons = 3 Search in array RecSeqSearch 1 6 4 3 12 18 1 4 5 6 Recursive Sequential Search: SearchItem 3 is on position 3 number of item comparisons = 8 Array before bubbleSort BinSearch 2 4 6 8 10 15 9 Array after bubbleSort and before binary search number of item comparisons in bubble sort = 21 BinSearch 2 4 6 8 9 10 15 Binary Search: SearchItem 8 is on position 3 number of item comparisons = 1 Array before bubbleSort RecBinSearch 2 4 6 8 10 15 18 5 2 1 Array after bubbleSort and before recursive binary search RecBinSearch 1 2 2 4 5 6 8 10 15 18 Recursive Binary Search: SearchItem 18 is on position 9 number of item comparisons = 7