1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <fstream>
#include <iostream>
using namespace std;
//-----------------------------------------------------------------------------
// Sort an array 'arr' of 'n' elements in non-decreasing order.
//
void bubbleSort(int arr[], int n)
{
// this function is fine
}
//-----------------------------------------------------------------------------
// Append the contents of a text file of integers to the argument array.
// On input, '*n' indicates the initial size of the array.
// On output, '*n' indicates the final size of the array.
// It is presumed that the array can accomodate the size of the file.
//
void readFile(const char* filename, int arr[], int* n)
{
ifstream infile( filename );
if (!infile)
{
cerr << "I could not open the file " << filename << "\n";
return;
}
// This is where your code to read the integers in infile into your array.
// Remember to append the data to your array. Hence, if you succeed in
// getting an integer from the file, first increment '*n', then make 'arr[*n]' = the
// value you read.
...
}
//-----------------------------------------------------------------------------
int main()
{
// Remember, you need somewhere to put your integers.
// Make sure that your array is big enough to hold all the integers you will read.
int xs[ 1000 ];
int number_of_xs = 0;
// This is where you read each file to append the integers to the end of your array.
readFile( "inputdata1.in", xs, &number_of_xs );
readFile( "inputdata2.in", xs, &number_of_xs );
readFile( "inputdata3.in", xs, &number_of_xs );
// Now sort your array
bubbleSort( xs, number_of_xs );
// Create the output file...
ofstream outfile( "outfile.out" );
if (!outfile)
{
cerr << "I could not create the file output.out\n";
return 1;
}
// Here is where you write your sorted array back to file
for (int n = 0; n < number_of_xs; n++)
outfile << xs[ n ] << endl;
// All done!
return 0;
}
|