Code:
//main
// openFiles
// populateInputArray
// mergeSortedArrays
// selSort
// findIndexOfMin
// exchange
// saveResultsToOutputFile
/* The format of the input data files is multiple lines, each containing one or more double values:
12345.44 90.1
23456.1
31245 66 567.11
......
*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX_SIZE = 100; // max size of data file and arrays
const bool DEBUG = true; // used during testing
//const bool DEBUG = false; // used during production
void openFiles (ifstream &inFile1, ifstream& inFile2, ofstream& outFile);
void populateInputArray (ifstream& inFile, double array[], int& arraySize);
void selSort (double array[], int arraySize);
int findIndexOfMin(const double x[], int startIndex, int endIndex);
void exchange (double& a1, double& a2);
void mergeSortedArrays (double array1 [], int array1Size, double array2 [], int array2Size, double array3[], int& array3Size);
void saveResultsToOutputFile (ofstream& outFile, double array [], int arraySize);
int main()
{
ifstream in1, in2;
ofstream out;
double array1[MAX_SIZE];
double array2[MAX_SIZE];
double array3[MAX_SIZE];
int array1Size, array2Size, array3Size;
openFiles (in1, in2, out);
populateInputArray (in1, array1, array1Size);
populateInputArray (in2, array2, array2Size);
mergeSortedArrays(array1, array1Size, array2, array2Size, array3, array3Size);
selSort(array1, array1Size);
selSort(array2, array2Size);
saveResultsToOutputFile (out, array3, array3Size);
in1.close();
in2.close();
return 0;
}
void openFiles (ifstream& inFile1, ifstream& inFile2, ofstream& outFile)
{
string fileName;
cout << "Enter name of the first input file" << endl;
cin >> fileName;
inFile1.open (fileName.c_str());
if (inFile1.fail())
{
cout << "Could not open file " << fileName << endl;
cout << "Exiting program." << endl;
exit (0);
}
cout << "Enter name of the second input file" << endl;
cin >> fileName;
inFile2.open (fileName.c_str());
if (inFile2.fail())
{
cout << "Could not open file " << fileName << endl;
cout << "Exiting program." << endl;
exit (0);
}
cout << "Enter name of the output file" << endl;
cin >> fileName;
outFile.open (fileName.c_str());
if (outFile.fail())
{
cout << "Could not open file " << fileName << endl;
cout << "Exiting program." << endl;
exit (0);
}
}
void populateInputArray (ifstream& inFile, double array[], int& arraySize)
{
for (int i = 0; i < MAX_SIZE; i++)
{
if (inFile.eof())
{
break;
}
else
{
inFile >> array[i];
}
}
arraySize = i;//gets size of array
}
void mergeSortedArrays (double array1 [], int array1Size, double array2 [], int array2Size, double array3[], int& array3Size)
{
int count = 0;
int a;
while ((count < array1Size) && (count < array2Size))
{
for (a = 0; a < MAX_SIZE; a++)
{
if (array1[a] < array2[a])
{
array3[a] = array1[a];
}
else
{
array3[a] = array2[a];
}
if (array1[a] == array2[a])
{
array3[a] = array3[a];
}
count++;
}
}
// at this point one of the arrays has been completely read
cout << "One of the arrays is empty" << endl;
if ( count < array1Size)
{
for (int b = a+1; b < array1Size; b++)
{
array3[b] = array1[b];
count++;
}
}
if ( count < array2Size)
{
for (int c = a+1; c < array2Size; c++)
{
array3[c] = array2[c];
count++;
}
}
array3Size = count;//gets size of array
}
void selSort(double array[], int arraySize)
{
// Local data ...
int minSub; // subscript of each smallest item
for (int i = 0; i < arraySize-1; i++)
{
// Find index of smallest element in unsorted section of
// items.
minSub = findIndexOfMin(array, i, arraySize-1);
// Exchange items at position minSub and i
exchange(array[minSub], array[i]);
//cout << 'exchange' << '\n';
}
}
int findIndexOfMin
(const double array[], // IN: array of elements
int startIndex, // IN: subscript of first element
int endIndex) // IN: subscript of last element
{
// Local data ...
int minIndex; // index of the smallest element found
int i; // index of the current element
// Validate subarray bounds
if ((startIndex < 0) || (startIndex > endIndex))
{
cerr << "Error in subarray bounds" << endl;
return -1; // return error indicator
}
// Assume the first element of subarray is smallest and check
// the rest.
// minIndex will contain subscript of smallest examined so far.
minIndex = startIndex;
for (i = startIndex + 1; i <= endIndex; i++)
if (array[i] < array[minIndex])
minIndex = i;
// All elements are examined and minIndex is
// the index of the smallest element.
return minIndex; // return result
} // end findIndexOfMin
void exchange (double& smElem, double& value2)
{
double valueHolder;//holds the value of the subscript of the smallest element
valueHolder = smElem;
smElem = value2;
value2 = valueHolder;
}
void saveResultsToOutputFile (ofstream& outFile, double array[], int arraySize)
{
for (int i = 0 ; i < arraySize; i++)
{
outFile << array[i] << '\n';
}
outFile.close();
}