Question: "Write a function with header [S] = mySum(A) where A is a one-dimensional array, and S is the sum of all the elements of A. You can use recursion or iteration to solve the problem, but do not use MATLAB’s function sum." Answer (there is error)
function [S] = mySum(A)% this program is the sum of all the elements of A
%A is a one dimention array
%get A
[n]=size(A); if n==1 % base case.
S= A(1);elseif n==2 %base case
S=A(1) + A(2);else %recusrive step
S = mySum(A(n-1)) + mySum(A(n-2));endend
Where is the error?
Best Answer