Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

File 1: main.m while input('Enter a positive number to execute or repeat this program. ') list1 = randi( [0 100], 1, 20 ); list2 =

File 1:

main.m

while input('Enter a positive number to execute or repeat this program. ')

list1 = randi( [0 100], 1, 20 );

list2 = randi( [0 100], 1, 20 );

question = ('Please select an option from the list: 1) Merge only 2) Sum only 3) Merge and Sum ');

option1 = 0;

while (option1 3)

option1 = input(question);

end %end while loop

switch option1

case 1

merge_sorted_lists(list1, list2, option1);

case 2

disp(sum_lists(list1, list2));

case 3

merge_sorted_lists(list1, list2, option1);

otherwise

disp('Please enter a valid option');

end %end switch

end %end while loop

File 2:

merge_sorted_lists.m

function merge_sorted_lists (alist, another_list, option)

if option == 1

display_final( merge_lists( sort(alist), sort(another_list) ) );

else

temp_list = merge_lists( sort(alist), sort(another_list) );

display_final(temp_list);

display_final( sum_lists(temp_list, 0 ) );

end %end if-else

end %end function

function the_list = merge_lists(list1, list2)

the_list = 0;

list1_position = 1;

list2_position = 1;

for ii = 1:( length(list1) * 2 )

if list1_position == (length(list1)+1)

the_list(ii) = list2(list2_position);

list2_position = list2_position+1;

elseif list2_position == (length(list2)+1)

the_list(ii) = list1(list1_position);

list1_position = list1_position+1;

elseif list1(list1_position)

the_list(ii) = list1(list1_position);

list1_position = list1_position+1;

else

the_list(ii) = list2(list2_position);

list2_position = list2_position+1;

end %end if-elseif

end %end for loop

end %end function

function display_final(alist)

if length(alist) > 1

for ii = 1:length(alist)

disp( alist(ii) );

end %end for loop

else

disp(alist);

end %end if-else

end %end function

File 3:

sum_lists.m

function result = sum_lists(list1, list2)

if length(list2) > 1

result = sum_list(list1);

result = result + sum_list(list2);

else

result = sum_list(list1);

end %end if-else

end %end function

function result = sum_list(the_list)

result = 0;

for ii = 1:length(the_list)

result = result + the_list(ii);

end %end for loop

end %end functionimage text in transcribed

2) Which of the functions (the m files, main functions only) return an output argument when D) 80 called? A) File2 and file3 onhy B) File1, file2, and file3 C) File2 only D) Files only 7) In the "mergesoredit function (file2), choose the answer that depicts the correct order that functions (any functions) are executed in if the user chooses to only merge lists (Option 1 from file 1). A) metge-iste gislestinal sort B) sort, sort, mergeJists, display tinal, umlsts, display final C) sort, sort, mergeJists, display final 3) Which functions return an output (helper functions included)? c) le2) mecseusts (ile2) glselax.final (files) sun ists (ile3) um list 8) In file 1, is it possible that the "otherwise case will ever get executed given the way this program is written? 4) How are the lists being merged inside the for loop of the "mereJist function (file2)? A) No B) Only if the user enters any numberthat is not 1, 2, or 3 for optionl C) Only if the user enters an invalid option D) Yes A) The element at "list1_position* in "list1 and "list2_position" in "ist2" are compared. The smaller item is moved into a new array, and the counter for that list is incremented. This repeats until there are no more elements in either list. B) One element from "ist1" is added to a new array, then one element from "list2" is added to the same array. This continues until there are no more elements in either list. 9) What do variables "list1 and "list2" from file 1 contain? C) The element at "list1_position* in "list1 and "list2_position" in "list2 are compared. The larger item is moved into a new array, and the counter for that list is incremented. This repeats until there are no more elements in either list. All of the elements from listl are added to a new array, then all of the elements from st2 are concatenated to that same array. A) Ax10D array of random integers B) A1x100 array of random integers between 1 and 20 C) A Ix20 array of random integers D) A 1x20 array of random integers between 0 and 100 D) 5) How many times will the first while loop in file 1 repeat? A) Never B) It will keep repeating as long as the user enters a positive number C) It wil repeat until the user enters the number 0 D) Once 6) As the code is written, how many times will the for loop in the "gplax fioal" function in file 2 iterate if it is executed? A) 10 B) 40 C) 20

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago