**ACTION:**- Generate the next combination, a subset of a specified size chosen
from a specified number of items.
**CALL:**`CALL sla_COMBN (NSEL, NCAND, LIST, J)`

**GIVEN:**-

*NSEL***I**number of items (subset size) *NCAND***I**number of candidates (set size)

**GIVEN and RETURNED:**-

*LIST***I(NSEL)**latest combination, LIST(1)=0 to initialize

**RETURNED:**-

*J***I**status: -1 = illegal NSEL or NCAND 0 = OK +1 = no more combinations available

**NOTES:**- 1.
- NSEL and NCAND must both be at least 1, and NSEL must be less than or equal to NCAND.
- 2.
- This routine returns, in the LIST array, a subset of NSEL integers chosen from the range 1 to NCAND inclusive, in ascending order. Before calling the routine for the first time, the caller must set the first element of the LIST array to zero (any value less than 1 will do) to cause initialization.
- 3.
- The first combination to be generated is:
LIST(1)=1, LIST(2)=2, ..., LIST(NSEL)=NSEL

This is also the combination returned for the ``finished'' (J=1) case. The final permutation to be generated is:LIST(1)=NCAND, LIST(2)=NCAND-1, ...,

LIST(NSEL)=NCAND-NSEL+1 - 4.
- If the ``finished'' (J=1) status is ignored, the routine continues to deliver combinations, the pattern repeating every NCAND!/(NSEL!(NCAND-NSEL)!) calls.
- 5.
- The algorithm is by R.F.Warren-Smith (private communication).

Starlink User Note 67

P. T. Wallace

12 October 1999

E-mail:ptw@star.rl.ac.uk