Sum of elements below main diagonal of matrix: Difference between revisions

→‎{{header|RPL}}: HP-49/50 version
m (add REXX - with different understanding of below :-))
(→‎{{header|RPL}}: HP-49/50 version)
(90 intermediate revisions by 35 users not shown)
Line 1:
{{Draft task}}
 
;Task:
;Task:Find sum of elements below main diagonal of matrix.
Find and display the sum of elements that are below the main diagonal of a matrix.
<br>The matrix should be square matrix.
 
<br><br>Matrix =
The matrix should be a square matrix.
<br><br>
 
[[1,3,7,8,10],
 
[2,4,16,14,4],
::: ─── &nbsp; Matrix to be used: &nbsp; ───
[3,1,9,18,11],
<big>
[12,14,17,18,20],
[7,[1,3,97,5]8,10],
[2,4,16,14,4],
[3,1,9,18,11],
[12,14,17,18,20],
[7,1,3,9,5]] </big>
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F sumBelowDiagonal(m)
V result = 0
L(i) 1 .< m.len
L(j) 0 .< i
result += m[i][j]
R result
 
V m = [[ 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]]
 
print(sumBelowDiagonal(m))</syntaxhighlight>
 
{{out}}
<pre>
69
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC PrintMatrix(INT ARRAY m BYTE size)
BYTE x,y
INT v
 
FOR y=0 TO size-1
DO
FOR x=0 TO size-1
DO
v=m(x+y*size)
IF v<10 THEN Put(32) FI
PrintB(v) Put(32)
OD
PutE()
OD
RETURN
 
INT FUNC SumBelowDiagonal(INT ARRAY m BYTE size)
BYTE x,y
INT sum
 
sum=0
FOR y=1 TO size-1
DO
FOR x=0 TO y-1
DO
sum==+m(x+y*size)
OD
OD
RETURN (sum)
 
PROC Main()
INT sum
INT ARRAY m=[
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]
 
PrintE("Matrix")
PrintMatrix(m,5)
PutE()
sum=SumBelowDiagonal(m,5)
PrintF("Sum below diagonal is %I",sum)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_elements_below_main_diagonal_of_matrix.png Screenshot from Atari 8-bit computer]
<pre>
Matrix
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
 
Sum below diagonal is 69
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_Io;
with Ada.Numerics.Generic_Real_Arrays;
 
procedure Sum_Below_Diagonals is
 
type Real is new Float;
 
package Real_Arrays
is new Ada.Numerics.Generic_Real_Arrays (Real);
 
