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: 3667587 • 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 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 using language C++.

Be sure to implement using 3 methods:

array_size <- readfile(inputArray[], instream)

outputArray_size <- sort(inputArray1[], inputArray1_size, inputArray2[], inputArray2_size, outputArray[])

?void <- writefile(outputArray[], outputArray_size)

Sample Output Below:

***Unit Test Cases ** Unit Test Case 1: Function Name - show function name here Case 1.1: explain test case 11 here (e.g-, input arguments) Case 1.1 passed Case 1.2: explain test case 1.2 here (e.g-, input arguments) Case 1.2 passed Press any key to continue Unit Test Case 2: Function Name - show function name here Case 2.1: explain test case 2.1 here (e.g-, input arguments) Case 2.1 passed Case 2.2: explain test case 2.2 here (e.g-, input arguments) Case 2.2 passed Press any key to continue Add more test cases below Press any key to continue Welcome to Xiao Qin' s sorting program Enter the first input file name: input1.txt The list of 4 numbers in file inputl.txt is 12 Enter the second input file name input2.txt The list of 6 numbers in file input2. txt is 16 23 The sorted list of 10 numbers is: 3 4 6 7 8 9 11 12 16 23 Enter the output file name: output3.txt Please check the new file - output3.txt Goodbye.

Explanation / Answer

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream file1; //declare fstream variables
ifstream file2;
ofstream outFile;

int data1, data2; //declare int variables for reading and writing ints

file1.open("textFile1.txt"); //open files for input operations
file2.open("textFile2.txt");
if (file1.fail() || file2.fail()){ //check for failure to open
cout << "Error opening file.";
}
outFile.open("out.txt"); //open out file for output operations
if (outFile.fail()){
cout << "Outfile open failed.";
}

file1 >> data1; //read file1 and first integer
file2 >> data2; //read file2 and first integer
while (!file1.eof() || !file2.eof()) //I think my problem lies here
{
if (data1 <= data2) //check if data1 <= data2
{
outFile << data1; //if so, record data1
file1 >> data1; //read file1 again
  
}
else if (data1 >= data2) //check if data1 >= data2
{
outFile << data2; //if so, record data2
file2 >> data2; //read data2 again
}
else if (file1.eof()) //check if you are at the end of file1
{
if(!file2.eof()) //while not at the end of file2,
//record data2
{
outFile << data2;
}
}
else if (file2.eof()) //check if you are at the end of file2
{
if(!file1.eof()) //while not at the end of file1,
//record data1
{
outFile << data1;
}
}
  
  
}
  
file1.close(); //close all files
file2.close();
outFile.close();
cout<<"Good Bye";
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