132k views
4 votes
13.8

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^4).

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 DungGramer
by
8.0k points

1 Answer

5 votes

Final answer:

To complete the recursive function RaiseToPower() in MATLAB, modify the assignment statement to multiply the base value with the result of the recursive call. For the PrimeChecker function, assign different values to primeResult based on test cases.

Step-by-step explanation:

To complete the recursive function RaiseToPower() in MATLAB, you need to modify the assignment statement below:

resultVal = 1;

Inside the else statement, you can call the RaiseToPower function recursively by multiplying the base value with the result of the recursive call, like this:

resultVal = baseVal * RaiseToPower(baseVal, exponentVal-1);

This implementation will calculate the power of a given base value and exponent using recursion. For example, if the userBase is 2 and the userExponent is 4, the output raisedValue will be assigned as 16 (i.e. 2^4).

The skeleton code provided in the PrimeChecker function can be completed to determine if a number is prime. Make the following modifications:

  1. For test case 1, assign primeResult with 0 if testVal is 0 or 1.
  2. For test case 2, assign primeResult with 1 if testVal is only divisible by 1 and itself.
  3. For test case 3, use the % operator to check if testVal can be evenly divided by divVal. If it can, assign primeResult with 0.
  4. In the else case, assign primeResult with the result of the recursive call to PrimeChecker with testVal and (divVal - 1).

Calling CheckValue(1) will return 0, indicating that 1 is not prime. Calling CheckValue(5) will return 1, indicating that 5 is prime.

User Ayvazj
by
8.1k points