SRL Archive - Development

MinArr + MaxArr


Function

2010-02-09 19:04:12

Hey! I don't really know where to post this, so I will post it here. Anyways while developing my script I came across and little speedbump where I needed to know which, of three variables, was the smallest. So I could call Min although that limits you to 2 variables. Thus MinArr + MaxArr was born.

function MaxArr(iArr: TIntegerArray): integer;
var
i: integer;
begin
for i := 0 to high(iArr) do
if(iArr[i] > result)then
result := iArr[i];
end;
function MinArr(iArr: TIntegerArray): integer;
var
i: integer;
begin
result := MaxArr(iArr);
for i := 0 to high(iArr) do
if(iArr[i] < result)then
result := iArr[i];
end;

I don't know if this could be helpful to anyone else but personally I think it could replace, the already implemented, "Min" and "Max" functions.

Just give me some feedback, thanks. :)

mixster

2010-02-09 19:47:01

function MinArr(iArr: TIntegerArray): integer;
var
i: integer;
begin
if High(iArr) = -1 then // if it's empty
exit; // exit so we don't get an invalid index next

result := iArr[0];
for i := 1 to high(iArr) do
if(iArr[i] < result)then
result := iArr[i];
end;

Just a little idea for speeding up MinArr. May be worth making it return -1 if it's an empty array or something like that so people can use it as a check. Alternatively, you could always set result to the highest integer possible, thus any number would be smaller or equal to, making it work (2^31 - 1 should be the number I believe)

Function

2010-02-09 20:02:27

From: mixster
function MinArr(iArr: TIntegerArray): integer;
var
i: integer;
begin
if High(iArr) = -1 then // if it's empty
exit; // exit so we don't get an invalid index next

result := iArr[0];
for i := 1 to high(iArr) do
if(iArr[i] < result)then
result := iArr[i];
end;

Just a little idea for speeding up MinArr. May be worth making it return -1 if it's an empty array or something like that so people can use it as a check. Alternatively, you could always set result to the highest integer possible, thus any number would be smaller or equal to, making it work (2^31 - 1 should be the number I believe)


Thanks. And yeah that looks good. But I don't see where your coming from to see if an array has values? Maybe that is something more with advanced scripting, idk. Also I thought about setting the result to the highest integer, like you did, but I had it run through my other function MaxArray to get the highest number. Unless it takes less time to set it to the highest integer. Again thanks for the feedback :)

Sex

2010-02-09 20:37:13

function MaxArr(iArr : TIntegerArray) : integer;
begin
if GetArrayLength(iArr) = 0 then
exit;
QuickSort(iArr);
result := iArr[High(iArr)];
end;

function MinArr(iArr : TIntegerArray) : integer;
begin
if GetArrayLength(iArr) = 0 then
exit;
QuickSort(iArr);
result := iArr[0];
end;
I'm so tired right now but that works in SCAR. I don't know if Simba has QuickSort or not though.

Smartzkid

2010-02-09 20:44:40

function MaxArr(iArr: TIntegerArray): integer;
var
i: integer;
begin
result := iArr[0];
for i := 1 to high(iArr) do
if(iArr[i] > result)then
result := iArr[i];
end;

Did the same thing as mixter did to MinArr. Old code would fail if the whole array was negative.

Nava2

2010-02-09 22:18:29

Isn't this already in freepascal?

Gu3st...

2010-02-10 04:49:05

function MaxArr(iArr : TIntegerArray): Integer;
var
I, H, L : Integer;
begin
Result := -1;
if GetArrayLength(iArr) = 0 then exit;
L := Low(iArr);
H := High(iArr);
for I := L to H do
if(iArr[I] > result)then
Result := iArr[I];
end;
Even faster, as you wouldn't have to use High() over and over and over..

Wizzup?

2010-02-10 06:17:56

From: Nava2
Isn't this already in freepascal?


I know QuickSort is, but I'm not too sure about the MinArr/MaxArr.

Nava2

2010-02-10 07:57:42

I thought that Min() and Max() took arrays.. or at least an infinitum of params.

Gu3st...

2010-02-10 08:02:16

function Max(a, b: Integer): Integer;
Returns biggest of two integer values.

function Min(a, b: Integer): Integer;
Returns smallest of two integer values.

From SCAR manual

mastaraymond

2010-02-10 10:10:03

From: Wizzup?
I know QuickSort is, but I'm not too sure about the MinArr/MaxArr.

^ it's not.. It's only implemented in the TList class, but that doesn't come in handy when using arrays.

Wizzup?

2010-02-10 10:37:39

From: mastaraymond
^ it's not.. It's only implemented in the TList class, but that doesn't come in handy when using arrays.


That's weird. We could make one with a callback, that passes a pointer to the two items that are to be compared?

mixster

2010-02-10 10:53:03

From: Function
Thanks. And yeah that looks good. But I don't see where your coming from to see if an array has values? Maybe that is something more with advanced scripting, idk. Also I thought about setting the result to the highest integer, like you did, but I had it run through my other function MaxArray to get the highest number. Unless it takes less time to set it to the highest integer. Again thanks for the feedback :)


If the array is empty, like if you did myArr := []; or SetLength(myarr, 0), then it would try to access the first index, myarr[0], which wouldn't exist and throw an error.