Click or drag to resize

Combination Constructor (Int32, Int32)

Initializes a new combination from elements supplied in source picked from the supplied number of choices.

Namespace:  Kaos.Combinatorics
Assembly:  KaosCombinatorics (in KaosCombinatorics.dll) Version: 6.0.0.0
Syntax
C#
public Combination(
	int choices,
	int[] source
)

Parameters

choices
Type: SystemInt32
Number of values to pick from.
source
Type: SystemInt32
Array of integers.
Exceptions
ExceptionCondition
ArgumentNullExceptionWhen source is null.
ArgumentOutOfRangeException When length of source is greater than picks; when source contains invalid data.
Remarks
Supplying a value for choices that is greater than the number of elements in source will instantiate a k-combination also known as a pick-combination.
Examples
C#
using System;
using Kaos.Combinatorics;

namespace ExampleApp
{
    class CnExample04
    {
        static void Main()
        {
            // Create a k-combination of the supplied rank:
            var cn = new Combination (choices:6, picks:4, rank:2);
            Console.WriteLine ($"{cn}  n={cn.Choices}, k={cn.Picks}, rank={cn.Rank}\n");

            // Assign -1 to get the last rank:
            cn.Rank = -1;
            Console.WriteLine ($"{cn}  n={cn.Choices}, k={cn.Picks}, last={cn.Rank}\n");

            // Rank will always stay in bounds:
            cn.Rank = cn.Rank + 1;
            Console.WriteLine ($"{cn}  n={cn.Choices}, k={cn.Picks}, rank={cn.Rank}\n");

            // Create a k-combination of n=9 from the supplied picks:
            cn = new Combination (9, new int[] { 2, 4, 6, 8 });
            Console.WriteLine ($"{cn}  n={cn.Choices}, k={cn.Picks}, rank={cn.Rank}");
        }

        /* Output:

        { 0, 1, 2, 5 }  n=6, k=4, rank=2

        { 2, 3, 4, 5 }  n=6, k=4, last=14

        { 0, 1, 2, 3 }  n=6, k=4, rank=0

        { 2, 4, 6, 8 }  n=9, k=4, rank=105

        */
    }
}
See Also