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

→‎{{header|RPL}}: HP-49/50 version
(→‎{{header|RPL}}: HP-49/50 version)
 
(18 intermediate revisions by 11 users not shown)
Line 19:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F sumBelowDiagonal(m)
V result = 0
L(i) 1 .< m.len
Line 32:
[ 7, 1, 3, 9, 5]]
 
print(sumBelowDiagonal(m))</langsyntaxhighlight>
 
{{out}}
Line 40:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC PrintMatrix(INT ARRAY m BYTE size)
BYTE x,y
INT v
Line 84:
sum=SumBelowDiagonal(m,5)
PrintF("Sum below diagonal is %I",sum)
RETURN</langsyntaxhighlight>
{{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]
Line 99:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Numerics.Generic_Real_Arrays;
 
Line 137:
Put (Sum, Exp => 0, Aft => 1);
New_Line;
end Sum_Below_Diagonals;</langsyntaxhighlight>
{{out}}<pre>Sum below diagonal: 69.0</pre>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight 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 #
Line 171:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 179:
=={{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...
<langsyntaxhighlight 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 %
Line 203:
write( i_w := 1, lowerSum( m, 1, 5 ) )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 211:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight lang="apl">sum_below_diagonal ← +/(∊⊢×(>/¨⍳∘⍴))</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">matrx :=[[1,3,7,8,10]
,[2,4,16,14,4]
,[3,1,9,18,11]
Line 234 ⟶ 255:
. "`nsum below diagonal = " sumB
. "`nsum on diagonal = " sumD
. "`nsum all = " sumAll</langsyntaxhighlight>
{{out}}
<pre>sum above diagonal = 111
Line 242 ⟶ 263:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUM_OF_ELEMENTS_BELOW_MAIN_DIAGONAL_OF_MATRIX.AWK
BEGIN {
Line 279 ⟶ 300:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 291 ⟶ 312:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="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[?,]
Line 304 ⟶ 325:
 
print "Sum of elements below main diagonal of matrix is "; sumDiag
end</langsyntaxhighlight>
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">Dim As Integer diag(1 To 5, 1 To 5) = { _
{ 1, 3, 7, 8,10}, _
{ 2, 4,16,14, 4}, _
Line 324 ⟶ 345:
 
Print "Sum of elements below main diagonal of matrix is"; sumDiag
Sleep</langsyntaxhighlight>
{{out}}
<pre>Sum of elements below main diagonal of matrix is 69</pre>
 
==={{header|GW-BASIC}}===
<langsyntaxhighlight lang="gwbasic">10 DATA 1,3,7,8,10
20 DATA 2,4,16,14,4
30 DATA 3,1,9,18,11
Line 340 ⟶ 361:
100 NEXT COL
110 NEXT ROW
120 PRINT SUM</langsyntaxhighlight>
{{out}}<pre>69</pre>
==={{header|PL/I}}===
<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;
</lang>
{{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|QBasic}}===
{{works with|QBasic}}
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="qbasic">DEFINT A-Z
 
DIM diag(1 TO 5, 1 TO 5)
Line 408 ⟶ 394:
DATA 3, 1, 9,18,11
DATA 12,14,17,18,20
DATA 7, 1, 3, 9, 5</langsyntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="qbasic">DIM diag(5, 5)
LET lenDiag = UBOUND(diag, 1)
LET ind = lenDiag
Line 437 ⟶ 423:
 
PRINT "Sum of elements below main diagonal of matrix:"; sumDiag
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">dim diag(5, 5)
lenDiag = arraysize(diag(),1)
ind = lenDiag
Line 466 ⟶ 452:
data 3, 1, 9,18,11
data 12,14,17,18,20
data 7, 1, 3, 9, 5</langsyntaxhighlight>
 
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">SumBelowDiagonal ← +´∘⥊⊢×(>⌜´)∘(↕¨≢)
 
matrix ← >⟨⟨ 1, 3, 7, 8,10⟩,
Line 478 ⟶ 464:
⟨ 7, 1, 3, 9, 5⟩⟩
 
SumBelowDiagonal matrix</langsyntaxhighlight>
{{out}}
<pre>69</pre>
Line 484 ⟶ 470:
=={{header|C}}==
Interactive program which reads the matrix from a file :
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 548 ⟶ 534:
return 0;
}
</syntaxhighlight>
</lang>
 
Input Data file, first row specifies rows and columns :
Line 578 ⟶ 564:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 601 ⟶ 587:
std::cout << sum_below_diagonal(matrix) << std::endl;
return 0;
}</langsyntaxhighlight>
{{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}}==
Line 613 ⟶ 663:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">=LAMBDA(isUpper,
LAMBDA(matrix,
LET(
Line 637 ⟶ 687:
)
)
)</langsyntaxhighlight>
 
{{Out}}
Line 818 ⟶ 868:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Sum below leading diagnal. Nigel Galloway: July 21st., 2021
let _,n=[[ 1; 3; 7; 8;10];
Line 825 ⟶ 875:
[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>
</lang>
{{out}}
<pre>
Line 833 ⟶ 883:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: kernel math math.matrices prettyprint sequences ;
 
: sum-below-diagonal ( matrix -- sum )
Line 845 ⟶ 895:
{ 12 14 17 18 20 }
{ 7 1 3 9 5 }
} sum-below-diagonal .</langsyntaxhighlight>
{{out}}
<pre>
Line 852 ⟶ 902:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 877 ⟶ 927:
}
fmt.Println("Sum of elements below main diagonal is", sum)
}</langsyntaxhighlight>
 
