Function Name: recurSum Inputs (1): - (double) A 1xN array ofscalars Outputs (1)
ID: 3617337 • Letter: F
Question
Function Name: recurSum Inputs (1): - (double) A 1xN array ofscalars Outputs (1): - (double) The sum of the elements of theinput arrayFunction Description: Given a 1xN array of scalars (a vector), write a functionrecurSum that recursively sums the elements of the input array.Your function should then return this sum. An empty vector ([])has a sum of 0.
Constraints: You MUST use recursion to solve this problem.
Hints: This function should behave like the built in MATLAB functionsum().
Test Cases:
a = recurSum([12 -3 0 17 25 62 34]); a => 147
b = recurSum([]); b => 0; Function Name: recurSum Inputs (1): - (double) A 1xN array ofscalars Outputs (1): - (double) The sum of the elements of theinput array
Function Description: Given a 1xN array of scalars (a vector), write a functionrecurSum that recursively sums the elements of the input array.Your function should then return this sum. An empty vector ([])has a sum of 0.
Constraints: You MUST use recursion to solve this problem.
Hints: This function should behave like the built in MATLAB functionsum().
Test Cases:
a = recurSum([12 -3 0 17 25 62 34]); a => 147
b = recurSum([]); b => 0;
Explanation / Answer
function A= recurSum(B) [m n]=size(B); %you can add error checking to make sure m=1 if youwant if(n>0) A=B(1,1)+recurSum(B(1,2:end)); else A=0; end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.