In: Computer Science
Write a program in c++ that merges numbers from two files and writes all the numbers into a third file in ascending order. Each input file contains a list of 50 sorted double floating-point numbers from the smallest to largest. After the program is run, the output file will contain all 100 numbers between in the two input files, also sorted from smallest to largest. Format the output into two columns – the first column contains the numbers 1-100, and the second column contains the appropriate number from the text file. Also print the results to the screen.
Specifications:
The input file names are called “numbers1.txt” and “numbers2.txt”, and can be found at the bottom of the page. Copy and paste the numbers from each text file into a “numbers1.txt” and “numbers2.txt” within your project directory. This can be done by creating a project with a main.cpp source file, compiling it, launching a terminal window, and using nano to create a text file where the numbers can be copied into (separately for each file). Once the numbers are copied into a generic text file, save it with the appropriate filename and exit nano.
The program should use two functions with the following function prototypes:
void checkFailure(ifstream &fin1, ifstream &fin2, ofstream &fout); void sortNumbers(ifstream &fin1, ifstream &fin2, ofstream &fout);
The checkFailure() function should accept the two input-file streams and the output-file stream as call-by-reference arguments. The function should make sure that the files were opened properly and exit the program with EXIT_FAILURE if necessary.
If a file fails to open, the error should read:
Input file “[filename]” failed to open.
[filename] represents the file that failed to open. For instance, if numbers2.txt fails to open, the error message to be printed to the screen is:
Input file “numbers2.txt” failed to open.
The sortNumbers() function should also accept the two input-file streams and the output-file stream as call-by-reference arguments. The function should read in the values from the input files (without using an array) and compare values one at a time to determine the smallest value between any pair of numbers. As a new number is read in, the smallest value should be written to the output file.
The first column written to the output file should be an integer between 1-100, representing which value is being represented, since there are a 100 numbers total. The integer values 1 - 100 should be printed with a width of 3 and right justified.
The second column written to the output file should be the appropriate double floating-point number. Print each number with a precision of 8 values after the decimal point. The two columns should be separated by a tab.
The final output should print the results to the screen, and to a file called “sorted.txt”.
NOTE: When you first read in the first two numbers, one of them is the absolute minimum, and is the first number written to the file. If that number came from numbers1.txt, then the next number to be read in should also be from numbers1.txt. If the new number read in is still smaller than the other number from numbers2.txt, write it to the file and repeat the process until the next number read in from numbers1.txt is no longer the smallest number. Then write the number from numbers2.txt to the file and read in a new value from numbers2.txt. Repeat the process as necessary (i.e., read in a new number one at a time from the appropriate data file) to ensure each value is written to the data file in the correct order. If/When you run out of numbers from one of the input data files, any remaining numbers in the second input data file should be read in and written to the output file directly, since comparisons are no longer necessary.
As an example, if you execute the program with the following underlined inputs, the output will be:
~> main.o 1 0.00476587 2 0.00952649 3 0.01107230 4 0.01862460 5 0.02598610 ... 99 0.98603100 100 0.99549800 ~> more sorted.txt 1 0.00476587 2 0.00952649 3 0.01107230 4 0.01862460 5 0.02598610 ... 99 0.98603100 100 0.99549800 ~>
numbers1.txt
0.00476587
0.00952649
0.0110723
0.0186246
0.0259861
0.0328688
0.0861446
0.0948271
0.112295
0.159563
0.184204
0.244189
0.248323
0.293049
0.308473
0.34332
0.407723
0.408518
0.417362
0.421145
0.456012
0.485628
0.585623
0.600914
0.610335
0.621299
0.651852
0.658051
0.673929
0.680218
0.689137
0.693944
0.705223
0.710375
0.726621
0.741053
0.7481
0.761997
0.788303
0.795886
0.812099
0.832322
0.848749
0.870877
0.928862
0.945333
0.962629
0.96473
0.971811
0.986031
numbers2.txt
0.00476587
0.00952649
0.0110723
0.0186246
0.0259861
0.0328688
0.0861446
0.0948271
0.112295
0.159563
0.184204
0.244189
0.248323
0.293049
0.308473
0.34332
0.407723
0.408518
0.417362
0.421145
0.456012
0.485628
0.585623
0.600914
0.610335
0.621299
0.651852
0.658051
0.673929
0.680218
0.689137
0.693944
0.705223
0.710375
0.726621
0.741053
0.7481
0.761997
0.788303
0.795886
0.812099
0.832322
0.848749
0.870877
0.928862
0.945333
0.962629
0.96473
0.971811
0.986031
SOLUTION-
I have solve the problem in C++ code with
output screenshot for easy understanding :)
CODE-
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<fstream>
using namespace std;
void checkFailure(ifstream &fin1, ifstream &fin2);
void inputData(ifstream &fin1, ifstream &fin2, double a[], int n);
void sortData(double a[], int n);
void printResults(const double a[], int n);
int main(){
double a[100];
int n = 100;
ifstream fin1, fin2;
checkFailure(fin1, fin2);
inputData(fin1, fin2, a, n);
sortData(a, n);
printResults(a, n);
return 0;
}
void checkFailure(ifstream &fin1, ifstream &fin2){
fin1.open("number1.txt");
if(fin1.fail()){
cout<<"Input file \"numbers1.txt\" failed to open."<<endl;
exit(EXIT_FAILURE);
}
fin2.open("number2.txt");
if(fin2.fail()){
cout<<"Input file \"numbers2.txt\" failed to open."<<endl;
exit(EXIT_FAILURE);
}
}
void inputData(ifstream &fin1, ifstream &fin2, double a[], int n){
int i=0;
while(fin1>>a[i]){
i++;
}
while(fin2>>a[i]){
i++;
}
}
void sortData(double a[], int n){
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
if(a[i]>a[j]){
double temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void printResults(const double a[], int n){
cout<<setprecision(8)<<fixed;
for(int i=0; i<n; i++){
cout<<setw(3)<<(i+1)<<"\t"<<a[i]<<endl;
}
}
SCREENSHOT-
output file -
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------