16.13 Homework9 (due Apr 8) 16.14 Homework 10 (due April 20) Students This conte
ID: 3821560 • Letter: 1
Question
16.13 Homework9 (due Apr 8) 16.14 Homework 10 (due April 20) Students This content is controlled by your instructor, and is not zyBooks content. Direct questions or concerns about this content to your instructor If you have any technical issues with the zyLab submission system, use the "Trouble with lab?" button at the bottom of the lab Note: Manual grading will be performed; passing the single test case does not mean you will get all the points 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 END OF PROGRAM Contents of file HW10F1.txt are: 3 5 7 10 Contents of file HW10F2.txt are: 1 2 4 68 Contents of merged file, output txt are: 1 2 3 4 5 6 7 8 10 Lab 16.14.1: Homework 10 (due April 20) Submission main.cpp ad default template 1 #include Kiost ream #includeExplanation / Answer
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
void bubbleSort(vector<int> &v);
void merge(ifstream& input1, ifstream& input2, ofstream& output);
int main() {
string filename1,filename2,output;
cout<<"Filenames are restricted to 12 character"<<endl;
cout<<"Enter the first of two input file names: ";
cin >> filename1;
cout<<"Now a second input file name: ";
cin >> filename2;
cout<<"Enter the output file name: ";
cin >> output;
ifstream inputFile1, inputFile2;
inputFile1.open(filename1.c_str());
inputFile2.open(filename2.c_str());
ofstream outputFile;
outputFile.open (output.c_str());
merge(inputFile1, inputFile2, outputFile);
inputFile1.close();
inputFile2.close();
outputFile.close();
return 0;
}
void merge(ifstream& input1, ifstream& input2, ofstream& output){
vector<int> v;
int n;
cout<<"Contents of first file: "<<endl;
if (input1.is_open()) {
while (!input1.eof()) {
input1 >> n;
cout<<n <<" ";
v.push_back(n);
}
}
cout<<endl;
cout<<"Contents of second file: "<<endl;
if (input2.is_open()) {
while (!input2.eof()) {
input2 >> n;
cout<<n <<" ";
v.push_back(n);
}
}
cout<<endl;
bubbleSort(v);
cout<<"Contents of merged file: "<<endl;
for(int i=0; i<v.size(); i++) {
cout<<v[i]<<" ";
output<<v[i]<<" ";
}
cout<<endl;
}
void bubbleSort(vector<int> &v) {
int n =v.size();
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(v[j-1] > v[j]){
//swap the elements!
temp = v[j-1];
v[j-1] = v[j];
v[j] = temp;
}
}
}
}
Output:
h-4.2$ g++ -o main *.cpp
sh-4.2$ main
Filenames are restricted to 12 character
Enter the first of two input file names: HW10F1.txt
Now a second input file name: HW10F2.txt
Enter the output file name: output.txt
Contents of first file:
3 5 7 10
Contents of second file:
1 2 4 6 8
Contents of merged file:
1 2 3 4 5 6 7 8 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.