#include <iostream>
#include <time.h>
#include <fstream>
#define MAXSIZE 1000 
#include <cstring>
#include <string>

using namespace std;

class Students
{
   public:
      string firstName;
      string lastName;
      string social;
      double gpa;

   Students()
   {
      firstName = "Adam";
      lastName = "Wilson";
      social = "000-00-0000";
      gpa = 0.00;
   }

   Students(string newSocial, string newFirst, string newLast, double newGPA)
   {
      firstName = newFirst;
      lastName = newLast;
      social = newSocial;
      gpa = newGPA;
   }
};

bool binarySearch( Students *X[], int l, int r, string sv, int &pos);
void shellSort(Students *X[], int n);
void mergeSort(Students *X[], int l, int r);
void merge(Students *X[], int l, int m, int r);

int main(void)
{
   
   ifstream infile;
   ofstream outfile;

   infile.open("bigfile");
   
   if(infile.fail()) 
   {
      cerr<<"bigfile has an problem"<<endl;
      exit(1);
   }
   
   outfile.open("output");
   
   if(outfile.fail()) 
   {
       cerr<<"results has an problem"<<endl;
       exit(1);
   }

   clock_t start_msec,stop_msec;   //TIME
   clock_t start_sort,stop_sort;   //TIME

   start_msec = clock();   //TIME
   
   string input;         
   
   int n = 0;
   
   string social;
   string firstName;
   string lastName;
   double gpa;
   int position;

   Students * allInfo[100000] = {0};
   
   infile >> social >> firstName >> lastName >> gpa;

   Students * currentStudent;

   while (!infile.eof())
   {
      currentStudent = new Students(social, firstName, lastName, gpa);
      allInfo[n] = currentStudent;
      n++;
      infile >> social >> firstName >> lastName >> gpa;
   }
   
   infile.close();
   infile.open("file");
         
   exit(1);   
   mergeSort(allInfo, 0, n);
   
   infile >> input;
   
   while(!infile.eof())
   {
      bool s = binarySearch(allInfo, 0, n - 1, input, position);
      if(s)
      {
         outfile << (*allInfo[position]).social 
         << " " << (*allInfo[position]).firstName << " " 
         << (*allInfo[position]).lastName 
         << " " << (*allInfo[position]).gpa << endl;
      }
      else
         outfile << input << " not found!" << endl;
         infile >> input;
   }
   
   stop_msec = clock();   //TIME

   cerr<<"sort start: "<<start_msec<<endl;;   //TIME
   cerr<<"sort stop: "<<stop_msec<<endl;;   //TIME
   cerr<<(double)(stop_msec-start_msec)/CLOCKS_PER_SEC<<endl;   //TIME
   infile.close();
   outfile.close();

   return 0;
}

void mergeSort(Students *X[], int l, int r)
{
   if ( l < r )
   {
      int m = (l + r) / 2;
      mergeSort(X, l, m);
      mergeSort(X, m + 1, r);
      merge(X, l, m, r);
   }
}

void merge(Students *X[], int l, int m, int r)
{
   int l1 = l, r1 = m, l2 = m+1, r2 = r, t = l;
   
   Students *temp[MAXSIZE];

   while( l1 <= r1 && l2 <= r2)
   {
      if ((*X[l1]).social > (*X[l2]).social)
         temp[t++] = X[l1++];
      else
         temp[t++] = X[l2++];
   }
   while (l1 <= r1)
   {
      temp[t++] = X[l1++];
   }
   while (l2 <= r2)
   {
      temp[t++] = X[l2++];
   }
   for (int i = l; i <= r; i++)
   {
      X[i] = temp[i];
   }
   
}

bool binarySearch( Students *X[], int l, int r, string sv, int &pos)
{
   if ( l <= r )
   {
      int m = ( l + r ) / 2;
      if ( (*X[m]).social == sv )
      {
         pos = m;
         return true;
      }
      else if ((*X[m]).social < sv )
         return binarySearch( X, m +1, r, sv, pos );
      else
         return binarySearch( X, l, m - 1, sv, pos);
   }
   else
      return false;
}

the segmentation fault is in the merge sort but i can tfind out how to fix it

>the segmentation fault is in the merge sort but i can tfind out how to fix it
A segmentation fault means you accessed memory beyond your boundaries. If you're using a pointer it likely means the value of the pointer is incorrect. If you're using array indexing, it likely means the value of the index is incorrect. You know the problem is in the merge sort, so the first step should be to validate the indices at every step.

it was just n -1....wow what a dumb mistake

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.