Jump to content

Sum of squares: Difference between revisions

→‎{{header|ALGOL W}}: Added an alternative solution using Jensen's Device
(Easylang)
(→‎{{header|ALGOL W}}: Added an alternative solution using Jensen's Device)
Line 357:
 
=={{header|ALGOL W}}==
 
===Using a dedicated "sum of squares" procedure===
 
<syntaxhighlight lang="algolw">begin
% procedure to sum the squares of the elements of a vector. As the procedure can't find %
% the bounds of the arrayvector formust itself,be we pass thempassed in lb and ub %
real procedure sumSquares ( real array vector ( * )
; integer value lb
Line 377 ⟶ 380:
write( sumSquares( numbers, 1, 5 ) );
end.</syntaxhighlight>
 
===Using Jensen's device===
 
Using the classic [[Jensen's Device]] (first introduced in Algol 60) we can use a generic summation procedure, as in this sample:
 
<syntaxhighlight lang="algolw">
begin % sum the squares of the elements of a vector, using Jensen's Device %
integer i;
real procedure sum ( integer %name% i; integer value lo, hi; real procedure term );
% i is passed by-name, term is passed as a procedure which makes it effectively passed by-name %
begin
real temp;
temp := 0;
i := lo;
while i <= hi do begin % The Algol W "for" loop (as in Algol 68) creates a distinct %
temp := temp + term; % variable which would not be shared with the passed "i" %
i := i + 1 % Here the actual passed "i" is incremented. %
end while_i_le_temp;
temp
end;
real array A ( 1 :: 5 );
for i := 1 until 5 do A( i ) := i;
r_format := "A"; r_w := 10; r_d := 1; % set fixed point output %
write( sum( i, 1, 5, A( i ) * A( i ) ) );
end.
</syntaxhighlight>
 
=={{header|Alore}}==
3,044

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.