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: XXXXXXXXXXFile2.txt: 2 XXXXXXXXXX13) The C++...




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="" -="">

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 <>

{

fout < n1=""><>

fin1 >> n1;

//fin1.ignore(5, '\n');

}

else

{

fout < n2=""><>

fin2 >> n2;

//fin2.ignore(5, '\n');

}

}

while(!fin1.eof())

{

fout<><>

fin1>>n1;



}

while(!fin2.eof())

{

fout<><>

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=""><>

cin>>fin1;

if(check(fin1))

{

cout<"enter the="" second=""><>

cin>>fin2;

if(check(fin2))

{

cout<"enter the="" third="" file="" to="" store=""><>

cin>>fin3;



merge(fin1,fin2,fin3);

cout<"two files="" merged=""><>

}

}

if(check(fin3))

{

cout<"the merged="" file="" is=""><>

}

}








Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here