多维数组问题
自定义一个求最大值函数:Amax:
function Amax(a:array of double):double;
var i:integer;
begin
result:=a[low(a)];
for i:=low(a) to high(a) do begin
if a[i]>result then
result:=a[i];
end;
end;
假设现在有个数组B[1..n,1..m],即有n行,m列
要求每一个列的最大值
如何通过Amax函数调用,来求每一列的最大值
谢谢
[解决办法]
- Delphi(Pascal) code
type TArr1=array of array of double; {定义动态2维数组,用来计算每列最大值}type TArr2=array of double; {定义动态1维数组,用来保存结果}function Amax(a:Tarr1):Tarr2;var i,j:integer; b:Tarr2;begin setlength(b,high(a[0])+1); {分配1维数组长度:即等于2维数组的列} for i:=0 to high(a[0]) do for j:=0 to high(a) do if a[j,i]>b[i] then b[i]:=a[j,i]; result:=b; {返回1维数组}end;procedure TForm1.Button1Click(Sender: TObject);var i:integer; a:TArr1; b:TArr2;begin setlength(a,3,4); {分配2维数组长度:3*4即3行4列} {初始化,可以循环赋值,这里只是简单的,方便直接看结果} a[0,0]:=1; a[0,1]:=2; a[0,2]:=3; a[0,3]:=4; a[1,0]:=5; a[1,1]:=6; a[1,2]:=7; a[1,3]:=8; a[2,0]:=9; a[2,1]:=10; a[2,2]:=12; a[2,3]:=11; b:=Amax(a); for i:=low(b) to high(b) do showmessage(FloatToStr(b[i])); //show出结果end;