Home

Statistics Functions:
The Sums and Squares of a Series

 
void __fastcall SumsAndSquares(const double * Data,
                               const int Data_Size,
                               Extended &Sum,
                               Extended &SumOfSquares);

The SumsAndSquares() function performs two operations and returns two values. Using a group of numbers, the function calculates their total, Sum. It also calculates the square S of each number then calculates the total of the S values, which produces a SumOfSquares. The first argument, Data, is the array of the numbers considered. The Data_Size argument is the number of items in the array minus 1. The Sum and the SumOfSquares arguments are passed by reference, which allows the function to return these last two values.

Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	Extended Total, TotalOfSquares;
	int Size;
	double Values[] = { 32.12, 208.45, 14.80, 95.25,
			    30.32, 102.55, 88.20, 100.05 };

	Size = (sizeof(Values)/sizeof(double)) - 1;
	SumsAndSquares(Values, Size, Total, TotalOfSquares);

	Edit1->Text = FloatToStr(Total);
	Edit2->Text = FloatToStr(TotalOfSquares);
}
//---------------------------------------------------------------------------
 
Home Copyright 2004-2007 FunctionX, Inc.