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

PLEASE READ IN EACH LINE AND USE EVAL each line: a ata file called \"mathfile.da

ID: 3923961 • Letter: P

Question

PLEASE READ IN EACH LINE AND USE EVAL

each line: a ata file called "mathfile.dat" stores three characters on each operand la single digit number), an operator la one character op as+, , and then another operand (a single digit number). For example, it might look like this: 16. A data file called "mathfile.dat stores three characters on haracter operator, such >>type mathfile.dat 5+2 8-1 3+3 You are to write a script that will use fgetl to read from the file, one line at a time, perform the specified operation, and print the result.

Explanation / Answer

mathfile.dat

5+2
4-3
8-1
3+3

mathfile.m

% This script reads a text file and evaluates the expression on each line

fileid = fopen('mathfile.dat');
if fileid == -1
    disp('File open not successful')
else
    while ~feof(fileid)
        str = fgetl(fileid);
        result = eval(str);
        x = str2num(str(1));
        operator = str(2);
        y = str2num(str(3));
        fprintf('%d %c %d = %d ',x,operator,y,result)
    end
  
    closeresult = fclose(fileid);
    if closeresult == 0
        disp('File close successful')
    else
        disp('File close not successful')
    end
end

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