Logo

Programming-Idioms

History of Idiom 195 > diff from v3 to v4

Edit summary for version 4 by tkoenig:
[Pascal] This is not two-dimensional

Version 3

2019-09-29, 09:08:57

Version 4

2019-09-29, 09:17:20

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);
...
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.
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.

However, this does not seem to be a two-dimensional array.