function Sum_Below_Diagonal (M : Real_Arrays.Real_Matrix) return Real
with Pre => M'Length (1) = M'Length (2)
is
Sum : Real := 0.0;
begin
for Row in 0 .. M'Length (1) - 1 loop
for Col in 0 .. Row - 1 loop
Sum := Sum + M (M'First (1) + Row,
M'First (2) + Col);
end loop;
end loop;
return Sum;
end Sum_Below_Diagonal;
 
M : constant Real_Arrays.Real_Matrix :=
(( 1.0, 3.0, 7.0, 8.0, 10.0),
( 2.0, 4.0, 16.0, 14.0, 4.0),
( 3.0, 1.0, 9.0, 18.0, 11.0),
(12.0, 14.0, 17.0, 18.0, 20.0),
( 7.0, 1.0, 3.0, 9.0, 5.0));
Sum : constant Real := Sum_Below_Diagonal (M);
 
package Real_Io is new Ada.Text_Io.Float_Io (Real);
use Ada.Text_Io, Real_Io;
begin
Put ("Sum below diagonal: ");
Put (Sum, Exp => 0, Aft => 1);
New_Line;
end Sum_Below_Diagonals;</syntaxhighlight>
{{out}}<pre>Sum below diagonal: 69.0</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN # sum the elements below the main diagonal of a matrix #
# returns the sum of the elements below the main diagonal #
# of m, m must be a square matrix #
OP LOWERSUM = ( [,]INT m )INT:
IF 1 LWB m /= 2 LWB m OR 1 UPB m /= 2 UPB m THEN
# the matrix isn't square #
print( ( "Matrix must be suare for LOWERSUM", newline ) );
stop
ELSE
# have a square matrix #
INT sum := 0;
FOR r FROM 1 LWB m + 1 TO 1 UPB m DO
FOR c FROM 1 LWB m TO r - 1 DO
sum +:= m[ r, c ]
OD
OD;
sum
FI; # LOWERSUM #
# task test case #
print( ( whole( LOWERSUM [,]INT( ( 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 )
)
, 0
)
, newline
)
)
END</syntaxhighlight>
{{out}}
<pre>
69
</pre>
 
=={{header|ALGOL W}}==
One of the rare occasions where the lack of lower/upper bound operators in Algol W actually simplifies things, assuming the programmer gets things right...
<syntaxhighlight lang="algolw">begin % sum the elements below the main diagonal of a matrix %
% returns the sum of the elements below the main diagonal %
% of m, m must have bounds lb :: ub, lb :: ub %
integer procedure lowerSum ( integer array m ( *, * )
; integer value lb, ub
) ;
begin
integer sum;
sum := 0;
for r := lb + 1 until ub do begin
for c := lb until r - 1 do sum := sum + m( r, c )
end for_r;
sum
end lowerSum ;
begin % task test case %
integer array m ( 1 :: 5, 1 :: 5 );
integer r, c;
r := 1; c := 0; for v := 1, 3, 7, 8, 10 do begin c := c + 1; m( r, c ) := v end;
r := 2; c := 0; for v := 2, 4, 16, 14, 4 do begin c := c + 1; m( r, c ) := v end;
r := 3; c := 0; for v := 3, 1, 9, 18, 11 do begin c := c + 1; m( r, c ) := v end;
r := 4; c := 0; for v := 12, 14, 17, 18, 20 do begin c := c + 1; m( r, c ) := v end;
r := 5; c := 0; for v := 7, 1, 3, 9, 5 do begin c := c + 1; m( r, c ) := v end;
write( i_w := 1, lowerSum( m, 1, 5 ) )
end
end.</syntaxhighlight>
{{out}}
<pre>
69
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">sum_below_diagonal ← +/(∊⊢×(>/¨⍳∘⍴))</syntaxhighlight>
{{out}}
<pre> matrix ← 5 5⍴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
sum_below_diagonal matrix
69</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">square?: $[m][every? m 'row -> equal? size row size m]
 
sumBelow: function [m][
ensure -> square? m
fold.seed: 0 .with:'i m [a b] -> a + sum take b i
]
 
m: [[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 ]]
 
print ["Sum below diagonal is" sumBelow m]</syntaxhighlight>
 
{{out}}
 
<pre>Sum below diagonal is 69</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">matrx :=[[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]]
sumA := sumB := sumD := sumAll := 0
for r, obj in matrx
for c, val in obj
sumAll += val
,sumA += r<c ? val : 0
,sumB += r>c ? val : 0
,sumD += r=c ? val : 0
 
MsgBox % result := "sum above diagonal = " sumA
. "`nsum below diagonal = " sumB
. "`nsum on diagonal = " sumD
. "`nsum all = " sumAll</syntaxhighlight>
{{out}}
<pre>sum above diagonal = 111
sum below diagonal = 69
sum on diagonal = 37
sum all = 217</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUM_OF_ELEMENTS_BELOW_MAIN_DIAGONAL_OF_MATRIX.AWK
BEGIN {
arr1[++n] = "1,3,7,8,10"
arr1[++n] = "2,4,16,14,4"
arr1[++n] = "3,1,9,18,11"
arr1[++n] = "12,14,17,18,20"
arr1[++n] = "7,1,3,9,5"
for (i=1; i<=n; i++) {
x = split(arr1[i],arr2,",")
if (x != n) {
printf("error: row %d has %d elements; S/B %d\n",i,x,n)
errors++
continue
}
for (j=1; j<i; j++) { # below main diagonal
sum_b += arr2[j]
cnt_b++
}
for (j=i+1; j<=n; j++) { # above main diagonal
sum_a += arr2[j]
cnt_a++
}
for (j=1; j<=i; j++) { # on main diagonal
if (j == i) {
sum_o += arr2[j]
cnt_o++
}
}
}
if (errors > 0) { exit(1) }
printf("%5g Sum of the %d elements below main diagonal\n",sum_b,cnt_b)
printf("%5g Sum of the %d elements above main diagonal\n",sum_a,cnt_a)
printf("%5g Sum of the %d elements on main diagonal\n",sum_o,cnt_o)
printf("%5g Sum of the %d elements in the matrix\n",sum_b+sum_a+sum_o,cnt_b+cnt_a+cnt_o)
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
69 Sum of the 10 elements below main diagonal
111 Sum of the 10 elements above main diagonal
37 Sum of the 5 elements on main diagonal
217 Sum of the 25 elements in the matrix
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">arraybase 1
dim diag = {{ 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}}
ind = diag[?,]
sumDiag = 0
 
for x = 1 to diag[?,]
for y = 1 to diag[,?]-ind
sumDiag += diag[x, y]
next y
ind -= 1
next x
 
print "Sum of elements below main diagonal of matrix is "; sumDiag
end</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">Dim As Integer diag(1 To 5, 1 To 5) = { _
{ 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}}
Dim As Integer lenDiag = Ubound(diag), ind = lenDiag
Dim As Integer sumDiag = 0, x, y
 
For x = 1 To lenDiag
For y = 1 To lenDiag-ind
sumDiag += diag(x, y)
Next y
ind -= 1
Next x
 
Print "Sum of elements below main diagonal of matrix is"; sumDiag
Sleep</syntaxhighlight>
{{out}}
<pre>Sum of elements below main diagonal of matrix is 69</pre>
 
==={{header|GW-BASIC}}===
<syntaxhighlight lang="gwbasic">10 DATA 1,3,7,8,10
20 DATA 2,4,16,14,4
30 DATA 3,1,9,18,11
40 DATA 12,14,17,18,20
50 DATA 7,1,3,9,5
60 FOR ROW = 1 TO 5
70 FOR COL = 1 TO 5
80 READ N
90 IF ROW > COL THEN SUM = SUM + N
100 NEXT COL
110 NEXT ROW
120 PRINT SUM</syntaxhighlight>
{{out}}<pre>69</pre>
==={{header|QBasic}}===
{{works with|QBasic}}
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">DEFINT A-Z
 
DIM diag(1 TO 5, 1 TO 5)
lenDiag = UBOUND(diag)
ind = lenDiag
sumDiag = 0
 
FOR x = 1 TO lenDiag
FOR y = 1 TO lenDiag
READ diag(x, y)
NEXT y
NEXT x
 
FOR x = 1 TO lenDiag
FOR y = 1 TO lenDiag - ind
sumDiag = sumDiag + diag(x, y)
NEXT y
ind = ind - 1
NEXT x
 
PRINT "Sum of elements below main diagonal of matrix is"; sumDiag
END
 
DATA 1, 3, 7, 8,10
DATA 2, 4,16,14, 4
DATA 3, 1, 9,18,11
DATA 12,14,17,18,20
DATA 7, 1, 3, 9, 5</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">DIM diag(5, 5)
LET lenDiag = UBOUND(diag, 1)
LET ind = lenDiag
LET sumDiag = 0
 
DATA 1, 3, 7, 8,10
DATA 2, 4,16,14, 4
DATA 3, 1, 9,18,11
DATA 12,14,17,18,20
DATA 7, 1, 3, 9, 5
 
FOR x = 1 TO lenDiag
FOR y = 1 TO lenDiag
READ diag(x, y)
NEXT y
NEXT x
 
FOR x = 1 TO lenDiag
FOR y = 1 TO lenDiag - ind
LET sumDiag = sumDiag + diag(x, y)
NEXT y
LET ind = ind - 1
NEXT x
 
PRINT "Sum of elements below main diagonal of matrix:"; sumDiag
END</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">dim diag(5, 5)
lenDiag = arraysize(diag(),1)
ind = lenDiag
sumDiag = 0
 
for x = 1 to lenDiag
for y = 1 to lenDiag
read diag(x, y)
next y
next x
 
for x = 1 to lenDiag
for y = 1 to lenDiag-ind
sumDiag = sumDiag + diag(x, y)
next y
ind = ind - 1
next x
 
print "Sum of elements below main diagonal of matrix: ", sumDiag
end
 
data 1, 3, 7, 8,10
data 2, 4,16,14, 4
data 3, 1, 9,18,11
data 12,14,17,18,20
data 7, 1, 3, 9, 5</syntaxhighlight>
 
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">SumBelowDiagonal ← +´∘⥊⊢×(>⌜´)∘(↕¨≢)
 
matrix ← >⟨⟨ 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⟩⟩
 
SumBelowDiagonal matrix</syntaxhighlight>
{{out}}
<pre>69</pre>
 
=={{header|C}}==
Interactive program which reads the matrix from a file :
<syntaxhighlight lang="c">
#include<stdlib.h>
#include<stdio.h>
 
typedef struct{
int rows,cols;
int** dataSet;
}matrix;
 
matrix readMatrix(char* dataFile){
FILE* fp = fopen(dataFile,"r");
matrix rosetta;
int i,j;
fscanf(fp,"%d%d",&rosetta.rows,&rosetta.cols);
rosetta.dataSet = (int**)malloc(rosetta.rows*sizeof(int*));
for(i=0;i<rosetta.rows;i++){
rosetta.dataSet[i] = (int*)malloc(rosetta.cols*sizeof(int));
for(j=0;j<rosetta.cols;j++)
fscanf(fp,"%d",&rosetta.dataSet[i][j]);
}
fclose(fp);
return rosetta;
}
 
void printMatrix(matrix rosetta){
int i,j;
for(i=0;i<rosetta.rows;i++){
printf("\n");
for(j=0;j<rosetta.cols;j++)
printf("%3d",rosetta.dataSet[i][j]);
}
}
 
int findSum(matrix rosetta){
int i,j,sum = 0;
for(i=1;i<rosetta.rows;i++){
for(j=0;j<i;j++){
sum += rosetta.dataSet[i][j];
}
}
return sum;
}
 
int main(int argC,char* argV[])
{
if(argC!=2)
return printf("Usage : %s <filename>",argV[0]);
matrix data = readMatrix(argV[1]);
printf("\n\nMatrix is : \n\n");
printMatrix(data);
printf("\n\nSum below main diagonal : %d",findSum(data));
return 0;
}
</syntaxhighlight>
 
Input Data file, first row specifies rows and columns :
<pre>
5 5
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
</pre>
And output follows :
{{out}}
<pre>
C:\My Projects\BGI>a.exe rosettaData.txt
 
 
Matrix is :
 
 
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
 
Sum below main diagonal : 69
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
template<typename T>
T sum_below_diagonal(const std::vector<std::vector<T>>& matrix) {
T sum = 0;
for (std::size_t y = 0; y < matrix.size(); y++)
for (std::size_t x = 0; x < matrix[y].size() && x < y; x++)
sum += matrix[y][x];
return sum;
}
 
int main() {
std::vector<std::vector<int>> matrix = {
{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}
};
std::cout << sum_below_diagonal(matrix) << std::endl;
return 0;
}</syntaxhighlight>
{{out}}
<pre>69</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
type T5Matrix = array[0..4, 0..4] of Double;
 
var TestMatrix: T5Matrix =
(( 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));
 
 
function BottomTriangleSum(Mat: T5Matrix): double;
var X,Y: integer;
begin
Result:=0;
for Y:=1 to 4 do
for X:=0 to Y-1 do
begin
Result:=Result+Mat[Y,X];
end;
end;
 
 
procedure ShowBottomTriangleSum(Memo: TMemo);
var Sum: double;
begin
Sum:=BottomTriangleSum(TestMatrix);
Memo.Lines.Add(IntToStr(Trunc(Sum)));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
69
Elapsed Time: 0.873 ms.
 
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc sumbd . m[][] r .
r = 0
for i = 2 to len m[][]
for j = 1 to i - 1
r += m[i][j]
.
.
.
m[][] = [ [ 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 ] ]
sumbd m[][] r
print r
</syntaxhighlight>
 
{{out}}
<pre>
69
</pre>
 
=={{header|Excel}}==
===LAMBDA===
 
Binding the name ''matrixTriangle'' to the following lambda expression in the Name Manager of the Excel WorkBook:
 
(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])
 
