Illustration of a recursive function, MATLAB in Mathematics

Assignment Help:

Illustration of a recursive function:

illustration is of a recursive function which does not return anything, but simply prints. The below function prtwords receives a sentence, and prints words in the sentence in the reverse order. The algorithm for prtwords function is as shown below:

  •  Receive a sentence as input argument.
  •  Use strtok to split the sentence into the first word and the rest of the sentence.
  •  If the rest of the sentence is not blank (in another words, if there is more to it), recursively call the prtwords function and pass to it the rest of the sentence.
  •  Print the word.

The function definition is as shown:

1179_Illustration of a recursive function.png

An example of calling the function passes the sentence "what does this do":

>> prtwords('what does this do')

do

this

does

what

What occurs when the function is called here is outlined as shown.

1054_Illustration of a recursive function1.png

In this illustration, the base case is when the rest of the string is empty; in another words, the end of the original sentence has been reached. Each time the function is called, then the execution of the function is interrupted by a recursive call to the function, till the base case is reached. Whenever the base case is reached, the whole function can be executed, involving the printing of the word (in the base case, the word 'do'). The execution of the function is once completed, the program returns to the earlier version of the function in which the word was 'this', and finishes the execution by printing the word 'this'. This continues; the versions of the function are completed in the reverse order, therefore the program ends up printing the words from the sentence in the reverse order.


Related Discussions:- Illustration of a recursive function

Counting in a while loop, Counting in a While Loop: Whenever it is not...

Counting in a While Loop: Whenever it is not known ahead of the time how many values will be entered into the script, it is often essential to count the number of values which

Strvcat function - concatenation, strvcat function - concatenation: Th...

strvcat function - concatenation: The function strvcat will concatenate it vertically, that means that it will generate a column vector of the strings.   >> strvcat(fir

Writing to files, Writing to Files: There are many lower level functio...

Writing to Files: There are many lower level functions which can write to files. We will focus on the fprintf function that can be used to write a file and also to append to a

Illustration of advanced file input and output, Illustration of Advanced fi...

Illustration of Advanced file input and output: For illustration, to refer to the third number in the first element of the cell array: >> subjdata{1}(3) ans =

Naive Gauss Reduction, Write a MATLAB function [d1, u1, l1, c1, r1] = Naive...

Write a MATLAB function [d1, u1, l1, c1, r1] = NaiveGaussArrow(d, u, l, c, r) that takes as input the 5 vectors de ned above representing A. This function performs Naive Gauss redu

Illustration of a recursive function, Illustration of a recursive function:...

Illustration of a recursive function: illustration is of a recursive function which does not return anything, but simply prints. The below function prtwords receives a sentenc

Example of logical built-in functions, Example of Logical built-in function...

Example of Logical built-in functions: For equivalent to all the function, we should make sure that the entire elements in the vector are logically true. The one way of doing

Labels and prompts, Labels and Prompts: The script loads all the numbe...

Labels and Prompts: The script loads all the numbers from file into a row vector. It then splits the vector; it stores the initial element that is the experiment number in a v

Built-in functions for complex numbers, Built-in functions for Complex numb...

Built-in functions for Complex numbers:   We know that in MATLAB both i and j are built-in functions which return √-1 (therefore, they can be thought of as built-in constants).

Refer the subset of a matrix, Refer the subset of a matrix: It is also...

Refer the subset of a matrix: It is also possible to refer to the subset of a matrix. For illustration, this refers to the first & second rows, second & third columns: >> m

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd