In C++ And here are the texts Numb 1 0.00476587 0.00952649 0.0110723 0.0186246 0
ID: 3602322 • Letter: I
Question
In C++ And here are the texts Numb 1 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 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.986031Numb 2 0.0462637 0.0464418 0.0550811 0.0563323 0.0572136 0.105148 0.106926 0.258089 0.259395 0.273203 0.288239 0.300588 0.302576 0.310436 0.32071 0.327535 0.333724 0.349157 0.356483 0.368427 0.393754 0.394549 0.420565 0.420742 0.421271 0.453418 0.455615 0.464788 0.467221 0.537545 0.56808 0.589801 0.603384 0.61161 0.658521 0.660949 0.663934 0.669467 0.683455 0.68472 0.725062 0.750932 0.763951 0.81451 0.890713 0.918466 0.936566 0.941609 0.947487 0.995498 0.0462637 0.0464418 0.0550811 0.0563323 0.0572136 0.105148 0.106926 0.258089 0.259395 0.273203 0.288239 0.300588 0.302576 0.310436 0.32071 0.327535 0.333724 0.349157 0.356483 0.368427 0.393754 0.394549 0.420565 0.420742 0.421271 0.453418 0.455615 0.464788 0.467221 0.537545 0.56808 0.589801 0.603384 0.61161 0.658521 0.660949 0.663934 0.669467 0.683455 0.68472 0.725062 0.750932 0.763951 0.81451 0.890713 0.918466 0.936566 0.941609 0.947487 0.995498 Write a program that merges numbers from two files and prints the eats = acndng ordet Each ntins a list of 50 sorted double floating-point nambers from the smallest to largest output into two columns- the first column contains the numbers 1-100, and the second colum" cunoas the appropriate number from the text files. Usen the problem ay and a sort function to solve The input file names are caled "numbersl.txt and numbers2.txt and can be found on the websate for ECE 71. Copy and paste the numbers from each test filt=toa"numbers 1 . txt-and numbers2.txt" within your project direclory. This can be done by creating a peoject 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). Oace the mumbers are copied into a generic text file, save it with the appropriate filename and exit nano The program should use four functions with the following function peototypes vold checkFailure(fst.ream &fini; fstrean sfin2) a vold input Data (ifstream fin2ifstream in2 double at1. int n) a vold sortbata (double all int n)a vold printResults (conat double all Int nla 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 thait the files were opened properly and exit the program with EXIT FAILURE if nocessary If a file fails to open, the error should read Input file filename]" failed to open Ifilename] ropresents the file that failed so open. For instance, if numbers2.txt fails to open. emor message to be printed to the screen is Input filo "nunbera2.txt" failed tepen. ethe results in the The input Data( ) function should read in numbers from the 2 duta files and 100 clement array. The values can be read into the aray in any ondex The sort function should sort the numbers in the array. as appropr The printResults0 function should print the sorted numbers wsing two colum The fi column printed to the screen should be an integer between 1-100, indicating 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 writen to the output file should be the appropriate double flouting-point mumber Print each number with a precision of 8 values after the decimal point The two collumns should be separated by a tab. Asan example, if you execute the program with the following undalind input árouwa will bc >main.e 0.00476587 0.00952649 0.01107230 0.01862460 .02598610 0.98603100 .99549800 Develop your L'O diagram and pseudocode, debug your code, and subemit to the Grader Program
Explanation / Answer
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
using namespace std;
void checkFailure(ifstream &fin1, ifstream &fin2){
if (!fin1){
cout << "Input file "number1.txt" failed to open ";
exit(EXIT_FAILURE);
}
if (!fin2){
cout << "Input file "number2.txt" failed to open ";
exit(EXIT_FAILURE);
}
}
void inputData(ifstream &fin1, ifstream &fin2, double a[] ,int n){
for (int i = 0; i < n; i++){
if (i < 50)
fin1 >> a[i];
else
fin2 >> a[i];
}
}
void sort(double a[] ,int n){
double temp;
for (int i = 0; i < n; i++){
for (int j=i+1; j<n; j++){
if (a[i] > a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void printReaults(double a[] ,int n){
for (int i = 0; i < n; i++){
cout << setw(3) << i <<" " << setprecision(8) << a[i] << endl;
}
}
int main(){
double a[100];
ifstream fin1("number1.txt");
ifstream fin2("number2.txt");
checkFailure(fin1,fin2);
inputData(fin1,fin2,a,100);
sort(a,100);
printReaults(a,100);
return 0;
}