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

You are given two files “HW6Prob2a.dat” and “HW6Prob2b.dat” with numbers in them

ID: 3808709 • Letter: Y

Question

You are given two files “HW6Prob2a.dat” and “HW6Prob2b.dat” with numbers in them. One file MAY have more numbers than the other. Write a program to read the numbers from two files and find the sum of the number. Store the sum in the third file “Results.txt”. Make sure to check if the files have been properly opened. For example, if the first number of 1030Prob2a.txt is 10 and the first number of 1030Prob2a.txt is 20, the sum is 30 and hence the first number stored in Results.txt is 30. If the first number of 1030Prob2a.txt is 10 and the first number of 1030Prob2a.txt is 15, the sum is 25 and hence the first number stored in Results.txt is 25. Since one file MAY BE shorter than the other, all numbers from the longer file will not have a peer in the shorter file to add to. In that case, just add zero to the numbers of the longer file. Your program cannot assume to know which file is longer or shorter beforehand. They may also be of equal size. The program must be generic to work for files of all sizes.

HINT: 1. Start reading both files in a loop, one number at a time from each file, and sum them and store in the result file. 2. Use two Boolean variables to track whether the files have ended, and use these Booleans in your loop. Remember streaming operations return a Boolean. So, you can use a statement such as bool my_bool=(infile>>my_var); 3. Once the shorter file ends, stop the loop.

4. Keep on reading the longer file in a separate loop, add zero to what you read and store the sum in the result file

HW6Prob2a.dat:

10.23
-36.527
26.4544
11.25
63.112
-0.25
22.369
21.223
34.501
-17.201
94.227
-10.375
22.369
17.44

HW6Prob2b.dat

17.36
16.362
11.25
71.362
-11.75
28.47
43.26
18.231
11.305
-15.129
16.54

Explanation / Answer

I've used python 2.7.13 to implement this.

import os

def convert(number): #To convert the number from string to float.
    number.replace(' ', '') #To remove the trailing ' '.
    return float(number)
  
file1 = "HW6Prob2a.dat"
file2 = "HW6Prob2b.dat"
file3 = "Results.txt"

if os.path.isfile(file1) and os.path.isfile(file2): #Checking for existence of file.
    #Opening file object.
    input1 = open(file1, "r")
    input2 = open(file2, "r")
    output = open(file3, "w")

    #Calculating for number of lines.
    line1 = 0
    line2 = 0

    for line in input1.readlines(): #readlines() returns a list of all lines in the file.
        line1 += 1
  
    for line in input2.readlines():
        line2 += 1

    input1.close()
    input2.close()
    #To set the cursor to the beginning of the file.
    input1 = open(file1, "r")
    input2 = open(file2, "r")

    for i in range(min(line1, line2)):
        #min() returns the minimum of them.
        #range() gives a list of numbers upto the argument.
        num1 = convert(input1.readline())
        num2 = convert(input2.readline())
        s = num1 + num2
        output.write(str(s)) #Writing to file.
        output.write(" ") #Writing newline.

    #To add zero for files with unequal length.
    if line1 < line2:
        while i <= line2:
            output.write(input2.readline())
            i += 1
    if line1 > line2:
        while i <= line1:
            output.write(input1.readline())
            i += 1
  
    print " Thank you... "
    #Closing file objects.
    input1.close()
    input2.close()
    output.close()
  
else:
    print " File not found. Exiting... "
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote