function result = myVectorSum(v) %myVectorSum compute sum of a vector % % consume: a vector of numbers % produce: a scalar number, the sum of the vector % % Examples: % % >> myVectorSum([3 5 2]) % ans = % 10 % >> myVectorSum([4]) % ans = % 4 % >> myVectorSum([]) % ans = % 0 % P.Conrad for CISC106 10/20/06 (problem 1: write the body) % initialize a sum variable (clear it out) sum = 0; % use a for loop to step through the vector, adding up the elements for k= 1:length(v) sum = sum + v(k); end % for % return the result result = sum; return; end