Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

samle numbers1.txt 403 1699 1738 3027 3237 4433 4443 5043 5553 5845 6799 7185 79

ID: 3572746 • Letter: S

Question

samle numbers1.txt

403 1699 1738 3027 3237 4433 4443 5043 5553 5845

6799 7185 7952 8011 9719 10967 11693 11730 11952 12996

14566 21565

-------------------------------------------------------------------------------------

sample numbers2.txt

1335 1787 1998 3328 4033 4292 4907 5018 5140 5330
5793 5844 6005 6019 6063 6716 7618 9262 10225 15025

please use command line for files, complete the following code:

#include <stdio.h>

// usage: ./mergeSort numbers1.txt numbers2.txt sortedNumbers.txt
int main(int argc, char **argv)
{
if (argc != 4) {
printf ("Incorrect number of parameters. Check your parameters. ");
return -1;
}

FILE *file1, *file2, *outputFile;

file1 = fopen(argv[1], "r");
file2 = fopen(argv[2], "r");
outputFile = fopen(argv[3], "w");
double val1, val2;

// fscanf(file1, "%lf", &val1);
// fprintf(outputFile, "%lf ", val1 or val2);

// read val1 from file1
// read val2 from file2
while (!feof(file1) && !feof(file2)) {
// make sure inside this loop, only one read from file will happen
if (val1 < val2) {
// write val1 to outputFile
//read next val1 from file1
} else {
//write val2 to outputFile
// read next val2 from file2
}
}

if (feof(file1)) //write the rest values from file2 to outputFile;
else //write the rest values from file1 to outputFile;

fclose(file1);
fclose(file2);
fclose(outputFile);

return 0;
}

Write a C program mergesort.c that accepts three strings as parameters from the command line in main These three strings are names of three files with the first two files containing each a sorted list of mumbers separated by spaces. The program performs the following operation: a) open the first two files as input files for reading data in text mode b create a new text file as the output file using the third string as the file name c) read from the first file a number and store it in x d) read from the second file a number and store it in y e) append the smaller of the two numbers to the output file with a space read the next number from the input file o ifx was smaller, read from the first file the next number and put it in x o if y was smaller, read from the second file the next number and put it in y g continue with step e h) if in step the input file contains no more numbers, i e. the end of the file was reached, append the remaining numbers of the other file to the output file i) close all files and terminate the program There is a special case in step c) and d) above. If either the first file or the second file contains no numbers, i.e. the file is empty, append numbers from the corresponding second or first file to the output file, close all files, and terminate. Your program implements the classic merge operation of a merge sort using two smaller lists of already sorted numbers to generate a complete list of sorted numbers I may call the program by entering the following command on the command line mergesort numbers 1.txt numbers2.txt sortedNumbers.txt

Explanation / Answer

#include<stdlib.h>
#include<stdio.h>

int main(int argc, char **argv)       //command line arguments argc and **argv
{
if (argc != 4)
{
printf ("Incorrect number of parameters. Check your parameters. ");
return -1;
}

FILE* file1 = fopen(argv[1], "r");   //reading first file in file descriptor file1
FILE* file2 = fopen(argv[2], "r");   //reading 2nd input file in file descriptor file2
FILE* outputFile = fopen(argv[3], "w");   //craeting 3rd file for writing data

double val1,val2;

if(file1 == NULL)       //if file does not exit printing error
{
printf("1st input file doesn't exist ");
return 0;
}
if(file2 == NULL)
{
printf("2nd input file doesn't exist ");
return 0;
}

fscanf(file1, "%lf", &val1);   //scanning first number from file1 and saving in val1
fscanf(file2, "%lf", &val2);   //scanning first number from file2 and saving val2

while(!feof(file1) && !feof(file2))       //reading file1 and file untill either of the file reaches end
{
if(val1 < val2)
{
fprintf(outputFile, "%.2lf ", val1);
fscanf(file1, "%lf", &val1);
}
else
{
fprintf(outputFile, "%.2lf ", val2);
fscanf(file2, "%lf", &val2);

}
}
if(feof(file1))   //if file 1 reaches end then just copy numbers from file2 to output file
{
while(!feof(file2))
{
fscanf(file2, "%lf", &val2);
fprintf(outputFile, "%.2lf ", val2);
}
}
else       //if file 2 reaches end then just copy file 1 into output file
{
while(!feof(file1))
{
fscanf(file1, "%lf", &val1);
fprintf(outputFile, "%.2lf ", val1);
}
}
//closing all files
fclose(file1);
fclose(file2);
fclose(outputFile);

return 0;
}

Here is output of the file sortedNumbers.txt

403.00 1335.00 1699.00 1738.00 1787.00 1998.00 3027.00 3237.00 3328.00 4033.00 4292.00 4433.00 4443.00 4907.00 5018.00 5043.00 5140.00 5330.00 5553.00 5793.00 5844.00 5845.00 6005.00 6019.00 6063.00 6716.00 6799.00 7185.00 7618.00 7952.00 8011.00 9262.00 9719.00 10225.00 10967.00 11693.00 11730.00 11952.00 12996.00 14566.00 15025.00 21565.00