78.7k views
0 votes
Use matlab

1- Writing a recursive math function
Complete the recursive function RaiseToPower().

Ex: If userBase is 2 and userExponent is 4, then raisedValue is assigned with 16 (i.e. 2⁴).

Note: This example is for practicing recursion; a non-recursive function, or using the built-in function pow(), would be more common.

complete the solution:

function resultVal = RaiseToPower(baseVal, exponentVal)
% baseVal: Base value
% exponentVal: Power userBase is raised to

resultVal = 0;

if(exponentVal == 0)
resultVal = 1;
else
% Complete the assignment statement below
resultVal = 1;
end
end

// Check if RaiseToPower(2, 4) returns 16

2- Prime checker

Complete the recursive function to determine if a number is prime. Skeletal code is provided in the PrimeChecker function.

complete the solution:

% userNum: Value checked for prime
function isPrime = CheckValue(userNum)
% Do not modify, calls the recursive PrimeChecker function
isPrime = PrimeChecker(userNum, userNum-1);
end

function primeResult = PrimeChecker (testVal, divVal)
% Complete the recursive function PrimeChecker
% Function returns 0 if testVal is not prime and 1 if testVal is prime

% Test case 1: If testVal is 0 or 1, assign primeResult with 0 (not prime)
primeResult = 0;

% Test case 2: If testVal is only divisible by 1 and itself,
% assign primeResult with 1 (is prime)
% Hint: use divVal


% Test case 3: If testVal can be evenly divided by divVal,
% assign primeResult with 0 (not prime)
% Hint: use the % operator


% Otherwise, assign primeResult with the result of the recursive call
% to PrimeChecker with testVal and (divVal - 1)

end

//Check if CheckValue(1) returns 0

//Check if CheckValue(5) returns 1

User Mg Thar
by
7.1k points

1 Answer

3 votes

Final answer:

To complete the recursive function RaiseToPower(), you need to assign the correct value to the variable resultVal.

Step-by-step explanation:

To complete the recursive function RaiseToPower(), you need to assign the correct value to the variable resultVal. Here are the steps:

Check if the exponent value is 0 using an if statement. If it is, assign resultVal with 1.

If the exponent value is not 0, assign resultVal with 1.

Complete the else statement by recursively calling the RaiseToPower() function with the baseVal and exponentVal - 1 as the arguments. Multiply the result of the recursive call by the baseVal and assign it to resultVal.

Finally, return the value of resultVal at the end of the function.

For example, if the input values for baseVal and exponentVal are 2 and 4 respectively, the function will return 16, as 2 raised to the power of 4 is 16.

The solution for the Prime Checker function is similar. The function should return 1 if the input value is prime, and 0 otherwise. Here are the steps to complete the function:

Check if the input value is 0 or 1 using an if statement. If it is, assign primeResult with 0.

If the input value is not 0 or 1, check if it is only divisible by 1 and itself using an if statement with a modulo operation. If it is, assign primeResult with 1.

If the input value is not divisible by only 1 and itself, assign primeResult with the result of the recursive call to PrimeChecker() with the input value and divVal - 1 as the arguments.

Finally, return the value of primeResult at the end of the function.

User Thetaco
by
7.1k points