Jump to content

Averages/Arithmetic mean: Difference between revisions

Merge RPL/2 and RPL, CLEAR added to first implementation to make it always work
(→‎Joy: add)
(Merge RPL/2 and RPL, CLEAR added to first implementation to make it always work)
Line 3,220:
 
=={{header|RPL}}==
This is a simple rewrite of the dc version above. This works on an HP 48. "→" is a single right arrow character on the 48. Feel free to alter this code as necessary to work on RPL/2.
{{works with|Halcyon Calc|4.2.7}}
 
<syntaxhighlight lang="rpl/2">1 2 3 5 7
AMEAN
<< CLEAR DEPTH DUP 'N' STO →LIST ΣLIST N / >>
3.6</syntaxhighlight>
 
===Hard-working approach===
Works for all RPL versions.
≪ DUP SIZE SWAP OVER
0 1 ROT '''FOR''' j
OVER j GET + '''NEXT'''
NEXT
ROT / SWAP DROP
Line 3,231 ⟶ 3,237:
No significant impact on program size or speed, but much more readable
≪ DUP SIZE → vector n
≪ 0 1 n '''FOR''' j
vector j GET + '''NEXT'''
NEXT
n /
≫ ≫
Line 3,239 ⟶ 3,244:
The dot product of any vector with [1 1 ... 1] gives the sum of its elements.
≪ SIZE LAST DUP 1 CON DOT SWAP / ≫
''''AMEAN'''' STO
 
===Using built-in statistics features===
Most of the code is dedicated to store the input array according to built-in statistics requirements, which requires a matrix with one line per record. Main benefit of this approach is that you can then easily calculate standard deviation and variance by calling resp. <code>SDEV</code> and <code>VAR</code> functions.
≪ { 1 } OVER SIZE + RDM TRN '∑DAT' STO MEAN ≫ ''''AMEAN'''' STO
'AMEAN' STO
 
[ 1 5 0 -4 6 ] '''AMEAN'''
{{out}}
<pre>
1: 1.6
</pre>
 
=={{header|RPL/2}}==
 
This is a simple rewrite of the dc version above. This works on an HP 48. "→" is a single right arrow character on the 48. Feel free to alter this code as necessary to work on RPL/2.
 
<syntaxhighlight lang="rpl/2">1 2 3 5 7
AMEAN
<< DEPTH DUP 'N' STO →LIST ΣLIST N / >>
3.6</syntaxhighlight>
 
=={{header|Ruby}}==
1,150

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.