function result = sumUpToK(v,k)
%sumUpToK  sum all elements of v from 1 thru k
% if k is <= 0, or > length return 0
%
% Examples:
%
%   sumUpToK([1 2 3],2)
%     ans = 3
%
%   sumUpToK([1 1 1 2 3 4],4)
%     ans = 5
%
% P. Conrad 10/13/06 for CISC106 Matthew Rodriguez mrod

if( (k<= 0) || (k> length(v)) )
  result = 0;
  return;
end

%precondition: sum = 0

sum = 0;
for(ii = 1: k)
  sum = sum + v(ii);
end;

result = sum;
end


