Averages/Arithmetic mean: Difference between revisions

(task description: Improve formatting and add related tasks box)
Line 1,006:
colmeans = sum(b,2)/max(size(b,1),1) ! SUM elements in each column (dimension 2)
! dividing by the length of the column, which is the number of rows (SIZE of dimension 1)</lang>
 
=={{header|FreeBASIC}}==
<lang freebasic>
' FB 1.05.0 Win64
 
Function Mean(array() As Double) As Double
Dim length As Integer = Ubound(array) - Lbound(array) + 1
If length = 0 Then
Return 0.0/0.0 'NaN
End If
Dim As Double sum = 0.0
For i As Integer = LBound(array) To UBound(array)
sum += array(i)
Next
Return sum/length
End Function
 
Function IsNaN(number As Double) As Boolean
Return Str(number) = "-1.#IND" ' NaN as a string in FB
End Function
 
Dim As Integer n, i
Dim As Double num
Print "Sample input and output"
Print
Do
Input "How many numbers are to be input ? : ", n
Loop Until n > 0
Dim vector(1 To N) As Double
Print
For i = 1 to n
Print " Number #"; i; " : ";
Input "", vector(i)
Next
Print
Print "Mean is"; Mean(vector())
Print
Erase vector
num = Mean(vector())
If IsNaN(num) Then
Print "After clearing the vector, the mean is 'NaN'"
End If
Print
Print "Press any key to quit the program"
Sleep
</lang>
 
{{out}}
<pre>
Sample input and output
 
How many numbers are to be input ? : 6
 
Number # 1 : 12
Number # 2 : 18
Number # 3 : 5.6
Number # 4 : 6
Number # 5 : 23
Number # 6 : 17
 
Mean is 13.6
 
After clearing the vector, the mean is 'NaN'
</pre>
 
=={{header|Frink}}==
9,492

edits