This is for MatLab! For this problem you will be asked to modify the function be
ID: 3856246 • Letter: T
Question
This is for MatLab!
For this problem you will be asked to modify the function below as follows:
Add an additional input argument (call it user_sel_in ) that will allow a user to enter an operation type (addition, subtraction for example). This will always be a numeric argument. If the user enters a 1, they mean to add, if the user enters a 2, they mean to subtract.
Add an additional output argument (call it user_sel_out) that will allow you to return a string to the user, representing what they chose to do. For example, if the user enters a 1, you should return the string 'addition'. If the user enters a 2, you should always return the string 'subtraction'.
In the function body, you should do the following: Based on what the user entered for the parameter that you created, you will either add up the first two parameters, or you will subtract the second parameter from the first. Example: first_arg - second_arg.
You should return the answer in the output argument result, and you should indicate what the user chose in the output argument that you created, called user_sel_out.
Explanation / Answer
%matlab code
function [user_sel_out, result] = math(user_sel_in, first_arg, second_arg)
if user_sel_in == 1
user_sel_out = 'addition';
result = first_arg + second_arg;
elseif user_sel_in == 2
user_sel_out = 'subtract';
result = first_arg - second_arg;
elseif user_sel_in == 3
user_sel_out = 'multiply';
result = first_arg * second_arg;
else
user_sel_out = 'divide';
result = first_arg / second_arg;
end
end
user_sel_in = input('Input Operator: ');
first_arg = 5;
second_arg = 4;
[user_sel_out, result] = math(user_sel_in, first_arg, second_arg)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.