smmcfarl 0 Light Poster

I am having trouble with a program where I have to read in 40 numbers from a file and search them, 20 are successful searches and the other 20 are unsuccessful. I am supposed to show the number of comparisons it takes to find the numbers as well, this is my biggest problem I am having along with the hashing.
I have attached the input and what the output should look like.

Here is my code that I have completed already.

#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

class Search
{
public:
void prepareArr(ifstream& inFile);
void copyArr();
void sequentialSearch(ofstream& outFile, int);
void binarySearch(ofstream& outFile, int);
void summary(ofstream& outFile);
void hashing(ofstream& outFile);
void SelectionSort(ifstream& inFile);

private:
  static const int ArrayMax = 20;
  int initialArr[ArrayMax];
  int sortResult[ArrayMax];

};
int main()
{
  ifstream inFile;
  ofstream outFile;
  inFile.open("in.data");
  outFile.open("out.data");

  int sequentialSearchComp=0;
  int binarySearchComp=0;
  int ArrayMax = 20;
  int num;

  Search search;

  search.prepareArr(inFile);
  outFile << "<<<***Successful Sequential Search***>>>" << endl << endl;
  outFile << "ID        Number of Comparisons" << endl;
  outFile << "--        --------------------" << endl << endl;
  search.sequentialSearch(outFile, ArrayMax);


  search.prepareArr(inFile);
  outFile << "<<<***Unsuccessful Sequential Search***>>>" << endl << endl;
  outFile << "ID        Number of Comparisons" << endl;
  outFile << "--        --------------------" << endl << endl;
  search.sequentialSearch(outFile, ArrayMax);

  outFile << "<<<***Successful Binary Search***>>>" << endl << endl;
  outFile << "ID        Number of Comparisons" << endl;
  outFile << "--        --------------------" << endl << endl;
  search.binarySearch(outFile, ArrayMax);

  outFile << "<<<***Unsuccessful Binary Search***>>>" << endl << endl;
  outFile << "ID        Number of Comparisons" << endl;
  outFile << "--        --------------------" << endl << endl;
  search.binarySearch(outFile, ArrayMax);

  outFile << "<<<***Storing with Division Method/Hash Table***>>>" << endl << endl;
  outFile << "Array Index      ID" << endl;
  outFile << "-----------      --" << endl << endl;

  outFile << "<<<***Successful Search with Division Method/Hash Table***>>>" << endl << endl;
  outFile << "ID        Number of Comparisons" << endl;
  outFile << "--        --------------------" << endl << endl;
  search.hashing(outFile);

  outFile << "<<<***Unsuccessful Search with Division Method/Hash Table***>>>" << endl << endl;
  outFile << "ID        Number of Comparisons" << endl;
  outFile << "--        --------------------" << endl << endl;
  search.hashing(outFile);

  search.summary(outFile);
  return 0;
}
void Search::prepareArr(ifstream& inFile)
//******************************************************

{
  for(int i = 0; i <= ArrayMax-1;  i++)
    {
      inFile >> initialArr[i];
    }
}
void Search::copyArr()
//******************************************************

{
  for(int i = 0; i <= ArrayMax-1;  i++)
    {
      sortResult[i] = initialArr[i];
    }
}


void Search::sequentialSearch(ofstream& outFile, int key)
//******************************************************

{
  copyArr();
  int comp = 0;
  int comparisons = 0;
  searchResult = -1;
  for(int i = 0; i <= ArrayMax-1; i++)
    {
      comparisons++;
      comp = comparisons + comparisons;
      if(key == initialArr[i])
        {
          searchResult = i;
          break;
        }
          outFile << initialArr[i] << "        " << comparisons << endl;
    }
  outFile << "Average Number of probes: " << comp/20 << endl;
}

void Search::binarySearch(ofstream& outFile, int key)
//******************************************************
{
  int low = 0;
  int high = ArrayMax -1;
  int mid = (low + high + 1) / 2;
  int loc = -1;
  int comparisons = 0;
  int comp;

  for(int i = 0; i <= ArrayMax - 1; i++)
    {
      if(key == sortResult[mid])
        {
          loc = mid;
          comparisons++;
        }
      else if(key > sortResult[mid])
        {
          high = mid - 1;
          comparisons = comparisons + 2;
        }
      else
        {
          low = mid + 1;
          comparisons = comparisons + 2;
        }

      outFile << initialArr[i] << "           " << comparisons << endl;
    }
  outFile << "Average Number of probes: " << comp/20 << endl;

}

void Search::summary(ofstream& outFile)
{
  outFile << "***<Summary Report>***" << endl << endl;
  outFile << "<<<ASL for Successful Search>>>" << endl;
  outFile << "-------------------------------" << endl << endl;
  outFile << "Sequential        Binary          Hashing" << endl;
  outFile << "----------      ------          -------" << endl << endl;


  outFile << "<<<ASL for Unsuccessful Search>>>" << endl << endl;
  outFile << "---------------------------------" << endl;
  outFile << "Sequential        Binary          Hashing" << endl;
  outFile << "----------      ------          -------" << endl << endl;
}

void Search::hashing(ofstream& outFile)
//******************************************************
{
  int i;
  for(i=1; i <= ArrayMax; i++)
    {

      outFile << initialArr[i] << endl;
    }
}
void Search::SelectionSort(ifstream& inFile)
//******************************************************
{
  int i, j, first, temp;
  int numLength;
  for(int i = 0; i <= ArrayMax-1;  i++)
    {
      inFile >> initialArr[i];
    }


  for (i= numLength - 1; i > 0; i--)
    {
      first = 0;
      for (j=1; j<=i; j++)
        {
          if (initialArr[j] < initialArr[first])
          first = j;
        }
      temp = initialArr[first];
      initialArr[first] = initialArr[i];
      initialArr[i] = temp;
       }
  return;
}

I have also attached the output I am getting.
Thanks for all of your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.