Merge two lists!

This is a discussion on Merge two lists! within the C++ Programming forums, part of the General Programming Boards category; Write a program that merges the numbers in two files and writes all the numbers into a third file.Each input ...

Merge two lists!

Write a program that merges the numbers in two files and writes all the numbers into a third file.Each input list contains a list of numbers of type int in sorted order from the smallest to the largest.After running this program,then the new list should contain the all the numbers in the two input files and in sorted from smallest to largest.We must use a function which contains two input-file streams and one output-file stream.

I ask my professor to give me some hint,he told me like this:

a1 b1
a2 b2
. .
. .
. .

since a1<a2<......,b1<b2<.....,so we just compare a1 and b1,if a1<b1 then write a1 to the third list,then compare a2 and b1.

It sounds that it is a very easy problem,but how to apply this?
I think we can do this:

but how to do compare a2 with b1(or b2 with a1)?we use two arrays to store these numbers?

At first,I think we can do this:first input all the two lists into the third file,so we have a new
total list which contains the whole numbers,then sort only this list,of course it will be a bad
program since it will cost more time to finish the task.

You missed adding the code, I think. But if the lists are of different size doesn't matter - you are going to reach one end or the other. If there's anything left in one file only, then that must be appended to the output file - because it's obviously larger than the values in the other file.

how do you know where to stop?there maybe different numbers in the two lists and you don't know the size.

like some c ode like this:

That is simple to tell, since there's a function called eof() in ifstream, which returns true if you're at the end of file and false otherwise.

And yes, I could easily post the code for this, but I doubt your professor would appreciate it. At least try implementing the algorithm I suggested, if you don't get it to work then we can look at it again and point out what's wrong.