Task

Find and display the sum of elements that are below the main diagonal of a matrix.

The matrix should be a square matrix.



 ───   Matrix to be used:   ───


     [[1,3,7,8,10],
      [2,4,16,14,4],
      [3,1,9,18,11],
      [12,14,17,18,20],
      [7,1,3,9,5]] 



100H: /* SUM THE ELEMENTS BELOW THE MAIN DIAGONAL OF A MATRIX                */
 
   /* CP/M BDOS SYSTEM CALL, IGNORE THE RETURN VALUE                         */
   BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5;     END;
   PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S );      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;
 
   /* RETURNS THE SUM OF THE ELEMENTS BELOW THE MAIN DIAGONAL OF MX          */
   /* MX WOULD BE DECLARED AS ''( UB, UB )ADDRESS'' IF PL/M SUPPORTED        */
   /* 2-DIMENSIONAL ARRAYS, IT DOESN'T SO MX MUST ACTULLY BE DECLARED        */
   /* ''( UB * UB )ADDRESS'' - EXCEPT THE BOUND MUST BE A CONSTANT, NOT AN   */
   /* EXPRESSION                                                             */
   /* NOTE ''ADDRESS'' MEANS UNSIGNED 16-BIT QUANTITY, WHICH CAN BE USED FOR */
   /* OTHER PURPOSES THAN JUST POINTERS                                      */
   LOWER$SUM: PROCEDURE( MX, UB )ADDRESS;
      DECLARE ( MX, UB ) ADDRESS;
      DECLARE ( SUM, R, C, STRIDE, R$PTR ) ADDRESS;
      DECLARE M$PTR ADDRESS, M$VALUE BASED M$PTR ADDRESS;
      SUM    = 0;
      STRIDE = UB + UB;
      R$PTR  = MX + STRIDE;      /* ADDRESS OF ROW 1 ( THE FIRST ROW IS 0 )  */
      DO R = 1 TO UB - 1;
         M$PTR = R$PTR;
         DO C = 0 TO R - 1;
            SUM = SUM + M$VALUE;
            M$PTR = M$PTR + 2;
         END;
         R$PTR = R$PTR + STRIDE; /* ADDRESS OF THE NEXT ROW                  */
      END;
      RETURN SUM;
   END LOWER$SUM ;
 
   /* TASK TEST CASE                                                         */
   DECLARE T ( 25 )ADDRESS
                   INITIAL(  1,  3,  7,  8, 10
                          ,  2,  4, 16, 14,  4
                          ,  3,  1,  9, 18, 11
                          , 12, 14, 17, 18, 20
                          ,  7,  1,  3,  9,  5
                          );
   CALL PR$NUMBER( LOWER$SUM( .T, 5 ) );
 
EOF