{{Works with|Office 365 betas 2021}}
<syntaxhighlight lang="lisp">=LAMBDA(isUpper,
LAMBDA(matrix,
LET(
nCols, COLUMNS(matrix),
nRows, ROWS(matrix),
ixs, SEQUENCE(nRows, nCols, 0, 1),
x, MOD(ixs, nCols),
y, QUOTIENT(ixs, nRows),
IF(nCols=nRows,
LET(
p, LAMBDA(x, y,
IF(isUpper, x > y, x < y)
),
IF(p(x, y),
INDEX(matrix, 1 + y, 1 + x),
0
)
),
"Matrix not square"
)
)
)
)</syntaxhighlight>
 
{{Out}}
The formulae in cells B2 and B9 define and populate the matrices which fill the ranges B2:F6 and B9:F12
 
(The formula in B9 differs from that in B2 only in the first (Boolean) argument)
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="6" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=matrixTriangle(FALSE)(B16#)
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
| D
| E
| F
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
|
|
|
|
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
| style="text-align:right" | Lower triangle:
| style="text-align:right; background-color:#cbcefb" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| style="text-align:right" | 2
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| style="text-align:right" | 3
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
|
| style="text-align:right" | 12
| style="text-align:right" | 14
| style="text-align:right" | 17
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
|
| style="text-align:right" | 7
| style="text-align:right" | 1
| style="text-align:right" | 3
| style="text-align:right" | 9
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 7
| style="text-align:right; font-weight:bold" | Sum
| style="text-align:right; font-weight:bold" | 69
|
|
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 8
|
|
|
|
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 9
| style="text-align:right" | Upper triangle:
| style="text-align:right" | 0
| style="text-align:right" | 3
| style="text-align:right" | 7
| style="text-align:right" | 8
| style="text-align:right" | 10
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 10
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 16
| style="text-align:right" | 14
| style="text-align:right" | 4
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 11
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 18
| style="text-align:right" | 11
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 12
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 20
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 13
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 14
| style="text-align:right; font-weight:bold" | Sum
| style="text-align:right; font-weight:bold" | 111
|
|
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 15
|
|
|
|
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 16
| style="text-align:right" | Full matrix
| style="text-align:right" | 1
| style="text-align:right" | 3
| style="text-align:right" | 7
| style="text-align:right" | 8
| style="text-align:right" | 10
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 17
|
| style="text-align:right" | 2
| style="text-align:right" | 4
| style="text-align:right" | 16
| style="text-align:right" | 14
| style="text-align:right" | 4
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 18
|
| style="text-align:right" | 3
| style="text-align:right" | 1
| style="text-align:right" | 9
| style="text-align:right" | 18
| style="text-align:right" | 11
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 19
|
| style="text-align:right" | 12
| style="text-align:right" | 14
| style="text-align:right" | 17
| style="text-align:right" | 18
| style="text-align:right" | 20
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 20
|
| style="text-align:right" | 7
| style="text-align:right" | 1
| style="text-align:right" | 3
| style="text-align:right" | 9
| style="text-align:right" | 5
|}
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Sum below leading diagnal. Nigel Galloway: July 21st., 2021
let _,n=[[ 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]]|>List.fold(fun(n,g) i->let i,_=i|>List.splitAt n in (n+1,g+(i|>List.sum)))(0,0) in printfn "%d" n
</syntaxhighlight>
{{out}}
<pre>
69
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<syntaxhighlight lang="factor">USING: kernel math math.matrices prettyprint sequences ;
 
