function table = subset_sum_table ( t, n, w )

%*****************************************************************************80
%
%% subset_sum_table() sets a subset sum table.
%
%  Discussion:
%
%    The subset sum problem seeks to construct the value T by summing a 
%    subset of the values W.
%
%    This function seeks a solution by constructing a table TABLE of length T,
%    so that TABLE(I) = J means that the sum I can be constructed, and that
%    the last member of the sum is an entry of W equal to J.
%
%  Example:
%
%    w = [ 1, 2, 4, 8, 16, 32 ];
%    t = 22;
%
%    table = subset_sum ( w, t, r )
%    table = [ 1, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 
%      16, 16, 16, 16, 16, 16, 16 ]
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    09 November 2015
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer T, the target value.
%
%    integer N, the number of weights.
%
%    integer W(N), the weights.
%
%  Output:
%
%    integer TABLE(T), the subset sum table.  TABLE(I) is 0 if the
%    target value I cannot be formed.  It is J if the value I can be formed,
%    with the last term in the sum being the value J.
%
  table = zeros ( t, 1 );

  for i = 1 : n
    for j = t - w(i) : -1 : 0

      if ( j == 0 )
        if ( table(w(i)) == 0 )
          table(w(i)) = w(i);
        end
      elseif ( table(j) ~= 0 & table(j+w(i)) == 0 )
        table(j+w(i)) = w(i);
      end

    end

  end
  
  return
end
