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

Mat Lab Question: Create a function to perform calculations: +, -, *, or / % 1.1

ID: 3706373 • Letter: M

Question

Mat Lab Question:

Create a function to perform calculations: +, -, *, or /
% 1.1 The function name should be calc and saved in its file.
% 1.2 The function has three parameters a, b, and op. The op is an
% operator.  
% 1.3 The function returns variable r for the calculation result.
% 1.4 The function should provide the following help text:
% "The calc is a simple calculator. It has three parameters.
% The first two parameters are numerical operands, and the third one
% is an operator +, -, *, or / "
% 1.5 Use the if conditional statement in the funciton to check the operator op
% and compute accordingly.
% Note: Example about the syntax to check a single character: if(op == '+')
% Note: You can test the calc function in the Command Window using the syntax:
% >> calc(5, 3, '+')
% You can display the help text:
% >> help calc
%
% You can check if a division is divided by 0, but this is not required. Hint: use Inf or NaN
%

Explanation / Answer

function r = calc(n1, n2, op)
% CALC The calc is a simple calculator. It has three parameters.
% The first two parameters are numerical operands, and the third one
% is an operator +, -, *, or /
%
  
if op == '+'
r = n1+n2;
elseif op == '-'
r = n1-n2;
elseif op == '*'
r = n1*n2;
elseif op == '/'
r = n1/n2;
end
end