: sum-below-diagonal ( matrix -- sum )
dup square-matrix? [ "Matrix must be square." throw ] unless
0 swap [ head sum + ] each-index ;
 
{
{ 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 }
} sum-below-diagonal .</syntaxhighlight>
{{out}}
<pre>
69
</pre>
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"log"
)
 
func main() {
m := [][]int{
{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},
}
if len(m) != len(m[0]) {
log.Fatal("Matrix must be square.")
}
sum := 0
for i := 1; i < len(m); i++ {
for j := 0; j < i; j++ {
sum = sum + m[i][j]
}
}
fmt.Println("Sum of elements below main diagonal is", sum)
}</syntaxhighlight>
 
{{out}}
<pre>
Sum of elements below main diagonal is 69
</pre>
 
=={{header|Haskell}}==
Defining both upper and lower triangle of a square matrix:
 
<syntaxhighlight lang="haskell">----------------- UPPER OR LOWER TRIANGLE ----------------
 
matrixTriangle :: Bool -> [[a]] -> Either String [[a]]
matrixTriangle upper matrix
| upper = go drop id
| otherwise = go take pred
where
go f g
| isSquare matrix =
(Right . snd) $
foldr
(\xs (n, rows) -> (pred n, f n xs : rows))
(g $ length matrix, [])
matrix
| otherwise = Left "Defined only for a square matrix."
 
