How to resolve the algorithm Averages/Arithmetic mean step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Arithmetic mean step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

Write a program to find the mean (arithmetic average) of a numeric vector. In case of a zero-length input, since the mean of an empty set of numbers is ill-defined, the program may choose to behave in any way it deems appropriate, though if the programming language has an established convention for conveying math errors or undefined values, it's preferable to follow it.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Averages/Arithmetic mean step by step in the FreeBASIC programming language

Source code in the freebasic programming language

' 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

  

You may also check:How to resolve the algorithm Test a function step by step in the Python programming language
You may also check:How to resolve the algorithm Password generator step by step in the Clojure programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the BASIC programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the Sidef programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the Nim programming language