Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that merges the numbers in two files and writes all the numbers

ID: 641494 • Letter: W

Question

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 it output to a third file. Each input file contains a list of numbers of type int in sorted order from the smallest to 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 out - put file stream as three arguments.

here is my code; i am unsure of how to sort the arrays; any help would be appreciated!

#include
#include
using namespace std;
void merged(ifstream& fin, ifstream& fin2, ofstream& fout);

int main()
{
   //variables
   ifstream fin;
   ifstream fin2;
   ofstream fout;

   //open both files
   fin.open("test1.txt");
   fin2.open("test2.txt");
   fout.open("merged.txt");
   if(fin.fail())
   {
       cout << "Fail to open text document" << endl;
   }

   if(fin2.fail())
   {
       cout << "Fail to open text document" << endl;
   }

   merged(fin,fin2,fout);
   fin.close();
   fin2.close();
   fout.close();
   return 0;
}

void merged(ifstream& fin, ifstream& fin2, ofstream& fout)
{
   int file1[5];
   int file2[5];
   int i = 0, j = 0;


   while(!fin.eof() && !fin2.eof())
   {
       fin >> file1[i];
       fin2 >> file2[j];
       i++;
       j++;
   }

   for(int k = 0; k < 10; k++)
   {
       if(file1[k] <= file2[k])
       {
           fout << file1[k] << endl;
       }
       else
       {
           fout << file2[k] << endl;
       }
   }


}

Explanation / Answer

//Cpp program that merges two sorted files into a merged text files

#include<iostream>
#include<fstream>
using namespace std;
//function prototype
void merged(ifstream& fin, ifstream& fin2, ofstream& fout);

int main()
{
   //variables
   ifstream fin;
   ifstream fin2;
   ofstream fout;

   //open both files
   fin.open("test1.txt");
   fin2.open("test2.txt");
   fout.open("merged.txt");
   if(fin.fail())
   {
       cout << "Fail to open text document" << endl;
   }

   if(fin2.fail())
   {
       cout << "Fail to open text document" << endl;
   }

   merged(fin,fin2,fout);
   //close files
   fin.close();
   fin2.close();
   fout.close();
   system("pause");
   return 0;
}

void merged(ifstream& fin, ifstream& fin2, ofstream& fout)
{
   //Change the size of the arrays if more than 5 elements
   //are there in the files test1.txt and test2.txt
   int file1[5];
   int file2[5];
   //read sorted elements into file3 of size 10
   int file3[10];
   int m = 0, n = 0;
   int k=0;

   int i=0,j=0;


   while(!fin.eof() && !fin2.eof())
   {
       fin >> file1[m];
       fin2 >> file2[n];
       m++;
       n++;
   }

    for (i = 0; i < 10;)
   {
       if (j < 5 && k < 5)
       {
           //checking if the file1 elements are less
           //then file2 then write file1 to file merged
           if (file1[j] < file2[k])
           {
               file3[i] = file1[j];
               fout<<file3[i]<<" ";
               j++;
           }
       else
       {
          
           //otherwise write file2 to file merged
           file3[i] = file2[k];
           fout<<file3[i]<<" ";
           k++;
       }
       i++;
    }
    else if (j == 5)
   {
       //No elements are less than file1 found
       //Then write file2 elements to merged.txt
      for (; i < 10;)
      {
           file3[i] = file2[k];
           fout<<file3[i]<<" ";
           k++;
           i++;
      }
    }
    else
   {
      for (; i < 10;)
      {
       //No elements are less than file2 found
       //Then write file1 elements to merged.txt
      
        file3[i] = file1[j];
       fout<<file3[i]<<" ";
        j++;
        i++;
      }
    }
   }
}

------------------------------------------------------------------------------------------

Sample output:

test1.txt

1 5 7 8 10

test2.txt

2 3 11 14 18

Result file

merged.txt

1 2 3 5 7 8 10 11 14 18

Hope this helps you

Note: Place input files in the current working directory

or place in the text files in the visual stutio cpp project directory

where the .cpp files are exists.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote