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

Open two files, numbers1.txt and numbers2.txt, naming the file handles, InFile1

ID: 3827405 • Letter: O

Question

Open two files, numbers1.txt and numbers2.txt, naming the file handles, InFile1 and inFile2, respectively. First populate array1 by reading In values from InFile1 (be sure to convert each input string to a number before assigning to the array elements). Next, populate array1 by reading in values from inFile2 (be sure to convert each input string to a number before assigning to the array elements). Finally, traverse through the two arrays - array1 & array2, outputting each elements' value and whether or not they are equal.

Explanation / Answer

inFile1 = fopen('Newfile.m','r'); %# open file for reading
inFile2 = fopen('Newfile(1).m','r'); %# open file
A = [];
B = [];
while ~feof(inFile1) %read file 1
    line = fgets(inFile1); %# read line by line
    A = [A;str2num(line)]; %convert to number and store it in array
  
end
while ~feof(inFile2) %read file till end
    line = fgets(inFile2); %# read line by line
    B = [B;str2num(line)];
  
end

fclose(inFile1);
fclose(inFile2);

for i = 1 : numel(A)
    fprintf("%d %d",A(i), B(i)); %print content
    if A(i) == B(i)
        printf(" equal"); %print if equal
    end
    printf(' ');
end

I kept the code as simple as possible. I hope you like it. If incase you face any trouble, please let me know. I shall be glad to help you with the code.