Question

In: Computer Science

IN C LANGUAGE 16.16 Lab 5: merge Name this program merge.c - This program will take...

IN C LANGUAGE

16.16 Lab 5: merge

Name this program merge.c - This program will take two arguments from the command-line which will be the names of the two text files the program will read from. These text files contain a list of numbers each in ascending order. You'll open the text files and begin merging the two sets of numbers together until every unique number is printed to the screen once and in order. For example:

file1.txt file2.txt
1 2
2 4
3 6
6 7

file3.txt: 1 2 5 7 9 10 11 13 15 17 19 20 21 24 25

file4.txt: 3 4 6 8 10 11 12 14 16 18 20

Note: make sure your input text files end with an empty newline, or the last number may be skipped. Lines must end with a newline character, according to the standards we follow.

Example executions:

./a.out file1.txt file2.txt
1 2 3 4 6 7
./a.out file3.txt file4.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 24 25

Here is the solution in pseudo-code:

Read number1 from file1
Read number2 from file2
While ( not EOF for file1 AND not EOF for file2 )
    If number1 is less than number2
       Print number1 and read the next number from file1
    Else if number1 is greater than number2
       Print number2 and read the next number from file2
    Else (the numbers are the same)
       Print the number and read the next number from both files
End while
// at most one of the following two while statements will be true
While( file1 has not yet hit EOF)
   Print number1 and read the next number from file1
While( file2 has not yet hit EOF)
   Print number2 and read the next number from file2

Solutions

Expert Solution

CODE -

#include<stdio.h>

#include<stdlib.h>

int main(int argc, char *argv[])

{

    int number1, number2;

    // Opening files for reading

    FILE* f1 = fopen(argv[1], "r");

    FILE* f2 = fopen(argv[2], "r");

    // Reading first number from each file

    fscanf(f1, "%d", &number1);

    fscanf(f2, "%d", &number2);

    // Loop to run till End of file is reached in at least one of the files.

    while(!feof(f1) && !feof(f2))

    {

        // Printing the number from first file if it is smaller and then reading the next number from first file.

        if(number1<number2)

        {

            printf("%d ", number1);

            fscanf(f1, "%d", &number1);

        }

        // Printing the number from second file if it is smaller and then reading the next number from second file.

        else if(number1>number2)

        {

            printf("%d ", number2);

            fscanf(f2, "%d", &number2);

        }

        // Printing the number if both numbers are equal and then reading the next number from both the files.

        else

        {

            printf("%d ", number1);

            fscanf(f1, "%d", &number1);

            fscanf(f2, "%d", &number2);

        }  

    }

    // At most one of the following two while statements will be true

    // Printing the rest of the numbers from first file till End of File is reached

    while(!feof(f1))

    {

        printf("%d ", number1);

        fscanf(f1, "%d", &number1);

    }

    // Printing the rest of the numbers from second file till End of File is reached

    while(!feof(f2))

    {

        printf("%d ", number2);

        fscanf(f2, "%d", &number2);

    }

    // Closing the files

    fclose(f1);

    fclose(f2);

    return 0;

}

SCREENSHOTS -

CODE -

INPUT TEXT FILES -

OUTPUT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command...
IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the name of an input file and the name of an output file. The program should confirm the input and output files can be opened. If a file cannot be opened, print the error message Cannot open file '<filename>' where <filename> is the name of the file and return. fopen() will return 0 if it fails to open a file. Then, the...
make a calculator in C++ or C language that can take up to 5 values at...
make a calculator in C++ or C language that can take up to 5 values at same time
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
C++ Change the program to take user input for first name and last name for five...
C++ Change the program to take user input for first name and last name for five employees. Add a loop to read the first name and last name. // EmployeeStatic.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> #include <string> using namespace std; class Employee { public:    Employee(const std::string&, const std::string&); // constructor    ~Employee(); // destructor    std::string getFirstName() const; // return first name    std::string getLastName() const; // return...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display the area and perimeter of a rectangle with width = 4 and height = 8.
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Take the following C++ program and translate it into assembly language( pep9 ) #include using namespace...
Take the following C++ program and translate it into assembly language( pep9 ) #include using namespace std; char ch; int main() {    cin >> ch;    cout << "You inputted " << ch << endl;    ch++;    cout << "Next character is " << ch << endl; if (ch <= ‘Z’)         cout << “Could be luppercase\n”;    return 0; }
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT