How to resolve the algorithm Combinations step by step in the ERRE programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Combinations step by step in the ERRE programming language

Table of Contents

Problem Statement

Given non-negative integers   m   and   n,   generate all size   m   combinations   of the integers from   0   (zero)   to   n-1   in sorted order   (each combination is sorted and the entire table is sorted).

3   comb   5     is: If it is more "natural" in your language to start counting from   1   (unity) instead of   0   (zero), the combinations can be of the integers from   1   to   n.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Combinations step by step in the ERRE programming language

Source code in the erre programming language

PROGRAM COMBINATIONS

CONST M_MAX=3,N_MAX=5

DIM COMBINATION[M_MAX],STACK[100,1]

PROCEDURE GENERATE(M)
   LOCAL I
   IF (M>M_MAX) THEN
    FOR I=1 TO M_MAX DO
     PRINT(COMBINATION[I];" ";)
    END FOR
    PRINT
   ELSE
    FOR N=1 TO N_MAX DO
      IF ((M=1) OR (N>COMBINATION[M-1])) THEN
        COMBINATION[M]=N
        ! --- PUSH STACK -----------
        STACK[SP,0]=M  STACK[SP,1]=N
        SP=SP+1
        ! --------------------------

        GENERATE(M+1)

        ! --- POP STACK ------------
        SP=SP-1
        M=STACK[SP,0] N=STACK[SP,1]
        ! --------------------------
      END IF
    END FOR
   END IF
END PROCEDURE

BEGIN
 GENERATE(1)
END PROGRAM

  

You may also check:How to resolve the algorithm Towers of Hanoi step by step in the BASIC programming language
You may also check:How to resolve the algorithm Word ladder step by step in the jq programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Optional parameters step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the MUMPS programming language