File 1: main.m repeat_process = 1 ; while repeat_process data1 = []; data2 = [];
ID: 3588200 • Letter: F
Question
File 1:
main.m
repeat_process = 1;
while repeat_process
data1 = [];
data2 = [];
random_choice = randi( [0 1], 1, 1);
if random_choice
data1 = randi ( [0 100], 1, 1 );
data2 = randi ( [0 100], 1, 1 );
else
data1 = randi ( [0 100], 10, 10 );
data2 = randi ( [0 100], 10, 10 );
end
user_operation_choice = input('Please select one of the following options: 1) Addition 2) Subtraction ');
do_math(random_choice, user_operation_choice, data1, data2);
repeat_process = input('Enter 1 to repeat. ');
end
File 2:
do_math.m
function do_math (math_type, user_operation_choice, data1, data2)
result = 0;
if math_type
switch user_operation_choice
case 1
result = add_numbers(data1, data2);
case 2
result = subtract_numbers(data1, data2);
end %end switch statement
else
do_math_arrays(user_operation_choice, data1, data2);
return;
end %end if-else statement
disp(result);
end %end function do_math
function n_sum = add_numbers(data1, data2);
n_sum = data1 + data2;
end %end function add_numbers
function n_sub = subtract_numbers(data1, data2);
n_sub = data1 - data2;
end %end function add_numbers
File 3:
do_math_arrays.m
function do_math_arrays (user_operation_choice, data1, data2)
result = 0;
switch user_operation_choice
case 1
result = add_numbers(data1, data2)
case 2
result = subtract_numbers(data1, data2)
end %end switch statement
end %end if-else statement
disp(result);
end %end function do_math_arrays
function array_sum = add_numbers(data1, data2)
array_sum = data1 + data2;
end %end function add_numbers
function array_sub = subtract_numbers(data1, data2)
array_sub = data1 - data2;
end %end function add_numbers
1)What value would the variable random_choice have to be for the add_numbers function from which file 2 to be called?
a.0
b.1
c.2
d.3
2)How many output arguments does the function do_math_arrays return?
a.0
b.1
c.2
d.3
Explanation / Answer
Hi,
1.To call do_math, the inputs should be integers rather than arrays, we are setting inputs here,
if random_choice
data1 = randi ( [0 100], 1, 1 );
data2 = randi ( [0 100], 1, 1 );
else
data1 = randi ( [0 100], 10, 10 );
data2 = randi ( [0 100], 10, 10 );
hence if random_choice variable is 1, then only one random value between 1 and 100 is selected for data1 and data2
2.the function do_math_arrays doesnt return any output arguments, if it were to be returned it should be declared in form
function [y1,...,yN] = myfun(x1,...,xM)
this is the syntax to return y1,y2...yn o/p arguments
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.