use octave or MATLAB as programs to code. do not usedisp or num2str. also, do not use break or continuestatements.
3. In this problem we will investigate using recursion to determine whether or not a user- entered vector is sorted (either nondecreasing or nonincreasing). Given a length-1 vector, it clearly is sorted and we will say it’s nondecreasing. Given a length-2 vector, we can easily determine if it’s sorted by comparing the two elements. Given a vector of length > 2, we can break the problem down by noting that if we knew the status and smallest and largest elements of the subvector of elements 2 through the end, we could use them and the value of the first element to determine the status of the given vector. Write a program that calls the function get_vector which should • call the function get_positive integer (passing in an appropriate string) once to obtain a value for the length of the vector • use a loop to prompt the user to enter the vector one element at a time • return the entered vector and its length. The function get positive integer should • take in a string input variable to use in the prompt keep prompting the user and giving error messages (using %d) until a positive integer is entered return the resulting value. The main program should then call the function check_if_sorted which should • take in a vector of numbers and its length print an “entering.” status message giving the input variable values call itself recursively to determine if the vector is nondecreasing, nonincreasing, or not sorted (if it is both nondecreasing and nonincreasing, call it nondecreasing) print a “leaving” status message giving the output variable values return return a status integer that is equal to 1 if the vector is nondecreasing, -1 if it is nonincreasing, and ( if it is not sorted the smallest integer present in the input vector if it is sorted or otherwise set equal to -99999 D the largest integer present in the input vector if it is sorted or otherwise set equal to 99999 The main program should then print a message based upon the status. Sample output: November 25, 2019 4 of 8 MAT 2010L: Lab Assignment 14 Enter a positive integer for the length: -4 Invalid entry -4 Enter a positive integer for the length: 2.4 Invalid entry 2.4 Enter a positive integer for the length: 6 Enter number 1: -5 Enter number 2: 33 Enter number 3: Enter number 4: Enter number 5: BINTI Tch 20 Enter a positive integer for the length: 4 Invalid entry – Enter a positive integer for the length: 2.4 Invalid entry 2.4 Pater positive integer for the length> 6 Enter number 11 -5 Enter number 2 – 3 Enter number 3: -3 Enter number 4: 2 Enter number 5: 10 Enter number 6: 13 check it sorted: Entering with length 6 vector: -5.00 -3.00 -3.00 2.00 10.00 13.00 check.it sorted Entering with length 5 vector: -3.00 -3.00 2.00 10.00 13.00 check if sorted: Entering with length 4 vector: -3.00 2.00 10.00 13.00 check if sorted: Eatering with length 3 vector: 2.00 10.00 13.00 check 11 sorted: Entering with length 2 vector: 10.00 13.00 check if sorted: Leaving for length 2 vector with status 1. sallent 10.00, largest 13.00 check.it sorted: Leaving for length 3 vector with status 1, enllest 2.00, largest 13.00 checkif_sortedLeaving for length 4 vector with status 1. annllest -3.00, largest 13.00 check if sorted: Leaving for length 5 vector with status 1. annllest -3.00, largest 13.00 check.it sorted: Leaving for length 6 vector with status 1, mallest -5.00, largest 13.00 The vector is nondecreasing -5.50 Enter a positive integer for the length: 7 Enter number 1: 21.5 Enter number 2 : 15 Enter number 3: 9.245 Enter number 4: 6 Enter number 5: 6 Enter number 6: -3 Enter number 7: -5.5 check.it sorted Entering with length 7 vector; 21.50 16.00 9.24 check 11.Borted: Entering with length 6 vector: 15.00 9.24 6.00 check.11.sorted Entering with length 5 Vector 9.24 6.00 6.00 check if sorted: Entering with length 4 vector: 6.00 6.00 -3.00 check 11 aorted: Entering with length 3 vector: 6.00 -3.00 -5.50 check if sorted: Entering vith length 2 vector: -3.00 -5.50 check_it_sorted: Leaving for length 2 vector with status – mallest check 11.sorted: Leaving for length 3 vector with status , allest check if sorted Leaving for length 4 vector with status 1. tallest checi 11 sorted: Leaving for length 5 vector with status -1, smallest check at sorted: Leaving for length vector with status -1, umallest check.it sortedLeaving for length 7 vector with status -1, mallest The vector is increasing -3.00 -5.50 6.00 6.00 -3.00 -5.50 6.00 -3.00 -5.50 -5.50, largest -5.50, largest -5.50, largest -5.50, largest -5.50, largest -5.50, largest -3.00 6.00 6.00 9.24 15.00 21.50 -2.00 4.00 Enter a positive integer for the length: 7 Enter number 1: -2 Enter number 2: 4 Enter number 3: -5.6 Enter number 4: 15 Enter number 5: 13 Enter number 6: 12 Enter number 7: 10 check it sorted: Entering with length 7 vector: -5.60 15.00 13.00 12.00 10.00 checkif_sorted: Entering with length .60 15.00 13.00 12.00 10.00 check if sorted: Entering with length 5 vector: -5.60 15.00 13.00 12.00 10.00 check it sorted: Entering with length 4 vector 15.00 13.00 12.00 10.00 check 11.sorted: Entering with length 3 vector: 13.00 12.00 10.00 check.itsorted: Entering with length 2 vector: 12.00 ck_if_Borted: Leaving for length 2 vector with status -1, smallest 10.00, largest 12.00 ing for length 3 vector with status -1, smallest 10.00 Largest 13.00 check 11 Borted: Leaving for length 4 vector with status-1 4 vector with status smallest 10.00, largest 15.00 check.if.sorted: Leaving for length 5 vector with sta smallest -99999.00, largest 99999.00 check if sorted: Leaving for length 6 vector with st smallest -99999.00, largest 99999.00 check21.sorted: Leaving for length 7 vector with status o, smallest -99999.00 largest 99999.00 The vector is not sorted 10.00 Show transcribed image text 3. In this problem we will investigate using recursion to determine whether or not a user- entered vector is sorted (either nondecreasing or nonincreasing). Given a length-1 vector, it clearly is sorted and we will say it’s nondecreasing. Given a length-2 vector, we can easily determine if it’s sorted by comparing the two elements. Given a vector of length > 2, we can break the problem down by noting that if we knew the status and smallest and largest elements of the subvector of elements 2 through the end, we could use them and the value of the first element to determine the status of the given vector. Write a program that calls the function get_vector which should • call the function get_positive integer (passing in an appropriate string) once to obtain a value for the length of the vector • use a loop to prompt the user to enter the vector one element at a time • return the entered vector and its length. The function get positive integer should • take in a string input variable to use in the prompt keep prompting the user and giving error messages (using %d) until a positive integer is entered return the resulting value. The main program should then call the function check_if_sorted which should • take in a vector of numbers and its length print an “entering.” status message giving the input variable values call itself recursively to determine if the vector is nondecreasing, nonincreasing, or not sorted (if it is both nondecreasing and nonincreasing, call it nondecreasing) print a “leaving” status message giving the output variable values return
return a status integer that is equal to 1 if the vector is nondecreasing, -1 if it is nonincreasing, and ( if it is not sorted the smallest integer present in the input vector if it is sorted or otherwise set equal to -99999 D the largest integer present in the input vector if it is sorted or otherwise set equal to 99999 The main program should then print a message based upon the status. Sample output: November 25, 2019 4 of 8 MAT 2010L: Lab Assignment 14 Enter a positive integer for the length: -4 Invalid entry -4 Enter a positive integer for the length: 2.4 Invalid entry 2.4 Enter a positive integer for the length: 6 Enter number 1: -5 Enter number 2: 33 Enter number 3: Enter number 4: Enter number 5: BINTI Tch 20
Enter a positive integer for the length: 4 Invalid entry – Enter a positive integer for the length: 2.4 Invalid entry 2.4 Pater positive integer for the length> 6 Enter number 11 -5 Enter number 2 – 3 Enter number 3: -3 Enter number 4: 2 Enter number 5: 10 Enter number 6: 13 check it sorted: Entering with length 6 vector: -5.00 -3.00 -3.00 2.00 10.00 13.00 check.it sorted Entering with length 5 vector: -3.00 -3.00 2.00 10.00 13.00 check if sorted: Entering with length 4 vector: -3.00 2.00 10.00 13.00 check if sorted: Eatering with length 3 vector: 2.00 10.00 13.00 check 11 sorted: Entering with length 2 vector: 10.00 13.00 check if sorted: Leaving for length 2 vector with status 1. sallent 10.00, largest 13.00 check.it sorted: Leaving for length 3 vector with status 1, enllest 2.00, largest 13.00 checkif_sortedLeaving for length 4 vector with status 1. annllest -3.00, largest 13.00 check if sorted: Leaving for length 5 vector with status 1. annllest -3.00, largest 13.00 check.it sorted: Leaving for length 6 vector with status 1, mallest -5.00, largest 13.00 The vector is nondecreasing -5.50 Enter a positive integer for the length: 7 Enter number 1: 21.5 Enter number 2 : 15 Enter number 3: 9.245 Enter number 4: 6 Enter number 5: 6 Enter number 6: -3 Enter number 7: -5.5 check.it sorted Entering with length 7 vector; 21.50 16.00 9.24 check 11.Borted: Entering with length 6 vector: 15.00 9.24 6.00 check.11.sorted Entering with length 5 Vector 9.24 6.00 6.00 check if sorted: Entering with length 4 vector: 6.00 6.00 -3.00 check 11 aorted: Entering with length 3 vector: 6.00 -3.00 -5.50 check if sorted: Entering vith length 2 vector: -3.00 -5.50 check_it_sorted: Leaving for length 2 vector with status – mallest check 11.sorted: Leaving for length 3 vector with status , allest check if sorted Leaving for length 4 vector with status 1. tallest checi 11 sorted: Leaving for length 5 vector with status -1, smallest check at sorted: Leaving for length vector with status -1, umallest check.it sortedLeaving for length 7 vector with status -1, mallest The vector is increasing -3.00 -5.50 6.00 6.00 -3.00 -5.50 6.00 -3.00 -5.50 -5.50, largest -5.50, largest -5.50, largest -5.50, largest -5.50, largest -5.50, largest -3.00 6.00 6.00 9.24 15.00 21.50
-2.00 4.00 Enter a positive integer for the length: 7 Enter number 1: -2 Enter number 2: 4 Enter number 3: -5.6 Enter number 4: 15 Enter number 5: 13 Enter number 6: 12 Enter number 7: 10 check it sorted: Entering with length 7 vector: -5.60 15.00 13.00 12.00 10.00 checkif_sorted: Entering with length .60 15.00 13.00 12.00 10.00 check if sorted: Entering with length 5 vector: -5.60 15.00 13.00 12.00 10.00 check it sorted: Entering with length 4 vector 15.00 13.00 12.00 10.00 check 11.sorted: Entering with length 3 vector: 13.00 12.00 10.00 check.itsorted: Entering with length 2 vector: 12.00 ck_if_Borted: Leaving for length 2 vector with status -1, smallest 10.00, largest 12.00 ing for length 3 vector with status -1, smallest 10.00 Largest 13.00 check 11 Borted: Leaving for length 4 vector with status-1 4 vector with status smallest 10.00, largest 15.00 check.if.sorted: Leaving for length 5 vector with sta smallest -99999.00, largest 99999.00 check if sorted: Leaving for length 6 vector with st smallest -99999.00, largest 99999.00 check21.sorted: Leaving for length 7 vector with status o, smallest -99999.00 largest 99999.00 The vector is not sorted 10.00
Expert Answer
Answer to use octave or MATLAB as programs to code. do not use disp or num2str. also, do not use break or continue statements. …