Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this c++ code (bolded //ToDo parts ): ArrayUtilities.h #ifndef ARRAYUTILITIES_H_ #define ARRAYUTILITIES_H_ #include #include void fillArray( std::istream& io, int a[], const

I need help with this c++ code (bolded //ToDo parts):

ArrayUtilities.h

#ifndef ARRAYUTILITIES_H_

#define ARRAYUTILITIES_H_

#include

#include

void fillArray(

std::istream& io,

int a[],

const int maxSize,

int& actualSize

);

void outputArray(

std::ostream& os,

const int a[],

const int actualSize,

const std::string& info

);

#endif /* ARRAYUTILITIES_H_ */

SearchUtilities.h

#ifndef SEARCHUTILITIES_H_ #define SEARCHUTILITIES_H_

//Sequential Search int seqSearch(const int list[], int listLength, int searchItem, int &count);

//recursive Sequential Search int recSeqSearch(const int list[], int listLength, int searchItem, int &count);

//Binary Search int binarySearch(const int list[], int listLength, int searchItem, int &count);

//recursive Binary Search int recBinarySearch(const int list[], int listLength, int searchItem, int first, int last, int &count);

void bubbleSort(int list[], int listLength, int &count); #endif /* SEARCHUTILITIES_H_ */

ArrayUtilities.cpp

#include

#include

#include "ArrayUtilities.h"

using namespace std;

void fillArray(istream& io, int a[], const int maxSize, int& actualSize)

{

int temp,i=0;

io>>temp;

while (temp!=-999)

{

assert(i

a[i++]=temp;

io>>temp;

}

actualSize=i;

}

void outputArray(ostream& os,

const int a [],

const int actualSize,

const string& info )

{

os<

for (int i=0;i

{

os<

}

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago