function max (
ParField: tField;
lowerBound, upperBound: tIndex): integer;
var
Value : integer;
i : tIndex;
begin
Value := ParField[lowerBound];
for i := lowerBound + 1 to upperBound do
if ParField[i] > Value then
Value := ParField[i];
max := Value
end; { max }
This function walks through a list of integers starting at and up to a given index and determines the largest number within that range.
Given the the following definitions...
const
BOUNDARY = 10;
type
tIndex = 1..BOUNDARY;
tField = array [tIndex] of integer;
var
Field : tField;
w,
w1,
w2 : integer;
...which of the following code snippets (calling the function) will not violate the lists boundaries, regardless the lists actual integer values. For example, a violation would occur when trying to access the lists 12th element, as the list by definition only consists of ten slots.
[A]
The point to note here is "regardless the actual list values". The value of Field[1] can be anything, 1, 10, 1000...
Any value that is not within the range of 1..10 will cause a violation.
[B] w := max (Field, (BOUNDARY-1) div 2, (BOUNDARY-1) div 2);
The lower an upper bound are related to BOUNDARY and not to actual list values.
Accessing a list element at (BOUNDARY-1) div 2, which is 4, will do no harm.
[C] if max (Field, 1, (BOUNDARY-1) div 2) >
max (Field, (BOUNDARY+1) div 2, BOUNDARY) then
w := max (Field, 1, (BOUNDARY-1) div 2)
else
w := max (Field, (BOUNDARY +1) div 2, BOUNDARY);
max (Field, 1, (BOUNDARY-1) div 2) does not violate the lists boundaries.
Neither does max (Field, (BOUNDARY+1) div 2, BOUNDARY).
Regardless of the actual outcome of the > - comparison either
w := max (Field, 1, (BOUNDARY-1) div 2)
or
w := max (Field, (BOUNDARY +1) div 2, BOUNDARY)
will be executed.
As both function calls do not violate the lists boundaries, this option is valid.
[D]
The upper bound is not an issue in this case but the lower bound is. In case the value of w is less than 1, the lists lower boundary is violated.
[E] w1 := max (Field, 1, GRENZE);
w2 := max (Field, 4, GRENZE-1);
if (0 < w2) and (w1 <= GRENZE) then
begin
w := max (Field, 2, GRENZE);
w := max (Field, 1, w)
end;
This one is OK as well.
w1 can be of any value. w2 as well.
But for the conditional statement to be true, w2 has to be greater than 0 and w1 has to be less than or equal to 10.
There is no way that max (Field, 2, GRENZE); results in a value w that violates the lists boundaries in the following statement w := max (Field, 1, w).
w1 is the lists largest integer (position 1 to 10).
w (position 2 to 10) can only be equal or less than w1.

Keine Kommentare:
Kommentar veröffentlichen