How to resolve the algorithm Combinations with repetitions step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Combinations with repetitions step by step in the Ada programming language

Table of Contents

Problem Statement

The set of combinations with repetitions is computed from a set,

S

{\displaystyle S}

(of cardinality

n

{\displaystyle n}

), and a size of resulting selection,

k

{\displaystyle k}

, by reporting the sets of cardinality

k

{\displaystyle k}

where each member of those sets is chosen from

S

{\displaystyle S}

. In the real world, it is about choosing sets where there is a “large” supply of each type of element and where the order of choice does not matter. For example: Note that both the order of items within a pair, and the order of the pairs given in the answer is not significant; the pairs represent multisets. Also note that doughnut can also be spelled donut.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Combinations with repetitions step by step in the Ada programming language

Source code in the ada programming language

with Ada.Text_IO;
procedure Combinations is

   generic
      type Set is (<>);
   function Combinations
     (Count  : Positive;
      Output : Boolean := False)
      return   Natural;

   function Combinations
     (Count  : Positive;
      Output : Boolean := False)
      return   Natural
   is
      package Set_IO is new Ada.Text_IO.Enumeration_IO (Set);
      type Set_Array is array (Positive range <>) of Set;
      Empty_Array : Set_Array (1 .. 0);
      function Recurse_Combinations
        (Number : Positive;
         First  : Set;
         Prefix : Set_Array)
         return   Natural
      is
         Combination_Count : Natural := 0;
      begin
         for Next in First .. Set'Last loop
            if Number = 1 then
               Combination_Count := Combination_Count + 1;
               if Output then
                  for Element in Prefix'Range loop
                     Set_IO.Put (Prefix (Element));
                     Ada.Text_IO.Put ('+');
                  end loop;
                  Set_IO.Put (Next);
                  Ada.Text_IO.New_Line;
               end if;
            else
               Combination_Count := Combination_Count +
                                    Recurse_Combinations
                                       (Number - 1,
                                        Next,
                                        Prefix & (1 => Next));
            end if;
         end loop;
         return Combination_Count;
      end Recurse_Combinations;
   begin
      return Recurse_Combinations (Count, Set'First, Empty_Array);
   end Combinations;

   type Donuts is (Iced, Jam, Plain);
   function Donut_Combinations is new Combinations (Donuts);

   subtype Ten is Positive range 1 .. 10;
   function Ten_Combinations is new Combinations (Ten);

   Donut_Count : constant Natural :=
      Donut_Combinations (Count => 2, Output => True);
   Ten_Count   : constant Natural := Ten_Combinations (Count => 3);
begin
   Ada.Text_IO.Put_Line ("Total Donuts:" & Natural'Image (Donut_Count));
   Ada.Text_IO.Put_Line ("Total Tens:" & Natural'Image (Ten_Count));
end Combinations;


  

You may also check:How to resolve the algorithm Power set step by step in the Swift programming language
You may also check:How to resolve the algorithm Tau number step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Dot product step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Chat server step by step in the Erlang programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Swift programming language