{{out}}
Line 887 ⟶ 937:
Defining both upper and lower triangle of a square matrix:
 
<langsyntaxhighlight lang="haskell">----------------- UPPER OR LOWER TRIANGLE ----------------
 
matrixTriangle :: Bool -> [[a]] -> Either String [[a]]
Line 925 ⟶ 975:
]
)
["Lower", "Upper"]</langsyntaxhighlight>
{{Out}}
<pre>Lower triangle:
Line 933 ⟶ 983:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">sum_below_diagonal =: [:+/@,[*>/~@i.@#</langsyntaxhighlight>
{{out}}
<pre> mat
Line 943 ⟶ 993:
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}}==
Line 948 ⟶ 1,016:
Defining the lower triangle of a square matrix.
 
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 1,028 ⟶ 1,096:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>69</pre>
Line 1,035 ⟶ 1,103:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
<lang jq>
def add(s): reduce s as $x (null; . + $x);
 
Line 1,041 ⟶ 1,109:
def sum_below_diagonal:
add( range(0;length) as $i | .[$i][:$i][] ) ;
</syntaxhighlight>
</lang>
The task:
<langsyntaxhighlight 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</langsyntaxhighlight>
{{out}}
<pre>
Line 1,059 ⟶ 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 1,072 ⟶ 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]
Line 1,079 ⟶ 1,147:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="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]</langsyntaxhighlight>
{{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">
<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>
</lang>
{{out}}
<pre>
Line 1,100 ⟶ 1,187:
We use a generic definition for the square matrix type. The compiler insures that the matrix we provide is actually square.
 
<langsyntaxhighlight Nimlang="nim">type SquareMatrix[T: SomeNumber; N: static Positive] = array[N, array[N, T]]
 
func sumBelowDiagonal[T, N](m: SquareMatrix[T, N]): T =
Line 1,113 ⟶ 1,200:
[ 7, 1, 3, 9, 5]]
 
echo sumBelowDiagonal(M)</langsyntaxhighlight>
 
{{out}}
Line 1,119 ⟶ 1,206:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 1,133 ⟶ 1,220:
 
my $lowersum = sum map @{ $matrix->[$_] }[0 .. $_ - 1], 1 .. $#$matrix;
print "lower sum = $lowersum\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 1,140 ⟶ 1,227:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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>
Line 1,156 ⟶ 1,243:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">res</span>
<!--</langsyntaxhighlight>-->
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.
Line 1,163 ⟶ 1,250:
69
</pre>
==={{header|PL/I}}===
<syntaxhighlight lang="pl/i">
<lang PL/I>
trap: procedure options (main); /* 17 December 2021 */
declare n fixed binary;
Line 1,185 ⟶ 1,272:
end;
end trap;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,200 ⟶ 1,287:
=={{header|PL/M}}==
This can be compiled with the original 8080 PL/M compiler and run under CP/M or an emulator/clone.
<langsyntaxhighlight lang="pli">100H: /* SUM THE ELEMENTS BELOW THE MAIN DIAGONAL OF A MATRIX */
 
/* CP/M BDOS SYSTEM CALL, IGNORE THE RETURN VALUE */
Line 1,253 ⟶ 1,340:
CALL PR$NUMBER( LOWER$SUM( .T, 5 ) );
 
EOF</langsyntaxhighlight>
{{out}}
<pre>
69
</pre>
 
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from numpy import array, tril, sum
 
A = [[1,3,7,8,10],
Line 1,269 ⟶ 1,355:
[7,1,3,9,5]]
 
print(sum(tril(A, -1))) # 69</langsyntaxhighlight>
 
 
Or, defining the lower triangle for ourselves:
 
<langsyntaxhighlight lang="python">'''Lower triangle of a matrix'''
 
from itertools import chain, islice
Line 1,327 ⟶ 1,413:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>69</pre>
Line 1,333 ⟶ 1,419:
=={{header|R}}==
R has lots of native matrix support, so this is trivial.
<langsyntaxhighlight Rlang="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)]))</langsyntaxhighlight>
{{out}}
<pre>[1] 69</pre>
Line 1,344 ⟶ 1,430:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>sub lower-triangle-sum (@matrix) { sum flat (1..@matrix).map( { @matrix[^$_]»[^($_-1)] } )»[*-1] }
 
say lower-triangle-sum
Line 1,353 ⟶ 1,439:
[ 12, 14, 17, 18, 20 ],
[ 7, 1, 3, 9, 5 ]
];</langsyntaxhighlight>
{{out}}
<pre>69</pre>
Line 1,359 ⟶ 1,445:
=={{header|REXX}}==
=== version 1 ===
<langsyntaxhighlight 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 1,382 ⟶ 1,468:
End
End
Say 'Sum below main diagonal:' sum</langsyntaxhighlight>
<pre>
1 3 7 8 10
Line 1,394 ⟶ 1,480:
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).
<langsyntaxhighlight 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. */
Line 1,409 ⟶ 1,495:
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. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 1,421 ⟶ 1,507:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Sum of elements below main diagonal of matrix:" + nl
Line 1,443 ⟶ 1,529:
see "" + sumDiag + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,451 ⟶ 1,537:
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}}==
<langsyntaxhighlight lang="ruby">arr = [
[ 1, 3, 7, 8, 10],
[ 2, 4, 16, 14, 4],
Line 1,461 ⟶ 1,569:
]
p arr.each_with_index.sum {|row, x| row[0, x].sum}
</syntaxhighlight>
</lang>
{{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}}==
<langsyntaxhighlight ecmascriptlang="wren">var m = [
[ 1, 3, 7, 8, 10],
[ 2, 4, 16, 14, 4],
Line 1,480 ⟶ 1,642:
}
}
System.print("Sum of elements below main diagonal is %(sum).")</langsyntaxhighlight>
 
{{out}}
Line 1,488 ⟶ 1,650:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int Mat, X, Y, Sum;
[Mat:= [[1,3,7,8,10],
[2,4,16,14,4],
Line 1,500 ⟶ 1,662:
Sum:= Sum + Mat(Y,X);
IntOut(0, Sum);
]</langsyntaxhighlight>
 
{{out}}
1,150

edits