Centroid of a set of N-dimensional points: Difference between revisions

Added FreeBasic
(Added FreeBasic)
Line 236:
{{0, 0, 0, 0, 1}, {0, 0, 0, 1, 0}, {0, 0, 1, 0, 0}, {0, 1, 0, 0, 0}} => Centroid: {0, 0.25, 0.25, 0.25, 0.25}
</pre>
 
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<syntaxhighlight lang="vb">Sub Centroid(n As Ubyte, d As Ubyte, pts() As Single)
Dim As Ubyte i, j
Dim As Single ctr(d)
For j = 0 To d-1
ctr(j) = 0
For i = 0 To n-1
ctr(j)+ = pts(i,j)
Next
ctr(j) /= n
Next
Print "{";
For i = 0 To n-1
Print "{";
For j = 0 To d-1
Print Using "&"; pts(i,j);
If j < d-1 Then Print ", ";
Next
Print "}";
If i < n-1 Then Print ", ";
Next
Print "} => Centroid: {";
For j = 0 To d-1
Print Using "&"; ctr(j);
If j < d-1 Then Print ", ";
Next
Print "}"
End Sub
 
Dim pts1(2, 1) As Single = {{1}, {2}, {3}}
Dim pts2(1, 1) As Single = {{8, 2}, {0, 0}}
Dim pts3(1, 2) As Single = {{5, 5, 0}, {10, 10, 0}}
Dim pts4(3, 2) As Single = {{1, 3.1, 6.5}, {-2, -5, 3.4}, {-7, -4, 9}, {2, 0, 3}}
Dim pts5(3, 4) As Single = {{0, 0, 0, 0, 1}, _
{0, 0, 0, 1, 0}, {0, 0, 1, 0, 0}, {0, 1, 0, 0, 0}}
 
Centroid(3, 1, pts1())
Centroid(2, 2, pts2())
Centroid(2, 3, pts3())
Centroid(4, 3, pts4())
Centroid(4, 5, pts5())
 
Sleep</syntaxhighlight>
{{out}}
<pre>{{1}, {2}, {3}} => Centroid: {2}
{{8, 2}, {0, 0}} => Centroid: {4, 1}
{{5, 5, 0}, {10, 10, 0}} => Centroid: {7.5, 7.5, 0}
{{1, 3.1, 6.5}, {-2, -5, 3.4}, {-7, -4, 9}, {2, 0, 3}} => Centroid: {-1.5, -1.475, 5.475}
{{0, 0, 0, 0, 1}, {0, 0, 0, 1, 0}, {0, 0, 1, 0, 0}, {0, 1, 0, 0, 0}} => Centroid: {0, 0.25, 0.25, 0.25, 0.25}</pre>
 
=={{header|Go}}==
2,157

edits