isSquare :: [[a]] -> Bool
isSquare rows = all ((n ==) . length) rows
where
n = length rows
 
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ putStrLn $
zipWith
( flip ((<>) . (<> " triangle:\n\t"))
. either id (show . sum . concat)
)
( [matrixTriangle] <*> [False, True]
<*> [ [ [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]
]
]
)
["Lower", "Upper"]</syntaxhighlight>
{{Out}}
<pre>Lower triangle:
69
Upper triangle:
111</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">sum_below_diagonal =: [:+/@,[*>/~@i.@#</syntaxhighlight>
{{out}}
<pre> mat
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
sum_below_diagonal mat
69</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">public static void main(String[] args) {
int[][] matrix = {{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}};
int sum = 0;
for (int row = 1; row < matrix.length; row++) {
for (int col = 0; col < row; col++) {
sum += matrix[row][col];
}
}
System.out.println(sum);
}</syntaxhighlight>
{{Out}}
<pre>69</pre>
 
=={{header|JavaScript}}==
 
Defining the lower triangle of a square matrix.
 
<syntaxhighlight lang="javascript">(() => {
"use strict";
 
// -------- LOWER TRIANGLE OF A SQUARE MATRIX --------
 
// lowerTriangle :: [[a]] -> Either String [[a]]
const lowerTriangle = matrix =>
// Either a message, if the matrix is not square,
// or the lower triangle of the matrix.
isSquare(matrix) ? (
Right(
matrix.reduce(
([n, rows], xs) => [
1 + n,
rows.concat([xs.slice(0, n)])
],
[0, []]
)[1]
)
) : Left("Not a square matrix");
 
 
// isSquare :: [[a]] -> Bool
const isSquare = rows => {
// True if the length of every row in the matrix
// matches the number of rows in the matrix.
const n = rows.length;
 
return rows.every(x => n === x.length);
};
 
// ---------------------- TEST -----------------------
const main = () =>
either(
msg => `Lower triangle undefined :: ${msg}`
)(
rows => sum([].concat(...rows))
)(
lowerTriangle([
[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]
])
);
 
// --------------------- GENERIC ---------------------
 
// Left :: a -> Either a b
const Left = x => ({
type: "Either",
Left: x
});
 
 
// Right :: b -> Either a b
const Right = x => ({
type: "Either",
Right: x
});
 
 
// either :: (a -> c) -> (b -> c) -> Either a b -> c
const either = fl =>
// Application of the function fl to the
// contents of any Left value in e, or
// the application of fr to its Right value.
fr => e => e.Left ? (
fl(e.Left)
) : fr(e.Right);
 
 
// sum :: [Num] -> Num
const sum = xs =>
// The numeric sum of all values in xs.
xs.reduce((a, x) => a + x, 0);
 
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
<pre>69</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
def add(s): reduce s as $x (null; . + $x);
 
# input: a square matrix
def sum_below_diagonal:
add( range(0;length) as $i | .[$i][:$i][] ) ;
</syntaxhighlight>
The task:
<syntaxhighlight lang="jq"> [[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]]
| sum_below_diagonal</syntaxhighlight>
{{out}}
<pre>
69
</pre>
 
 
=={{header|Julia}}==
Line 16 ⟶ 1,127:
the components of the matrix A to the left and below the main diagonal. tril(A, -1) returns the lower triangular
elements of A excluding the main diagonal. The excluded elements of the matrix are set to 0.
<langsyntaxhighlight lang="julia">using LinearAlgebra
 
A = [ 1 3 7 8 10;
Line 29 ⟶ 1,140:
 
@show sum(tril(A, -1)) # 69
</langsyntaxhighlight>{{out}}<pre>
tril(A) = [1 0 0 0 0; 2 4 0 0 0; 3 1 9 0 0; 12 14 17 18 0; 7 1 3 9 5]
tril(A, -1) = [0 0 0 0 0; 2 0 0 0 0; 3 1 0 0 0; 12 14 17 0 0; 7 1 3 9 0]
sum(tril(A, -1)) = 69
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">m = {{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}};
Total[LowerTriangularize[m, -1], 2]</syntaxhighlight>
{{out}}
<pre>69</pre>
 
=={{header|MATLAB}}==
<syntaxhighlight lang="MATLAB">
clear all;close all;clc;
A = [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];
 
lower_triangular = tril(A, -1);
sum_of_elements = sum(lower_triangular(:)); % Sum of all elements in the lower triangular part
 
fprintf('%d\n', sum_of_elements); % Prints 69
</syntaxhighlight>
{{out}}
<pre>
69
</pre>
 
=={{header|MiniZinc}}==
<syntaxhighlight lang="minizinc">
% Sum below leading diagnal. Nigel Galloway: July 22nd., 2021
array [1..5,1..5] of int: N=[|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|];
int: res=sum(n,g in 1..5 where n>g)(N[n,g]);
output([show(res)])
</syntaxhighlight>
{{out}}
<pre>
69
----------
</pre>
 
=={{header|Nim}}==
We use a generic definition for the square matrix type. The compiler insures that the matrix we provide is actually square.
 
<syntaxhighlight lang="nim">type SquareMatrix[T: SomeNumber; N: static Positive] = array[N, array[N, T]]
 
func sumBelowDiagonal[T, N](m: SquareMatrix[T, N]): T =
for i in 1..<N:
for j in 0..<i:
result += m[i][j]
 
const M = [[ 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]]
 
echo sumBelowDiagonal(M)</syntaxhighlight>
 
{{out}}
<pre>69</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
use warnings;
use List::Util qw( sum );
 
my $matrix =
[[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]];
 
my $lowersum = sum map @{ $matrix->[$_] }[0 .. $_ - 1], 1 .. $#$matrix;
print "lower sum = $lowersum\n";</syntaxhighlight>
{{out}}
<pre>
lower sum = 69
</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">M</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">16</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">14</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">18</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">11</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">14</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">17</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">18</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">20</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">}}</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">height</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">M</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">height</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">width</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">M</span><span style="color: #0000FF;">[</span><span style="color: #000000;">row</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">height</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"not square"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">col</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">M</span><span style="color: #0000FF;">[</span><span style="color: #000000;">row</span><span style="color: #0000FF;">][</span><span style="color: #000000;">col</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">res</span>
<!--</syntaxhighlight>-->
You could of course start row from 2 and get the same result, for row==1 the col loop iterates zero times.<br>
Without the checks for square M expect (when not square) wrong/partial answers for height<=width+1, and (still human readable) runtime crashes for height>width+1.
{{out}}
<pre>
69
</pre>
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
trap: procedure options (main); /* 17 December 2021 */
declare n fixed binary;
get (n);
put ('The order of the matrix is ' || trim(n));
begin;
declare A (n,n) fixed binary;
declare sum fixed binary;
declare (i, j) fixed binary;
 
get (A);
sum = 0;
do i = 2 to n;
do j = 1 to i-1;
sum = sum + a(i,j);
end;
end;
put edit (A) (skip, (n) f(4) );
put skip data (sum);
end;
end trap;
</syntaxhighlight>
{{out}}
<pre>
The order of the matrix is 5
 
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
SUM= 69;
</pre>
 
=={{header|PL/M}}==
This can be compiled with the original 8080 PL/M compiler and run under CP/M or an emulator/clone.
<syntaxhighlight lang="pli">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</syntaxhighlight>
{{out}}
<pre>
69
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">from numpy import array, tril, sum
 
A = [[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]]
 
print(sum(tril(A, -1))) # 69</syntaxhighlight>
 
 
Or, defining the lower triangle for ourselves:
 
<syntaxhighlight lang="python">'''Lower triangle of a matrix'''
 
from itertools import chain, islice
from functools import reduce
 
 
# lowerTriangle :: [[a]] -> None | [[a]]
def lowerTriangle(matrix):
'''Either None, if the matrix is not square, or
the rows of the matrix, each containing only
those values that form part of the lower triangle.
'''
def go(n_rows, xs):
n, rows = n_rows
return 1 + n, rows + [list(islice(xs, n))]
 
return reduce(
go,
matrix,
(0, [])
)[1] if isSquare(matrix) else None
 
 
# isSquare :: [[a]] -> Bool
def isSquare(matrix):
'''True if all rows of the matrix share
the length of the matrix itself.
'''
n = len(matrix)
return all([n == len(x) for x in matrix])
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Sum of integers in the lower triangle of a matrix.
'''
rows = lowerTriangle([
[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]
])
 
print(
"Not a square matrix." if None is rows else (
sum(chain(*rows))
)
)
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre>69</pre>
 
=={{header|R}}==
R has lots of native matrix support, so this is trivial.
<syntaxhighlight lang="rsplus">mat <- rbind(c(1,3,7,8,10),
c(2,4,16,14,4),
c(3,1,9,18,11),
c(12,14,17,18,20),
c(7,1,3,9,5))
print(sum(mat[lower.tri(mat)]))</syntaxhighlight>
{{out}}
<pre>[1] 69</pre>
 
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" line>sub lower-triangle-sum (@matrix) { sum flat (1..@matrix).map( { @matrix[^$_]»[^($_-1)] } )»[*-1] }
 
say lower-triangle-sum
[
[ 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 ]
];</syntaxhighlight>
{{out}}
<pre>69</pre>
 
=={{header|REXX}}==
=== version 1 ===
<lang rexx>/* REXX */
<syntaxhighlight lang="rexx">/* REXX */
ml ='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'
Do i=1 To 5
Line 53 ⟶ 1,462:
End
 
sumasum=0
Do i=2 To 5
Do j=1 To i-1
sumasum=sumasum+m.i.j
End
End
Say 'Sum abovebelow main diagonal:' sumasum</syntaxhighlight>
<pre>
 
sumb=0
Do i=1 To 4
Do j=i+1 To 5
sumb=sumb+m.i.j
End
End
Say 'Sum below main diagonal:' sumb </lang>
<pre>
1 3 7 8 10
2 4 16 14 4
Line 74 ⟶ 1,475:
12 14 17 18 20
7 1 3 9 5
Sum abovebelow main diagonal: 69 </pre>
 
Sum below main diagonal: 111</pre>
=== version 2 ===
This REXX version makes no assumption about the size of the matrix, &nbsp; and it determines the maximum width of any
<br>matrix element &nbsp; (instead of assuming a width that might not properly show the true value of an element).
<syntaxhighlight lang="rexx">/*REXX pgm finds & shows the sum of elements below the main diagonal of a square matrix.*/
$= '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'; #= words($)
do siz=1 while siz*siz<#; end /*determine the size of the matrix. */
w= 0 /*W: the maximum width any any element*/
do j=1 for #; parse var $ @..j $ /*obtain a number of the array (list). */
w= max(w, length(@..j)) /*examine each element for its width. */
end /*j*/ /* [↑] this is aligning matrix elements*/
s= 0; z= 0 /*initialize the sum [S] to zero. */
do r=1 for siz; _= left('', 12) /*_: contains a row of matrix elements*/
do c=1 for siz; z= z + 1; @.z= @..z /*get a number of the " " */
_= _ right(@.z, w) /*build a row of elements for display. */
if c<r then s= s + @.z /*add a "lower element" to the sum. */
end /*r*/
say _ /*display a row of the matrix to term. */
end /*c*/
say 'sum of elements below main diagonal is: ' s /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
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
sum of elements below main diagonal is: 69
</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Sum of elements below main diagonal of matrix:" + nl
Line 100 ⟶ 1,529:
see "" + sumDiag + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 107 ⟶ 1,536:
69
done...
</pre>
 
=={{header|RPL}}==
« → m
« 0 2 m SIZE 1 GET '''FOR''' r
1 r 1 - '''FOR''' c
m r c 2 →LIST GET +
'''NEXT NEXT'''
» » '<span style="color:blue">∑BELOW</span>' STO
 
[[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]] <span style="color:blue">∑BELOW</span>
{{out}}
<pre>
1: 69
</pre>
{{works with|HP|49/50 }}
« DUP SIZE OBJ→ DROP « > » LCXM
HADAMARD AXL ∑LIST ∑LIST
» '<span style="color:blue">∑BELOW</span>' STO
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">arr = [
[ 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]
]
p arr.each_with_index.sum {|row, x| row[0, x].sum}
</syntaxhighlight>
{{out}}
<pre>69
</pre>
 
=={{header|Scala}}==
<syntaxhighlight lang="Scala">
object Main {
def main(args: Array[String]): Unit = {
val matrix = Array(
Array(1, 3, 7, 8, 10),
Array(2, 4, 16, 14, 4),
Array(3, 1, 9, 18, 11),
Array(12, 14, 17, 18, 20),
Array(7, 1, 3, 9, 5)
)
var sum = 0
for (row <- 1 until matrix.length) {
for (col <- 0 until row) {
sum += matrix(row)(col)
}
}
println(sum)
}
}
</syntaxhighlight>
{{out}}
<pre>
69
</pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
local
var integer: sum is 0;
var integer: i is 0;
var integer: j is 0;
const array array integer: m is [] ([] ( 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));
begin
for i range 2 to length(m) do
for j range 1 to i - 1 do
sum +:= m[i][j];
end for;
end for;
writeln(sum);
end func;</syntaxhighlight>
 
{{out}}
<pre>
69
</pre>
 
=={{header|Wren}}==
<syntaxhighlight lang="wren">var m = [
[ 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]
]
if (m.count != m[0].count) Fiber.abort("Matrix must be square.")
var sum = 0
for (i in 1...m.count) {
for (j in 0...i) {
sum = sum + m[i][j]
}
}
System.print("Sum of elements below main diagonal is %(sum).")</syntaxhighlight>
 
{{out}}
<pre>
Sum of elements below main diagonal is 69.
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">int Mat, X, Y, Sum;
[Mat:= [[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]];
Sum:= 0;
for Y:= 0 to 4 do
for X:= 0 to 4 do
if Y > X then
Sum:= Sum + Mat(Y,X);
IntOut(0, Sum);
]</syntaxhighlight>
 
{{out}}
<pre>
69
</pre>
1,150

edits