Task

Write a program to find the sum of squares of a numeric vector.

The program should work on a zero-length vector (with an answer of   0).



Related task

   Mean


MODULE SumSquares EXPORTS Main;
 
IMPORT IO, Fmt;
 
TYPE RealArray = ARRAY OF REAL;
 
PROCEDURE SumOfSquares(x: RealArray): REAL =
  VAR sum := 0.0;
  BEGIN
    FOR i := FIRST(x) TO LAST(x) DO
      sum := sum + x[i] * x[i];
    END;
    RETURN sum;
  END SumOfSquares;
 
BEGIN
  IO.Put(Fmt.Real(SumOfSquares(RealArray{3.0, 1.0, 4.0, 1.0, 5.0, 9.0})));
  IO.Put("\n");
END SumSquares.