My directions are to create a program that will read two files containing a sorted number of integers and merge them into one with all the integers sorted in descending order. I am having a problem with the algorithm that does the sorting.
I am just stuck with this thing. I cannot find any references for this type of program without it having something about an array. I am guessing you would use merge sort here but I am not sure of how to get the two files into one while sorting the integers in descending order.
I made some quick but not polished or tested code to serve as an example. And the files probably were text files, not binary as mine assumes. Notice how it works, it looks at numbers from each lists, compares them, then puts the higher one into the new file. It then looks at the next number of the file where the number was taken, and compares that with the other number. This repeats till one file is empty, then it fills it with what's left of the other file.
cout << "This program will compare two files containing numbers\n"
<< "and write the combination to an output file in descending order.\n";
cout << "Enter the input file name (maximum of 15 characters):\n";
cin >> fileA;
cout << "Enter the input file name (maximum of 15 characters):\n";
cin >> fileB;
cout << "Enter the output file name (maximum of 15 characters):\n";
cin >> fileC;
cout << "I will read numbers from the files: \n";
cout << fileA " and"<< fileB " then \n";
cout << "write the numbers in descending order to \n";
cout << fileC << endl;
in_stream1.open(fileA);
if (in_stream1.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
in_stream2.open(fileB);
if (in_stream2.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
out_stream.open(fileC);
if (out_stream.fail( ))
{
cout << "Output file opening failed.\n";
exit(1);
}
merge_sort(arrayA, arrayB, arrayC);
in_stream.close();
out_stream.close();
cout<<"End of Program.\n";
return 0;
}
void merge_sort(ifstream& arrayA, ifstream& arrayB, ofstream& arrayC)
{
int indexA = 0; // initialize the Array Indices
int indexB = 0;
int indexC = 0;
while((indexA < arrayA.length( )) && (indexB < arrayB.length( ))
{
if (arrayA[indexA] < arrayB[indexB])
{
arrayC[indexC] = arrayA[indexA];
indexA++; //increase the index
}
else
{
arrayC[indexC] = arrayB[indexB];
indexB++; //increase the index
}
indexC++; //move to the next position in the new array
}
// Push remaining elements to end of new array when 1 feeder array is empty
while (indexA < arrayA.length( ))
{
arrayC[indexC] = arrayA[indexA];
indexA++;
indexC++;
}
while (indexB < arrayB.length( ))
{
arrayC[indexC] = arrayB[indexB];
indexB++;
indexC++;
}
}
return;
}
In other words, why dance back and forth between the two input files? Chuck all the contents of both into a vector, sort it, reverse it, and output it to the third file, eliminating duplicate values.
well, I think that kind of misses the point of the assignment, though it would work. And, you can sort the vector in reverse order using: sort (v.begin(), v.end(), greater<int>)
You can do this using two boolean functions more () and copy() providing the two files to be merged are sorted in increasing order. The bool functions would look like: