: Hello! I have two dimensional array - 6 rows,6 columns. I need to switch the biggest number (which is over the main diagonal) with the least number (which is under the main diagonal). It looks like this:
:
: 1 2 3 4 5 6
: 7 8 9 10 11 12
: 13 14 15 16 17 18
: 19 20 21 22 23 24
: 25 26 27 28 29 30
: 31 32 33 34 35 36
:
: so, i need to switch 30 with 7. Any ideas?
:
First you'll have to find the location of those numbers. This involves two for-do loops and an if-then. Here is an untested example of finding the biggest number above the main dia.:
var
BigX, BigY, X, Y: integer;
begin
BigX := 2; { start with the top-left most number as begin value for the max: 2 in the example }
BigY := 1;
for Y := 1 to 6 do
for X := Y to 6 do begin { iterate through top-right half, inc. main dia. }
if (X <> Y) then begin { Remove main dia. }
if Numbers[X, Y] > Numbers[BigX, BigY] then begin
BigX := X; { Check for a larger number }
BigY := Y;
end;
end;
end;
end;
A similar code can be constructed for the smallest number below the diagonal. Switching the numbers shouldn't be too difficult, but here is the code anyway:
Temp := Numbers[BigX, BigY];
Numbers[BigX, BigY] := Numbers[SmallX, SmallY];
Numbers[SmallX, SmallY] := Temp;