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

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

ID: 3568654 • Letter: P

Question

Problem1:

Write a program that merges the numbers in two files and writes all the numbers into 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.

Sample output (bolded text denote input from user)

Filenames are restricted to 12 characters

Enter the first of two input file names: HW10F1.txt

Now a second input file name HW10F2.txt

Enter the output file name.

WARNING: ANY EXISTING FILE WITH THIS NAME WILL BE ERASED.

output.txt

(Note that the rest of the output are optional and just displays the contents of the files so you can see the expected result)

Contents of file HW10F1.txt are:

3

5

7

10

Contents of file HW10F2.txt are:

1

2

4

6

8

Contents of merged file, output.txt are:

1

2

3

4

5

6

7

8

10

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
   string line,f1,f2;
   cout<<"enter file names"<<endl;
   cin>>f1;
   cin>>f2;
   string s[9];
  
   int i=0;
ifstream myfile ("input1.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
s[i]=line;
i++;
}
myfile.close();
}
ifstream myfile2 ("input2.txt");
if (myfile2.is_open())
{
while ( getline (myfile2,line) )
{
s[i]=line;
i++;
}
myfile2.close();
}

else cout << "Unable to open file";
int j=0;
int k=0;
int k1=0;
for(j=0;j<i;j++)
{
   for(k=0;k<i-1;k++)
   {
       if(s[k]>s[k+1])
       {
           string t=s[k];
           s[k]=s[k+1];
           s[k+1]=t;
       }
   }
}
ofstream myfile3 ("output.txt");
if (myfile3.is_open())
{
for(k1=0;k1<i-1;k1++)
   {
myfile3 << s[k1];

}

   }
myfile.close();
  
   return 0;
  
  
}

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