The double factorial of an odd number n is given by: Ex: The double factorial of the…
- User
The double factorial of an odd number n is given by:
Ex: The double factorial of the number 9 is:
In MatLab, write a recursive function called OddDoubleFactorial that accepts a scalar integer input, N, and outputs the double factorial of N. The input to the function will always be an odd integer value. Each time the function assigns a value to the output variable, the value should be saved in 8-digit ASCII format to the data file recursion_check.dat. The -append option should be used so the file is not overwritten with each save. Ex: If the output variable is Result then, the command is: save recursion_check.dat Result -ascii -append The test suite will examine this file to check the stack and ensure the problem was solved using recursion.
Use this code:
function Result = OddDoubleFactorial(n) % Computes the double factorial of n using recursion, assumes n is odd
% Your code goes here % save recursion_check.dat Result -ascii -appendend
- Assistant
Here's an implementation of the recursive function "OddDoubleFactorial" in MATLAB:
function Result = OddDoubleFactorial(n) % Computes the double factorial of n using recursion, assumes n is odd
% Base case: n = 1 if n == 1 Result = 1; save recursion_check.dat Result -ascii -append return end % Recursive case: n > 1 Result = n * OddDoubleFactorial(n-2); save recursion_check.dat Result -ascii -appendend
- User
In MatLab, complete the function ConvertToPennies() so that the function returns the total number of pennies given a number of dollars and (optionally) a number of pennies.
Ex: ConvertToPennies(5 , 6) returns 506 and ConvertToPennies(8) returns 800.
Use this Code:
function totalPennies = ConvertToPennies(numDollars, numPennies) % numDollars: Number of dollars % numPennies: Number of pennies (optional) % Function output: Total number of pennies
% ConvertToPennies() returns the total number of pennies given % the number of dollars and optionally a number of pennies totalPennies = 0;end
- Assistant
Here's the completed code for the function "ConvertToPennies":
function totalPennies = ConvertToPennies(numDollars, numPennies) % numDollars: Number of dollars % numPennies: Number of pennies (optional) % Function output: Total number of pennies
% ConvertToPennies() returns the total number of pennies given % the number of dollars and optionally a number of pennies % Check if numPennies is provided, if not, set it to 0 if nargin < 2 numPennies = 0; end totalPennies = (numDollars * 100) + numPennies;end
- User
The string array classRoster is a row array of string scalars with the student names. The array entry corresponds to the seat number of the student. In MatLab, write a function that returns the name of the student in the seat given the string array classRoster and the seat number.
Ex: classRoster=["Jill Applegate"; "John Monroe"; "Cristian Phillips"]; studentName = StudentLookup(classRoster, 2)
returns
studentName = "John Monroe"
Use this code:
function studentName = StudentLookup(classRoster, seatPosition) studentName = ; end
- Assistant
Here's the completed code for the function "StudentLookup":
function studentName = StudentLookup(classRoster, seatPosition) % classRoster: String array containing student names % seatPosition: Seat number of the student % Function output: Name of the student in the seat
studentName = classRoster(seatPosition);end
- User
Write a statement that creates the character vector fullName by concatenating three character vectors: firstName, a space, then lastName.
Use this code:
function fullName = CombineNames(firstName, lastName) % firstName: User specified string % lastName: User specified string
% Write a statement that assigns fullName with firstName, a space, then lastName fullName = 'firstName' + ' ' + 'lastName';end
- Assistant
Here's the corrected code for the function "CombineNames":
function fullName = CombineNames(firstName, lastName) % firstName: User specified string % lastName: User specified string
% Assign fullName with firstName, a space, then lastName fullName = [firstName, ' ', lastName];end
- User
Write a statement that creates the character vector fullName by concatenating three character vectors: firstName, a space, then lastName.
Use this code:
function fullName = CombineNames(firstName, lastName) % firstName: User specified string % lastName: User specified string
% Write a statement that assigns fullName with firstName, a space, then lastName fullName = ;
end
- Assistant
Here's the corrected code for the function "CombineNames":
function fullName = CombineNames(firstName, lastName) % firstName: User specified string % lastName: User specified string
% Assign fullName with firstName, a space, then lastName fullName = [firstName, ' ', lastName];
end
- User
Write a function called ReverseLetters that takes an input phrase consisting of a single word and reverses the sequence of letters between the first letter and last letter. The input phrase could be a character vector of any length. Restrictions: The function may not use loops.
Use this code:
function sout= ReverseLetters(sin)
% Your code goes here %end
- Assistant
Here's an implementation of the "ReverseLetters" function in MATLAB that reverses the sequence of letters between the first and last letter of the input word:
function sout = ReverseLetters(sin) % ReverseLetters takes an input phrase consisting of a single word % and reverses the sequence of letters between the first letter and % last letter. The input phrase could be a character vector of any length.
% Convert the input word to a character array chars = char(sin); % Get the first and the last letter of the word firstLetter = chars(1); lastLetter = chars(end); % Reverse the sequence of letters between the first and last letter middleLetters = reverse(chars(2:end-1)); % Combine the reversed letters to form the output word sout = [firstLetter, middleLetters, lastLetter];end
- User
ReverseLetters('a') should return 'a'
- Assistant
Apologies for the omission. Here's the updated implementation that handles the case when the input word consists of only one letter:
function sout = ReverseLetters(sin) % ReverseLetters takes an input phrase consisting of a single word % and reverses the sequence of letters between the first letter and % last letter. The input phrase could be a character vector of any length.
% Convert the input word to a character array chars = char(sin); % Check if the input word consists of only one letter if length(chars) == 1 % If it does, then the output is the same as the input sout = sin; else % Get the first and the last letter of the word firstLetter = chars(1); lastLetter = chars(end); % Reverse the sequence of letters between the first and last letter middleLetters = reverse(chars(2:end-1)); % Combine the reversed letters to form the output word sout = [firstLetter, middleLetters, lastLetter]; endend
- User
Write a function called PrintGreeting to insert the text "Greetings", followed by a single space, and then a name, given by the string scalar username.
Use this code:
function welcomeMessage = PrintGreeting(userName)
% Insert your code hereend
- Assistant
Here's the implementation of the "PrintGreeting" function in MATLAB:
function welcomeMessage = PrintGreeting(userName) % PrintGreeting inserts the text "Greetings", followed by a single space, % and then a name, given by the string scalar userName.
% Construct the welcome message by concatenating the strings welcomeMessage = "Greetings " + userName; % Print the welcome message disp(welcomeMessage);end
- User
Websites are built using the idea of tags to define how a web browser displays the content. An opening tag indicates where the tag starts to take effect and a closing tag indicates where the tag stops having an effect. Write a function called WebMarkup that assigns annotatedText with the opening tag, followed by the elementText, and then the closing tag. All input arguments are character vectors.
Restrictions: No spaces should exist between the tags and elementText. The identifierTag should be surrounded by angle brackets. The closing identifierTag should contain an additional '/'.Use this code:
function annotatedText = WebMarkup(identifierTag, elementText)
% Insert your solution hereend
- Assistant
Here's the implementation of the "WebMarkup" function in MATLAB:
function annotatedText = WebMarkup(identifierTag, elementText) % WebMarkup assigns annotatedText with the opening tag, followed by % the elementText, and then the closing tag. All input arguments are character vectors.
% Construct the annotated text by concatenating the strings annotatedText = ['<', identifierTag, '>', elementText, '</', identifierTag, '>'];end