
Patrick B. answered 04/05/20
Math and computer tutor/teacher
STEP 1: first opens the files
Opens First File
Opens Second File
Opens Output File
Step 2: buffers the first records unless the file is empty in which case EOF is returned immediately
Reads record from file 1
Reads record from file 2
Step 3: primes the main loop
done_flag = False
file_flag = False <--- assume 2nd file shall finish before first file
Step 4: the MAIN LOOP
While Not done_flag
If rec1 <= rec2 Then
outputs first record
If Not EOF(1) Then
reads next record from file 1
Else 'EOF #1
done_flag = True
file_flag = True <-- flags that the first file finished before second file
End If 'reads from 1st file
Else 'rec1 vs rec2
outputs 2nd record
If Not EOF(2) Then
reads next record from file 2
Else 'EOF #2
done_flag = True
End If 'reads from 2nd file
End If ' rec1 vs. rec2
End While
Step 5: At this point, one of the files is finished
done_flag = False
While Not done_flag
If file_flag Then <--- first finished was done before 2nd file
writes 2nd record to output file
reads next record from 2nd file
Else <--- otherwise the 2nd file finished before the 1st file
writes 1st record to output file
reads next record from 1st file
End If
//comment: checks to see if the surviving file is finished
If file_flag Then <--- the 1st file finished before 2nd file
If EOF(2) Then done_flag = True <--- but now the 2nd file is also finished
Else
If EOF(1) Then done_flag = True <--- the 2nd file finished before first file, but now the 1st file is done
End If
End While
STEP 6: Flushed the buffers with any remaining data and writes to output file one last time
If file_flag Then <-- 1st file completed before second file, so the last record is in rec #2
writes 2nd record to output file
Else
writes 1st record to output file <--- 2nd file completed before 1st file, so last record is in rec #1
End If
STEP 7: closes the files
Close (1)
Close (2)
Close (3)