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


'   [RC] Sum of Squares
 
    SourceList$     ="3 1 4 1 5 9"
    'SourceList$     =""
 
    '   If saved as an array we'd have to have a flag for last data.
    '   LB has the very useful word$() to read from delimited strings.
    '   The default delimiter is a space character, " ".
 
    SumOfSquares    =0
    n               =0
    data$           ="666"  '   temporary dummy to enter the loop.
 
    while data$ <>""                                '   we loop until no data left.
        data$           =word$( SourceList$, n +1)  '   first data, as a string
        NewVal          =val( data$)                '   convert string to number
        SumOfSquares    =SumOfSquares +NewVal^2     '   add to existing sum of squares
        n =n +1                                     '   increment number of data items found
    wend
 
    n =n -1
 
    print "Supplied data was ";         SourceList$
    print "This contained ";            n; " numbers."
    print "Sum of squares is ";         SumOfSquares
 
    end