Logo

Programming-Idioms

History of Idiom 195 > diff from v2 to v3

Edit summary for version 3 by Bart:
New Pascal implementation by user [Bart]

Version 2

2019-09-28, 16:25:11

Version 3

2019-09-29, 09:08:57

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Code
procedure foo(a: array of double);
var
  sum: double;
  i: Integer;
begin
  for i := low(a) to high(a) do
    sum := sum + (i+1)*a[i];
  writeln('Size = ',Length(a),', Sum = ',sum:6:5);
end;

...
  foo(a);
...
Comments bubble
dynamic arrays start at index zero, so multiply by the index+1 for the sake of this example.
The :6:5 formats the output of the floating point varaible sum.