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

txt 1: 403 1699 1738 3027 3237 4433 4443 5043 5553 5845 6799 7185 7952 8011 9719

ID: 3571227 • Letter: T

Question

txt 1:

403 1699 1738 3027 3237 4433 4443 5043 5553 5845

6799 7185 7952 8011 9719 10967 11693 11730 11952 12996

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

txt 2:

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

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

//C code

#include <stdio.h>
#include <stdlib.h> // For exit() function


int main(int argc, char const *argv[])
{
int number1;
int number2;
FILE *infptr1;
FILE *infptr2;
FILE *outfptr;

if ((infptr1 = fopen(argv[1], "r")) == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);   
}

if ((infptr2 = fopen(argv[2], "r")) == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);   
}

if ((outfptr = fopen(argv[3], "w")) == NULL)
{
printf("Error!");
exit(1);   
}

// check if one of the file is empty
if(fscanf(infptr1, "%d", &number1) == -1)
{
while(fscanf(infptr2, "%d", &number2) == 1)
fprintf(outfptr,"%d ", number2);

return 0;
}
if(fscanf(infptr2, "%d", &number2) == -1)
{
while(fscanf(infptr1, "%d", &number1) == 1)
fprintf(outfptr,"%d ", number1);

return 0;
}
  
int flag;
while (1)
{
  
if(number1 < number2)
{
fprintf(outfptr,"%d ", number1);
if(fscanf(infptr1, "%d", &number1) == -1)
{
flag = 1;
break;
}
}

else
{
fprintf(outfptr,"%d ", number2);
if(fscanf(infptr2, "%d", &number2) == -1)
{
flag = 2;
break;
}
}
}
  
if(flag == 1)
fprintf(outfptr,"%d ", number2);
else
fprintf(outfptr,"%d ", number1);


while(fscanf(infptr2, "%d", &number2) == 1)
{
fprintf(outfptr,"%d ", number2);
}

while(fscanf(infptr1, "%d", &number1) == 1)
{
fprintf(outfptr,"%d ", number1);
}


fclose(infptr1);
fclose(infptr2);
fclose(outfptr);

  
return 0;
}

/*
number1.txt
403 1699 1738 3027 3237 4433 4443 5043 5553 5845
6799 7185 7952 8011 9719 10967 11693 11730 11952 12996

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

sortedNumbers.txt
403 1335 1699 1738 1787 1998 3027 3237 3328
4033 4292 4433 4443 4907 5018 5043 5140 5330
5553 5793 5844 5845 6005 6019 6063 6716 6799
7185 7618 7952 8011 9262 9719 10225 10967 11693
11730 11952 12996 15025
*/