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 (tising %a) until a positive integer is entered • return the resulting value. The main programi should then call the function check if sorted wbich should • take in a vector of numbers and its length • print an “entering status message giving the input mirable values • call itself recursively to determine if the vector is moderning Bo s ing or not sorted (if it is both nondecreasing and nonincreasing call it hon i ng • print loving an age giving the rol variable value • return • print a “leaving” status message giving the output variable values return Da status integer that is equal to 1 if the vector is nondecreasing, -1 if it is nonincreasing, and 0 if it is not sorted the smallest integer present in the input vector if it is sorted or otherwise set equal to -99999 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: – 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 saber 11-5 Enter number 21 23 Enter number 313 Enter nunber 42 Enter berb: 10 Enter number 61 13 Enter a positive integer for the length: -4 Invalid entry – 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 – 3 Enter number 3: -3 Enter number 4: 2 Enter number 5: 10 Enter number 6: 13 check if Borted: Entering with length 6 vector: -5.00 -3.00 -3.00 2.00 10.00 13.00 check.it.sorted: Entering with langth 5 vector: -3.00 -3.00 2.00 10.00 13.00 check if sorted: Entering with langth 4 vector: -3.00 2.00 10.00 13.00 check if sorted: Entering with length 3 vector: 2.00 10.00 13.00 check if sorted: Entering with length 2 vector: 10.00 13.00 check if sorted: Leaving for length 2 vector with status 1, malleat 10.00, largest 13.00 check.if.aorted: Loving for length 3 vector with status 1, sallent 2.00, largest 13.00 check if norted: Leaving for length 4 vector with status 1, sallent -3.00, largest 13.00 check.ir.sorted: Leaving for length 5 vector with statun 1, sellest -3.00, largest 13.00 check…sorted: Leaving for length 6 vector with status i, enllest -5.00, largest 13.00 The vector is nondecreasing Enter a positive integer for the length: Z 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_11_sorted: Entering with length 7 vector: 21.50 15.00 9.24 6.00 6.00 -3.00 -5.50 check 11 Borted: Entering with length 6 vector: 15.00 9.24 6.00 6.00 -3.00 -5.50 check if sorted: Entering with length 5 vector: 9.24 6.00 6.00 -3.00 -5.50 check.if.sorted: Entering with length 4 vector: 6.00 6.00 -3.00 -5.50 check.if.norted: Entering with length 3 vector: 6.00 -3.00 -5.50 check.if.sortedEntering with length 2 vector: -3.00 -5.50 check it orted: Leaving for length 2 vector with status -1, alleet -5.50, largest 3.00 check.irsortedLeaving for length 3 vector with statua 1, let 5.50, largest 6.00 check.it.corted: Leaving for length 4 vector with status 13 h,so, largest 6.00 check, sorted: Leaving for length 5 vector with atatus -1, wallet-5.50, largest 9.24 check.sr.sorted: Leaving for Length 6 vector with stat t 5.50, largest 15.00 check.ir.sorted: Leaving for length 7 vector with att 5.59, largest 21.50 The vector in nonincreasing We were unable to transcribe this imageShow 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 (tising %a) until a positive integer is entered • return the resulting value. The main programi should then call the function check if sorted wbich should • take in a vector of numbers and its length • print an “entering status message giving the input mirable values • call itself recursively to determine if the vector is moderning Bo s ing or not sorted (if it is both nondecreasing and nonincreasing call it hon i ng • print loving an age giving the rol variable value • return
• print a “leaving” status message giving the output variable values return Da status integer that is equal to 1 if the vector is nondecreasing, -1 if it is nonincreasing, and 0 if it is not sorted the smallest integer present in the input vector if it is sorted or otherwise set equal to -99999 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: – 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 saber 11-5 Enter number 21 23 Enter number 313 Enter nunber 42 Enter berb: 10 Enter number 61 13
Enter a positive integer for the length: -4 Invalid entry – 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 – 3 Enter number 3: -3 Enter number 4: 2 Enter number 5: 10 Enter number 6: 13 check if Borted: Entering with length 6 vector: -5.00 -3.00 -3.00 2.00 10.00 13.00 check.it.sorted: Entering with langth 5 vector: -3.00 -3.00 2.00 10.00 13.00 check if sorted: Entering with langth 4 vector: -3.00 2.00 10.00 13.00 check if sorted: Entering with length 3 vector: 2.00 10.00 13.00 check if sorted: Entering with length 2 vector: 10.00 13.00 check if sorted: Leaving for length 2 vector with status 1, malleat 10.00, largest 13.00 check.if.aorted: Loving for length 3 vector with status 1, sallent 2.00, largest 13.00 check if norted: Leaving for length 4 vector with status 1, sallent -3.00, largest 13.00 check.ir.sorted: Leaving for length 5 vector with statun 1, sellest -3.00, largest 13.00 check…sorted: Leaving for length 6 vector with status i, enllest -5.00, largest 13.00 The vector is nondecreasing Enter a positive integer for the length: Z 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_11_sorted: Entering with length 7 vector: 21.50 15.00 9.24 6.00 6.00 -3.00 -5.50 check 11 Borted: Entering with length 6 vector: 15.00 9.24 6.00 6.00 -3.00 -5.50 check if sorted: Entering with length 5 vector: 9.24 6.00 6.00 -3.00 -5.50 check.if.sorted: Entering with length 4 vector: 6.00 6.00 -3.00 -5.50 check.if.norted: Entering with length 3 vector: 6.00 -3.00 -5.50 check.if.sortedEntering with length 2 vector: -3.00 -5.50 check it orted: Leaving for length 2 vector with status -1, alleet -5.50, largest 3.00 check.irsortedLeaving for length 3 vector with statua 1, let 5.50, largest 6.00 check.it.corted: Leaving for length 4 vector with status 13 h,so, largest 6.00 check, sorted: Leaving for length 5 vector with atatus -1, wallet-5.50, largest 9.24 check.sr.sorted: Leaving for Length 6 vector with stat t 5.50, largest 15.00 check.ir.sorted: Leaving for length 7 vector with att 5.59, largest 21.50 The vector in nonincreasing
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. …