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

→‎{{header|RPL}}: HP-49/50 version
m (syntax highlighting fixup automation)
(→‎{{header|RPL}}: HP-49/50 version)
(8 intermediate revisions by 6 users not shown)
Line 216:
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}}==
Line 569 ⟶ 590:
{{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 1,066 ⟶ 1,151:
{{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}}==
Line 1,433 ⟶ 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}}==
Line 1,446 ⟶ 1,572:
{{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>
 
Line 1,476 ⟶ 1,628:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var m = [
[ 1, 3, 7, 8, 10],
[ 2, 4, 16, 14, 4],
1,150

edits