SOURCE FILE: driver.cpp
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
#include<iomanip>
#include "selectionSort.h"
#include "insertionSort.h"
#include "quicksort.h"
#include "mergesort.h"
using namespace std;
/* Leave the declaration below commented out, because
MAX_SIZE is already defined in size.h,
and size.h is included in mergesort.h,
which is included here.
However, _use_ MAX_SIZE as the size
of all your data arrays.
*/
//const int MAX_SIZE = 50000 ;
int main ()
{
string fname, label ;
ifstream inFile ; /* see pages 764-765 in Appendix G */
int howMany, idx ;
int A[MAX_SIZE] ; // declare all your data arrays to have MAX_SIZE
unsigned long dataComps = 0, dataMoves = 0 ;
/* What follows should help you understand how to do the
I/O you need to do. */
cout << "Enter Filename: " ;
cin >> fname ;
/* I'm not sure where in the book you find mention of the use of
the "c_str()" method, but you need it here, if fname is a C++ string.
The reason is that inFile.open expects an old-style C-string
as the parameter. Old style C-strings are arrays of characters
that use a null character as an end-of-string marker. The "c_str()"
method converts a C++ string into a C-string. */
inFile.open(fname.c_str()) ;
/* Note that to get the label, it makes sense to read an entire line
as one string -- and the line will include spaces. Usually white
space is taken as marking the end of a string.
Getline works in this situation. It inputs ALL characters, up to the
end of the line (eol). Getline is mentioned on the bottom of page 710.
It will not put the eol character at the end of the label string.
However getline will move past the eol in inFile, so that the next
read done from inFile will be from the beginning of the next line. */
getline (inFile, label) ; /* see page 710 */
inFile >> howMany ;
for (idx=0; idx<howMany; idx++)
{
inFile >> A[idx] ;
}
inFile.close() ;
/* selectionSort(A, howMany, dataComps, dataMoves) ; */
insertionSort(A, howMany, dataComps, dataMoves) ;
/* note difference in how these sort functions are called */
/* quicksort(A, 0, howMany-1, dataComps, dataMoves) ; */
/* mergesort(A, 0, howMany-1, dataComps, dataMoves) ; */
cout << endl << endl ;
cout << label << endl ;
cout << howMany << endl ;
for (idx=0; idx<howMany; idx++)
{
/* See page 696 for some discussion of setw(), the field width
manipulator. You will probably want to use it when you code
the part of your program that writes the counts to the user's
screen. */
cout << setw(9) << A[idx] ;
if ((idx+1)%6 == 0) cout << endl ;
else cout << ' ' ;
}
cout << endl << endl ;
return 0 ;
}