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

MatLab Coding Use fopen and fscanf to read cc071.txt and find the average of the

ID: 3914791 • Letter: M

Question

MatLab Coding
Use fopen and fscanf to read cc071.txt and find the average of the numbers contained within. The first line of cc071.txt contains an integer, which is the number of values in the file, while the rest of the files contains the integers to be averaged. Store your result in a variable named fileAvg.
%open file (edit following line)
[] = fopen();

%check for good open (edit following line)
if
    %good open, find average

else
   %bad open, display error
      disp(msg)
end

%close file
fclose(fid);

Explanation / Answer

Given below is the completed code. Please do rate the answer if it helped. Thank you


%open file (edit following line)
[fid, msg] = fopen('cc071.txt');

%check for good open (edit following line)
if fid ~= -1
%good open, find average
n = fscanf(fid, "%d");
nums = fscanf(fid, "%d");
fileAvg = sum(sum(nums))/n;
else
%bad open, display error
disp(msg)
end

%close file
fclose(fid);