SmartPascal
Mean
Function
Gives the average for a set of numbers Math unit
 function Mean ( const DataArray : array of Double ) : Extended;
Description
The Mean function returns the average of a set of Double values in a DataArray.
Related commands
Max Gives the maximum of two integer values
Min Gives the minimum of two integer values
CompareValue Compare numeric values with a tolerance
 
Example code : Calculate the mean of a set of 5 numbers
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  Math,   // Unit containing the Mean command
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  numbers         : array[1..5] of Double;
  aveVal, meanVal : Double;

begin
  // Set up the array of 5 floating point numbers
  numbers[1] :=  1.0;
  numbers[2] :=  2.5;
  numbers[3] :=  3.0;
  numbers[4] :=  4.5;
  numbers[5] := 25.0;

  // Calculate the average of these numbers
  aveVal := (numbers[1] + numbers[2] + numbers[3] +
             numbers[4] + numbers[5]) / 5;

  // Calculate the mean of these numbers
  meanVal := Mean(numbers);

  // Show these values
  ShowMessage('Average = '+FloatToStr(aveVal));
  ShowMessage('Mean    = '+FloatToStr(meanVal));
end;
 
end.
Hide full unit code
   Average = 7.2
   Mean    = 7.2