Hello / Good afternoon! Would you please explain for me the answer I get by adding a comment . Thanks.    Question C++ program : Two sorted files (File1.txt: 1 3 4 6 7 8 11) (File2.txt: 2  3 5 6 7 9 10 11 12 13) The C++ program going to check if the file are sorted and merge them to third file (Mergefile.txt: 1 2 3  3 4 5 6 6 7 7 8 9 10 11 11 12 13)   Program Plan: "Main.cpp" Include the requierd header files Declare the necessary function prototype and constants. Define the main() function  Under Main function: Get the first file name  from the user  Check if the first file is sorted using the function "check()" Get the second file name from the user Check if the second file is sorted using function "check()" Merge the two file using the function "merge()". Print the result in to merge file "Check.h" Define the template function necessary Define the bool check "Check if the file is sorted" Declare the necessery structure variables Define the necessary function prototype. Loop till the file is not empty Assign the first file value Assign the second and third and forth untill the reusults will show that the file is sorted  The function will not read only the first two values and print that file is sorted. It will read a few values untill the results will show that file is really sorted. "Merge.h" Define the template function necessary Define the merge "Merge the two sorted files" Declare the necessery structure variables Define the necessary function prototype. Include the required header files Assign null pointer to "merge two files" Check if the first file and the second file are not sorted and * return 0; Check if the both are sorted Make the first file to merge file make the second file to merge file Unconditional loop merge the two files Check the first file is not empty and return to the merge file check the second file is not empty and return to the merge file Condition for first file  non empty and second file non empty Make sure the Merge file is sorted  check_circle Expert Answer thumb_up   thumb_down Step 1 The program is written in C++   check.h #include #include using namespace std; bool arraySortedOrNot(int arr[], int n); bool check(char* f1) {   int a[20]; int i=0,n1; ifstream fin1; fin1.open(f1); while(!fin1.eof()) {   fin1>>n1; a[i]=n1; i++; } if(arraySortedOrNot(a,i)) { return1; } else return0; } bool arraySortedOrNot(int arr[], int n) { if (n == 1 || n == 0) return1;   if (arr[n - 1] < arr[n - 2]) return0; return arraySortedOrNot(arr, n - 1); }     Merge.h   #include #include using namespace std; void merge(char* f1,char* f2,char* f3) { ifstream fin1, fin2; ofstream fout; int n1, n2; fin1.open(f1); fin2.open(f2); fout.open(f3); fin1 >> n1; fin2 >> n2; while (!fin1.eof() && !fin2.eof()) { if (n1 < n2) { fout << n1 << endl; fin1 >> n1; //fin1.ignore(5, '\n'); } else { fout << n2 << endl; fin2 >> n2; //fin2.ignore(5, '\n'); } } while(!fin1.eof()) { fout<>n1; } while(!fin2.eof()) { fout<>n2; } fin1.close(); fin2.close(); fout.close(); }   main.cpp   #include "check.h" #include "Merge.h" int main() { char fin1[10], fin2[10], fin3[10]; cout<<"Enter the first file"<>fin1; if(check(fin1)) { cout<<"Enter the second file"<>fin2; if(check(fin2)) { cout<<"Enter the third file to store result"<>fin3;     merge(fin1,fin2,fin3); cout<<"Two files merged successfully"<

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section8.5: A Case Study: Pollen Count File Update
Problem 3E
icon
Related questions
Question
Hello / Good afternoon!
Would you please explain for me the answer I get by adding a comment . Thanks. 
 
Question

C++ program : Two sorted files (File1.txt: 1 3 4 6 7 8 11) (File2.txt: 2  3 5 6 7 9 10 11 12 13)

The C++ program going to check if the file are sorted and merge them to third file (Mergefile.txt: 1 2 3  3 4 5 6 6 7 7 8 9 10 11 11 12 13)

 

Program Plan:

"Main.cpp"

  • Include the requierd header files
  • Declare the necessary function prototype and constants.
  • Define the main() function 
  • Under Main function:
  • Get the first file name  from the user 
  • Check if the first file is sorted using the function "check()"
  • Get the second file name from the user
  • Check if the second file is sorted using function "check()"
  • Merge the two file using the function "merge()".
  • Print the result in to merge file

"Check.h"

  • Define the template function necessary
  • Define the bool check "Check if the file is sorted"
  • Declare the necessery structure variables
  • Define the necessary function prototype.
  • Loop till the file is not empty
  • Assign the first file value
  • Assign the second and third and forth untill the reusults will show that the file is sorted 
  • The function will not read only the first two values and print that file is sorted. It will read a few values untill the results will show that file is really sorted.

"Merge.h"

  • Define the template function necessary
  • Define the merge "Merge the two sorted files"
  • Declare the necessery structure variables
  • Define the necessary function prototype.
  • Include the required header files
  • Assign null pointer to "merge two files"
  • Check if the first file and the second file are not sorted and * return 0;
  • Check if the both are sorted
  • Make the first file to merge file
  • make the second file to merge file
  • Unconditional loop merge the two files
  • Check the first file is not empty and return to the merge file
  • check the second file is not empty and return to the merge file
  • Condition for first file  non empty and second file non empty
  • Make sure the Merge file is sorted 
check_circle

Expert Answer

thumb_up
 
thumb_down
Step 1

The program is written in C++

 

check.h

#include<iostream>
#include <fstream>
using namespace std;
bool arraySortedOrNot(int arr[], int n);
bool check(char* f1)
{
 
int a[20];
int i=0,n1;
ifstream fin1;
fin1.open(f1);
while(!fin1.eof())
{
 
fin1>>n1;
a[i]=n1;
i++;

}
if(arraySortedOrNot(a,i))
{
return1;
}
else
return0;
}
bool arraySortedOrNot(int arr[], int n)
{

if (n == 1 || n == 0)
return1;
 
if (arr[n - 1] < arr[n - 2])
return0;

return arraySortedOrNot(arr, n - 1);
}
 
 
Merge.h
 
#include <iostream>
#include <fstream>
using namespace std;
void merge(char* f1,char* f2,char* f3)
{
ifstream fin1, fin2;
ofstream fout;
int n1, n2;

fin1.open(f1);
fin2.open(f2);
fout.open(f3);

fin1 >> n1;
fin2 >> n2;

while (!fin1.eof() && !fin2.eof())
{
if (n1 < n2)
{
fout << n1 << endl;
fin1 >> n1;
//fin1.ignore(5, '\n');
}
else
{
fout << n2 << endl;
fin2 >> n2;
//fin2.ignore(5, '\n');
}
}
while(!fin1.eof())
{
fout<<n1<<endl;
fin1>>n1;

}
while(!fin2.eof())
{
fout<<n2<<endl;
fin2>>n2;

}
fin1.close();
fin2.close();
fout.close();
}
 
main.cpp
 
#include "check.h"
#include "Merge.h"

int main()
{
char fin1[10], fin2[10], fin3[10];
cout<<"Enter the first file"<<endl;
cin>>fin1;
if(check(fin1))
{
cout<<"Enter the second file"<<endl;
cin>>fin2;
if(check(fin2))
{
cout<<"Enter the third file to store result"<<endl;
cin>>fin3;
 
 
merge(fin1,fin2,fin3);
cout<<"Two files merged successfully"<<endl;
}
}
if(check(fin3))
{
cout<<"The merged file is sorted"<<endl;
}
}
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Header Files
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage