#include <iostream>
#include <time.h>
#include <fstream>
#define MAXSIZE 400000
#include <cstring>
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;
Students * allInfo[400000] = {new Students()};
int n = 0;
string social;
string firstName;
string lastName;
double gpa;
int position;
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;
}
I can't seem to figure out why it is giving me the fault. The program is designed to go through a file and sort it by the social security number but it gives me the fault during the sort routine.????