Need help with sorting integers in a file

This is an assignment my professor gave me:

Write a program that merges the numbers in two files and writes all the numbers into a third file. Your program takes input from two different files and writes its output to a third file. each input file contains a list of numbers of type int in sorted order from the smallest to the largest. After the program is run, the output file will contain all the numbers in the two input files in one longer list in sorted order from smallest to largest. Your program should define a function that is called with the two input-file streams and the output-file stream as three arguments.

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

//this function will open two input files & place the data in the output file
void combine_inputs(ifstream& in_file1, ifstream& in_file2, ofstream& out_file);


//this function will organize the numbers in ascending order
void organize(ifstream& file_to_order);

//main
int main(){
	//fstreams
	ifstream user_in1, user_in2, output_edit;
	ofstream ordered_list;

	//open the files
	//and exit program if they fail
	user_in1.open("input1.txt");
	if (user_in1.fail()){
		cout << "\nInput file 1 failed to open. Exiting";
		exit(1);
	}

	user_in2.open("input2.txt");
	if (user_in2.fail()){
		cout << "\nInput file 2 failed to open. Exiting";
		exit(1);
	}

	ordered_list.open("outputUnordered.txt");
	if (ordered_list.fail()){
		cout << "\nOutput file failed to open. Exiting";
		exit(1);
	}
	//run the combiner
	combine_inputs(user_in1, user_in2, ordered_list);
	//close the files
	user_in1.close();
	user_in2.close();
	ordered_list.close();

	//now that output file has all the numbers the program will organize them
	output_edit.open("outputUnordered.txt");
	if (output_edit.fail()){
		cout <<  "\nFile failed to open. Exiting";
		exit(1);
	}
	organize(output_edit);
	output_edit.close();
}//end of main

//combine inputs
void combine_inputs(ifstream& in_file1, ifstream& in_file2, ofstream& out_file){
	//variables
	int num1;


	//process the files
	//add all the numeric values to the output
	while (in_file1 >> num1){
		cout << "\n" <<num1;
		out_file << num1 << " ";
	}

	while (in_file2 >> num1){
		cout << "\n" <<num1;
		out_file << num1 << " ";
	}

}//end of combine 

//this function will organize the numbers
void organize(ifstream& file_to_order){
	//variables
	int current_num, compare_num;
	ofstream orderedList;
	//open file
	orderedList.open("output.txt");

	//compare the numbers
	while (file_to_order >> current_num){
			bool isSmaller = true;
			while (file_to_order >> compare_num && isSmaller == true){
				if (current_num > compare_num)
					isSmaller = false;
			}
		//if the number is smaller then add it to the final list
		if (isSmaller == true)
			orderedList << current_num;
	}
}//end of organize


I'm struggling with the second function that will organize all the numbers.
The code manages to add the first integer from file_to_order if it is less than the rest of the integers in file_to_order but the ">>" operator extracts all other values from the file. I thought about using an array for this but my professor shot down my idea (arrays are in the next chapter).
Hello nevem,

You are making more out of the program than is necessary.

Remember each input file contains a list of numbers of type int in sorted order from the smallest to the largest., so you only need the function combine_inputs which is set up correctly, but the implementation is wrong.

All you need is to read each input file and compare the two numbers then print the lowest first then the other before reading the next two numbers. Untried yet, but that should give you one file in sorted order.

Hope that helps,

Andy
Thank you for your response Andy.
So I'd have to scrap the organize function entirely and modify the combine_inputs to compare values before adding them to the file?
I could see how that would work but I'm worried about a case where the two input files would be something like this:

input1.txt : 1 10 18
input2.txt: 12 19 20

Wouldn't output file would be something like this

output.txt : 1 12 10 19 18 20


Possibly, yes. What you want to do is first read both files, then write the lowest value from the values read to the output file. Then you need to read the next item from the file where the lowest came from and then compare the numbers again. Repeat till done. If one of the files is shorter than the other you'll need to make sure you write all the contents of the larger file.


Would you be allowed to use the files in binary format?
In this case you just write both input files into the output file and finally sort the output file like an array with the seek method of the stream.
Hello nevem,

I did not use the "organize" function, but I did not have the input files that you suggested.

Without the use of an array I not sure yet how to read the file and sort it. That will take some thought and probably a nested for loop to deal with two numbers from the file. How to swap them is the tricky part.

Andy
How to swap them is the tricky part.

It's really not that tricky. But it might be easier to do a little manual experimentation.

Since your demo files each have three numbers take 6 pieces of paper with each number and place them in two piles on your desk. Each stack is sorted lowest on top highest on bottom.

1
2
3
1        12
10       19
18       20


1. Now read each file (pick up the top number).

2. Compare the two numbers in your hand.

3. Place the lower of the two on the desk in your "output" pile.

4. Now fill your empty hand (read the corresponding file) with a number from the corresponding pile.

5. Repeat until you can't read from one of the files (one of the piles is empty). (loop to step 2.)

6. Now you need to write the value remaining in your hand to the output file.

7. Now you probably have one or more numbers left in one of your piles that need to be read and then written to the output file. This can be accomplished by reading both input files and writing the values read to the output file.

Remember when the file is empty it triggers the eof() flag on the stream so that stream will no longer read anything.

Topic archived. No new replies allowed.