Merging program

I am new to C++, and due to scheduling problems I've missed the first week of class and the lessons I needed to understand how to do the work we've been assigned.

"Write a program that will read two files containing two lists of ordered integers (i.e. they are sorted) and merge them into a single list. The program will ask the user to specify the names of both input files and the output file. Your program should read the files into two arrays and use a third array to store the output before writing it to a file. No sorting is required, as the two lists are already sorted!"

I have checked around the internet and asked a friend for advice, but what I have a problem understanding is reading the data into an array, and then using the third array to write it out to another file.

I'm pretty far behind in this class, and any help is greatly appreciated! Thank you.
I'm going to begin by assuming the integers are sorted in an increasing order.
Lets start with file operations. To read or write from some file(s) we will need to:
#include <fstream>
Now we can create variables of type fstream. A variable of type fstream is a way to access the contents of the file that variable is set up for. So how to set up an fstream variable:
fstream myFileVariableName("myFileName.myFileExtension");
In your program you need to ask the user for all the filenames. All of these names need to each go into a seperate string variable. So this means including input/output and string libraries. (String is optional because you can use char arrays, but I'm just gonna show string)
1
2
#include <iostream>
#include <string> 

reading the data into an array

Start by creating an array that is large enough to hold all the integers:
int list1[ 100000 ] //you probably need something less than 100000
Now the next goal would be to read the entire file that the user typed in earlier for the first list:
1
2
3
4
5
6
int i = 0;
while( list1File != EOF )
{
    list1File >> list1[ i ];
    i++;
}

And now you might be thinking "Whoa! Whoa! Slow down!"
I created an integer i to keep track of how many elements have been read so far.
And at the start 0 elements have been read.
The while loop runs until list1File reaches the end of file (EOF)
So in other words the loop runs untile there is nothing left in the file to read :)
Now inside the loop you just read one integer at a time and store it to the array.
Then increase the integer i that is keeping track of how many elements have been read so far.
In c++ arrays start at index 0, so in that example list1[0] would be the first integer in that array.

You can kind of repeat that process to read in the integers for list2; just be sure to use different variable names.

I will post more about merging the lists in a little while.
In the meantime could you try to read in all the numbers from the file?
Just let me know if you need more help reading from files.
Last edited on
Heres an outline of your program:
1
2
3
4
5
6
Declare variables(Array1 size, Array2 size)
Open up each file, increment Array1/Array2 Size until end of file is reached
loop through each file, assigning the variables from the file to the array(hint, for loop)
Create a third array by adding the arraysize of 1 & 2
loop through each array, assigning the array into the third array.
Output the array


I understand this is very broad, if you have questions feel free to ask them. Just try to write some of the program first!
Alright, so I started by getting both of the lists into two different arrays. I checked to see if it was correct by printing out the lists and it works! Now I need to add it to a third array and then print that out.

I tried using your while loop but got an error, so I changed it so it could work. Is this fine or would it be able to cause problems?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main() {
    
    ifstream firstlist("list.txt");
    ifstream secondlist("list2.txt");
    int list1[50];
    int list2[50];
    int i = 0;

    while ( firstlist.good() ) {
           firstlist >> list1[ i ];
           i++;
    }
    i= 0;
    while ( secondlist.good() ) {
           secondlist >> list2[ i ];
           i++;
    }    
}


Thank you for your help by the way, it's great to know there's such friendly help here!
Last edited on
Your while loops work. Now merging the two lists should be easy.
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
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main() {
    
    ifstream firstlist("list.txt");
    ifstream secondlist("list2.txt");
    int SIZE1 = 50, SIZE2 = 50;  //I just made these for convenience since
                                 //later list3 has to be SIZE1 + SIZE2
    int list1[SIZE1];
    int list2[SIZE2];
    int list1size = 0;
    int list2size = 0;

    while ( firstlist.good() ) {  //!firstlist.eof()
           firstlist >> list1[ list1size ];
           list1size++;
    }
    while ( secondlist.good() ) {  //!secondlist.eof()
           secondlist >> list2[ list2size ];
           list2size++;
    }
    
    int list3[SIZE1 + SIZE2]; //needs to be at least as big as list1 and list2
    int index1 = 0; //these indices will keep track of the next
    int index2 = 0; //element in list1 and list2
    for( int index3 = 0; index3 < list1size + list2size; index3++ )
    {
        if( index1 == list1size )
        {
            //done taking elements from list1, so use list2
            list3[ index3 ] = list2[ index2 ];
            index2++;
        }
        else if( index2 == list2size )
        {
            //done taking elements from list2, so use list1
            list3[ index3 ] = list1[ index1 ];
            index1++;
        }
        else if( list1[ index1 ] < list2[ index2 ] )
        {
            //the next element in list1 is less than list2, so use list1
            list3[ index3 ] = list1[ index1 ];
            index1++;
        }
        else
        {
            //the next element in list2 is less than list1, so use list2
            list3[ index3 ] = list2[ index2 ];
            index2++;
        }
        cout << list3[ index3 ] << endl;
    }
    
    return 0;
}


Wikipedia has a nice little graphic on their merge sort page that may help you picture what my code is doing here: http://en.wikipedia.org/wiki/Merge_sort

Edit: You are right my filename != EOF was definitely bogus. Its actually !filename.eof()
Last edited on
I've tried using what you've shown me, but it doesn't seem to work correctly. It seems like the program can only read off of the second list. I've tried changing the program around and looked at that article you sent me, but I can't seem to figure out what's wrong. What usually happens is when I change something so I think it'll write off of the first list, I get six digit numbers.

Sorry for having to ask again, you've been very helpful and clear, but I'm not the sharpest knife the drawer, heh.
Alright, I ended up fixing the program and now it reads in both lists and writes them out. Thanks for the help! I wouldn't have been able to do it without you!
Topic archived. No new replies allowed.