function [ C, N, S, E, W ] = cnsew ( nr, nc )

%*****************************************************************************80
%
%% cnsew returns the variable indices of the 5 point Laplacian stencil.
%
%  Discussion:
%
%    We assume the grid is numbered something like this example,
%    for which nr = 5 (five rows) and nc = 4 (four columns):
%
%   |  17 18 19 20
%   |  13 14 15 16
%   I   9 10 11 12
%   |   5  6  7  8
%   |   1  2  3  4
%   |
%   +-------J-----
%
%    We also assume that periodic boundary conditions have been imposed, so
%    that the next node to the right of node 20 is node 17, and the next
%    node above node 20 is 4, for instance.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    16 July 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer i, j: the grid indices of a point.
%
%    integer nx: the number of nodes in the x direction.
%
%  Output:
%
%    integer C, N, S, E, W: the variable indices for the central,
%    north, south, east, and west nodes.
%
  n = nr * nc;

  C = zeros ( 1, n );
  N = zeros ( 1, n );
  S = zeros ( 1, n );
  E = zeros ( 1, n );
  W = zeros ( 1, n );

  k = 0;
  for i = 1 : nr
    for j = 1 : nc
      k = k + 1;

      C(k) = j     + ( i - 1 ) * nc;

      if ( i == nr )
        N(k) = j;
      else
        N(k) = j + ( i ) * nc;
      end

      if ( i == 1 )
        S(k) = j + ( nr - 1 ) * nc;
      else
        S(k) = j + ( i - 2 ) * nc;
      end

      if ( j == nc )
        E(k) = 1 + ( i - 1 ) * nc;
      else
        E(k) = j + 1 + ( i - 1 ) * nc;
      end

      if ( j == 1 )
        W(k) = nc + ( i - 1 ) * nc;
      else
        W(k) = j - 1 + ( i - 1 ) * nc;
      end

    end
  end

  return
end
