View Full Version : Max and Min
raimis89
01-19-2007, 02:01 PM
How should i use Function Max and Min with the array ?
Or maybe there are dirfrent functions to get that?
Boreas
01-19-2007, 04:09 PM
Do you want to find the biggest number in an array?
Like this?
program New;
{.include SRL/SRL.scar}
var
test:array [1..5] of integer;
function BiggestOfArray(TheArray:array of integer; TheStart,TheEnd:integer):integer;
var i:integer;
begin
i:=TheStart-1;
repeat
i:=i+1;
result:=max(result,test[i]);
until i=TheEnd;
end;
begin
SetupSRL;
test[1]:=2;
test[2]:=5;
test[3]:=6;
test[4]:=7;
test[5]:=1;
writeln(inttostr(BiggestOfArray(test,1,5)));
end.
raimis89
01-19-2007, 04:45 PM
Yes this works for me
Tnx Boreas
Boreas
01-19-2007, 08:07 PM
Np, for min, change the max to min, and put before the repeat loop result:=SomethingYouKnowIsBiggerThanTheSmallestInT heArray (hint: use the biggest function first)
This is because there is an invisible result:=0; in there, which is definitely lower than the biggest in the array, so for the reverse you need something higher than the lowest in the array.
raimis89
01-20-2007, 01:34 AM
yeah already noticed that its showing zero when using min
Boreas
01-20-2007, 01:42 AM
Yea this works
function LowestOfArray(TheArray:array of integer; TheStart,TheEnd:integer):integer;
var i:integer;
begin
i:=TheStart-1;
repeat
i:=i+1;
result:=max(result,test[i]);
until i=TheEnd;
i:=TheStart-1;
repeat
i:=i+1;
result:=min(result,test[i]);
until i=TheEnd;
end;
Edit: I'll write a sorting function when I get back that returns the array with the first being the lowest, and last being the highest.
raimis89
01-20-2007, 02:26 AM
It is not really necessary of course if u dont want to test your ability to write scripts ;D
Boreas
01-20-2007, 05:03 AM
Too late lol. Really convoluted but it works
program New;
{.include srl\srl.scar}
var
barray:array [0..5] of integer;
c:integer;
newarray:array of integer;
function SortArray(TheArray:array of integer; TheStart,TheEnd:integer):array of integer;
var i,test,fff,len,ending,swapit:integer;
blah:array of integer;
begin
len:=getarraylength(TheArray);
setarraylength(blah,TheEnd+1);
ending:=TheEnd;
blah:=TheArray;
//writeln('array length of blah is '+inttostr(getarraylength(blah)));
// writeln('array length of thearray is '+inttostr(getarraylength(thearray)));
// fff:=thestart-1;
// repeat
// fff:=fff+1;
/// writeln(inttostr(fff)+' '+inttostr(blah[fff]));
// until fff=theend;
repeat
// writeln('ending is '+inttostr(ending));
i:=TheStart-1;
repeat
i:=i+1;
// writeln('i is '+inttostr(i));
test:=max(test,blah[i]);
until i=ending;
i:=TheStart-1;
repeat
i:=i+1;
test:=min(test,blah[i]);
until i=ending;
i:=TheStart-1;
repeat
i:=i+1;
test:=max(test,blah[i]);
until i=ending;
swapit:=blah[ending];
blah[ending]:=test;
i:=TheStart-1;
repeat
i:=i+1;
until blah[i]=test;
blah[i]:=swapit;
ending:=ending-1;
until ending=(TheStart);
result:=blah;
end;
begin
setupsrl;
barray[1]:=9;
barray[2]:=8;
barray[3]:=6;
barray[4]:=7;
barray[5]:=5;
newarray:=SortArray(barray,1,5);
repeat
c:=c+1;
writeln(inttostr(c)+' '+inttostr(newarray[c]));
until c=5;
writeln('biggest is '+inttostr(newarray[5]));
writeln('smallest is '+inttostr(newarray[1]));
end.
raimis89
01-21-2007, 02:32 AM
The moust important is that it works , lol
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.