function x = knapsack_greedy ( p, w, c )

%*****************************************************************************80
%
%% knapsack_greedy() applies a greedy algorithm to a knapsack problem.
%
%  Discussion:
%
%    It is assumed that the data has been sorted in decreasing order of P/W.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    24 October 2022
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Donald Kreher, Douglas Simpson,
%    Combinatorial Algorithms,
%    CRC Press, 1998,
%    ISBN: 0-8493-3988-X,
%    LC: QA164.K73.
%
%  Input:
%
%    integer P(N), the "profit" or value of each object.
%
%    integer W(N), the "weight" or cost of each object.
%
%    integer C: the maximum weight capacity for the knapsack.
%
%  Output:
%
%    integer X(N): X(I) is 1 if object I is to be taken, 0 otherwise..
%
  n = length ( w );
  x = zeros ( 1, n );
  mass = 0;
  for i = 1 : n
    if ( mass + w(i) <= c )
      x(i) = 1;
      mass = mass + w(i);
    end
  end

  return
end
