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

CODE in C++ PLEASE code the program in more basic C++ Write a program that merge

ID: 3733787 • Letter: C

Question

CODE in C++ PLEASE code the program in more basic C++

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. Your program should define at least one function that is called with the two input-file streams and the output-file stream as three arguments. The list of numbers are not necessary of the same length. Aske the user for each file name and test if the file exit. Show the content of all three files on a screen

Sample input file 1:

22

44

55

77

99

500

Sample input file 2:

11

55

101

Sample output file:

11

22

44

55

55

77

99

101

500

This is how the complied program should look

hi le Entering Tile names,please enter the Tile name fol lowed byextension. Enter the first of two input file names: inNamel.txt Now a second input file name i nName2.txt Enter the output file name WARNING: ANY EXISTING FILE WITH THIS NAME WILL BE ERASED outName.txt Contents of file inName1.txt are: 500 Contents of file inName2.txt are: 101 Contents of merged file, outName.txt are: 101 500 Press any key to continue .. .

Explanation / Answer

PROGRAM

#include<iostream>

#include<fstream>

#include<malloc.h>

using namespace std;

// implement bubble sort function

void sort(int a[],int size)

{

for(int i=0;i<size;i++) // list of elements

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

// check condition first and second so on.. if first element is big then swapping of two elements

if(a[j]>a[j+1])

{

// swapping of two elements

int t=a[j];

a[j]=a[j+1];

a[j+1]=t;

}

}

// Create and implementing readnWrite() function take parameters

// character with pointers inFile1,inFile2,outFile

void readnWrite(char *inFile1,char *inFile2,char *outFile)

{

ifstream in1,in2; // Create ifstream objects in1,in2

ofstream out; // Create ofstream object out

// open files

in1.open(inFile1);

in2.open(inFile2);

out.open(outFile);

// check condition whether the files are exist or not

if(in1==NULL||in2==NULL||outFile==NULL)

{

cout<<"Could not open Files.. ";

return;

}

int *a; // declare integer pointer a for storing merging of two array elements

int x,y,counter=0,i=0; // declare and initialize integer elements

a=(int *)malloc(sizeof(int)); // allocate memory of integer pointer a

cout<<" Contents of file "<<inFile1<<" are: ";

while(in1>>x){ // this while for reading first file (inFile1) content until end of file

out<<x<<endl; // storing first file content into outFile

cout<<x<<endl; // display all the element from the first file

}

cout<<" Contents of file "<<inFile2<<" are: ";

while(in2>>y){ // simillarly, reading second file (inFile2) content until end of file

out<<y<<endl; // storing second file content into outFile

cout<<y<<endl;

}

// first close all the open files

in1.close();

in2.close();

out.close();

// again open outFile in two modes read and write

ifstream myout; // read

ofstream myout1; // write

myout.open(outFile); // open read mode

while(myout>>x) // read all the element in the outFile

{

counter++; // this counting number of elements in the out file

a[i++]=x; // store all the elements into array "a"

}

sort(a,counter); // call the function sort()

myout1.open(outFile); // open the same outFile in wirte mode

cout<<" Contents of merged file, "<<outFile<<" are: ";

for(i=0;i<counter;i++)

{

myout1<<a[i]<<endl; // store all the sorted elements into outFile file

cout<<a[i]<<endl; // display all the sorted element in the screen

}

  

// close the all the files.

myout.close();

myout1.close();

}

int main()

{

char input1[20],input2[20],output[20];

// input all the files

cout<<" While Entering File Names, Please enter the file name Followed by extension ";

cout<<"Enter the First of two input file names: ";

cin>>input1;

cout<<" Now a second input file name ";

cin>>input2;

cout<<" Enter the output file name. ";

cout<<" WARNING: ANY EXISTING FILE WITH THIS NAME WILL BE ERASED. ";

cin>>output;

readnWrite(input1,input2,output); // call readnWrite() function

return 0;

}

OUTPUT-1


While Entering File Names, Please enter the file name
Followed by extension

Enter the First of two input file names:
f:\input1.txt

Now a second input file name
f:\input2.txt

Enter the output file name.

WARNING: ANY EXISTING FILE WITH THIS NAME WILL BE ERASED.
f:\myoutput.txt

Contents of file f:\input1.txt are:
22
44
55
77
99
500

Contents of file f:\input2.txt are:
11
55
101

Contents of merged file, f:\myoutput.txt are:
11
22
44
55
55
77
99
101
500

OUTPUT-2


While Entering File Names, Please enter the file name
Followed by extension

Enter the First of two input file names:
ppp

Now a second input file name
ppp

Enter the output file name.

WARNING: ANY EXISTING FILE WITH THIS NAME WILL BE ERASED.
mmm
Could not open Files..

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