In MATLAB not C++, write a user defined recursive function named MySum(n) to pri
ID: 3773359 • Letter: I
Question
In MATLAB not C++, write a user defined recursive function named MySum(n) to print out the summation of the next consecutive five odd numbers of a given number as a function argument. Make sure you handle the case if the given argument is a negative value.
%Recursive Function named MySum(n)
%to print out the summation of the next consecutive
%five odd numbers of a given number as a function argument
function s=MySum(n)
r=mod(n,2);
s=n;
if (r==0) add=1;
else
add=2;
end
for i=1:5
s=MySum(n)+add*i;
end
end
Tried this function above but there is an infinite recursion within the program.
Explanation / Answer
function s=MySum(n,m)
if(m>0)
s = n + MySum(n+2,m-1);
else
s = 0;
end
end
function s=MySum(n)
r = mod(n,2);
if(r==0) n=n+2;
else
n=n+1;
end
s = MySum1(n,5);
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.