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 number in two files and writes all the numbers i

ID: 3620572 • Letter: W

Question

Write a program that merges the number in two files and writes all the numbers in third file. You Program take inout from diffrent files and writes its output in to third file. Each input file contains a list of number of type int in sorted order from the smallest to largest. After the program is run, the output file will contain all the numbers i the two input file in one larger list in sorted order from amallest to largest. Your program define a function that is called with the two input file streams and the output file stream as three arguments.


(Filename: Prob_7_1)File one contains 7, 3, 16 ,18, 21,12
(Filename: Prob_7_2)File two contains 8, 4, 15 ,19, 30, 100, 150, 200, 250

Explanation / Answer

using namespace std;

void Function(ifstream& instream1,ifstream& instream2, ofstream& outstream) ;

int main()

{

ifstream instream1,instream2;

ofstream outstream;

char filename1[10],filename2[10],filename3[10];

cout<<"Enter input file1 name:";

cin>>filename1;

cout<<"Enter input file2 name:";

cin>>filename2;

cout<<"Enter output file1 name:";

cin>>filename3;

instream1.open(filename1,ios::in);

instream2.open(filename2,ios::in);

outstream.open(filename3,ios::out);

if(!instream1)

{

cout<<"Error :cannot open file. ";

return 0;

}

if(!instream2)

{

cout<<"Error :cannot open file. ";

return 0;

}

if(!outstream)

{

cout<<"Error :cannot open file. ";

return 0;

}

Function(instream1,instream2,outstream);

instream1.close();

instream2.close();

outstream.close();

system("pause");

}

void Function(ifstream& instream1,ifstream& instream2,ofstream& outstream)

{

int size,array[40],i=0;

while(!instream1.eof())

{

instream1>>array[i];

i++;

}

while(!instream2.eof())

{

instream2>>array[i];

i++;

}

size=i;

bool swap;

int temp;

do

{

swap = false;

for (int count = 0; count < (size - 1); count++)

{

if (array[count] > array[count + 1])

{

temp = array[count];

array[count] = array[count + 1];

array[count + 1] = temp;

swap = true;

}

}

} while (swap);

for(int i=0;i<size;i++)

outstream<<array[i];

}

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