How to resolve the algorithm Department numbers step by step in the PL/M programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Department numbers step by step in the PL/M programming language

Table of Contents

Problem Statement

There is a highly organized city that has decided to assign a number to each of their departments:

Each department can have a number between   1   and   7   (inclusive). The three department numbers are to be unique (different from each other) and must add up to   12. The Chief of the Police doesn't like odd numbers and wants to have an even number for his department.

Write a computer program which outputs all valid combinations.

Possible output   (for the 1st and 14th solutions):

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Department numbers step by step in the PL/M programming language

Source code in the pl/m programming language

100H: /* SHOW POSSIBLE DEPARTMENT NUMBERS FOR POLICE, SANITATION AND FIRE    */
      /* THE POLICE DEPARTMENT NUMBER MUST BE EVEN, ALL DEPARTMENT NUMBERS   */
      /* MUST BE IN THE RANGE 1 .. 7 AND THE NUMBERS MUST SUM TO 12          */

   /* CP/M SYSTEM CALL AND I/O ROUTINES                                      */
   BDOS:      PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
   PR$CHAR:   PROCEDURE( C ); DECLARE C BYTE;    CALL BDOS( 2, C );  END;
   PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S );  END;
   PR$NL:     PROCEDURE;   CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
   PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH  */
      DECLARE N ADDRESS;
      DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
      V = N;
      W = LAST( N$STR );
      N$STR( W ) = '$';
      N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
      DO WHILE( ( V := V / 10 ) > 0 );
         N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
      END;
      CALL PR$STRING( .N$STR( W ) );
   END PR$NUMBER;

   /* TASK                                                                   */
   DECLARE MAX$DEPARTMENT$NUMBER LITERALLY '7';
   DECLARE DEPARTMENT$SUM        LITERALLY '12';
   DECLARE ( POLICE, SANITATION, FIRE ) BYTE;

   CALL PR$STRING( .'POLICE SANITATION FIRE$' );
   CALL PR$NL;

   DO POLICE = 2 TO MAX$DEPARTMENT$NUMBER BY 2;
      DO SANITATION = 1 TO MAX$DEPARTMENT$NUMBER;
         IF SANITATION <> POLICE THEN DO;
            FIRE = ( DEPARTMENT$SUM - POLICE ) - SANITATION;
            IF  FIRE <= MAX$DEPARTMENT$NUMBER
            AND FIRE <> SANITATION
            AND FIRE <> POLICE
            THEN DO;
               CALL PR$STRING( .'     $'      ); CALL PR$NUMBER( POLICE );
               CALL PR$STRING( .'          $' ); CALL PR$NUMBER( SANITATION );
               CALL PR$STRING( .'    $'       ); CALL PR$NUMBER( FIRE );
               CALL PR$NL;
            END;
         END;
      END;
   END;

EOF

  

You may also check:How to resolve the algorithm Remove duplicate elements step by step in the ECL programming language
You may also check:How to resolve the algorithm Call a function in a shared library step by step in the Phix programming language
You may also check:How to resolve the algorithm The Name Game step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the C programming language
You may also check:How to resolve the algorithm Pointers and references step by step in the Tcl programming language