Question

In: Computer Science

Write a program in c++ that merges numbers from two files and writes all the numbers...

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

Solutions

Expert Solution

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


Related Solutions

Edit question Write a program that merges two files as follows. The two files are in...
Edit question Write a program that merges two files as follows. The two files are in the docsharing which you can download it. One file will contain usernames(usernames.txt):foster001smith023nyuyen002...The other file will contain passwords(passwords.txt):x34rdf3ep43e4rddw32eds22...The program should create a third file matching username and passwords(usernamesPasswords.txt):foster001x34rdf3esmith023p43e4rddnyuyen002w32eds22......Give the user of your programs the option of displaying you output file. CAN ANYONE SOLVE THIS IN C
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
Write a short C++ program that takes all the lines input to standard input and writes...
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed. Please use vector datatype standaard library #include <vector> Thanks
Write a php program that writes numbers to a file. The file type should be .txt...
Write a php program that writes numbers to a file. The file type should be .txt and the file name should be numbers.tx. The numbers.txt should contain 10 random integers between 1 to 100 after the file is written.
C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
Please write program in C++ format: Write a program to accept five negative numbers from the...
Please write program in C++ format: Write a program to accept five negative numbers from the user. (1) Find the average of the five numbers and display the answer to the standard output. Keep the answer two decimal points - 5 points (2) Output the numbers in ascending order and display the answer to the standard output. - 5 points
write a C++ program that uses function swap (a,b) to enter two real numbers from the...
write a C++ program that uses function swap (a,b) to enter two real numbers from the keyboard and uses function min_max(a,b) to return a to be the smaller of the two numbers entered always.
Write a C++ program to swap two numbers and show your output
Write a C++ program to swap two numbers and show your output
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
Using the Python program: a/ Write a program that adds all numbers from 1 to 100...
Using the Python program: a/ Write a program that adds all numbers from 1 to 100 to a list (in order). Then remove the multiples of 3 from the list. Print the remaining values. b/ Write a program that initializes a list with ten random integers from 1 to 100. Then remove the multiples of 3 and 5 from the list. Print the remaining values. Please show your work and explain. Thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT