数组作为函数的参数 [英] Array as an argument of a function

查看:97
本文介绍了数组作为函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2维数组和函数。

我想这个函数把数组作为参数。<​​/ P>

我试过code是这样的:

  VAR ARRAY1:数组整数[1..10,1..10]
    数组2:数组整数[1..20,1..10]
函数名(VAR my_array:整数数组,正数组:整数);
函数名(VAR my_array:数组[1..1,1..10]整数;常量n:整数);

但我试图编译code有错误。任何提示?

如果你想我粘贴错误codeS每个版本请留下一个请求评论。

有关

 函数名(VAR my_array:整型数组的数组,n为整数);

错误code是:不兼容类型ARG号:得了数组[0..10]数组SMALLINT的[0..10],预计打开阵列OD SMALLINT每次我调用该函数。


解决方案

您必须声明自己的类型,然后使用该类型作为参数传递给你的函数。相反,通过阵列的尺寸,使用功能从系统单元;他们将与静态(pre-申报)和动态数组工作,避免硬编码数组的大小和迭代器。

您想避免硬编码整数索引,becaue帕斯卡静态数组没有下手指数 0 ;以下是完全合法的Pascal数组声明,其中数组边界是从index -3 3

  VAR
  编曲:数组整数[-3..3]

下面是一个使用动态数组一个例子(整数的数组的数组),通过二维数组循环,并求和值;它初始化一个5×5阵列,用数据填充它,然后调用 SumTwoDimIntArray 函数的值相加。

 程序Project2的;{$ APPTYPE CONSOLE}用途
  SysUtils单元;类型
  TTwoDimIntArray =整数数组的数组;功能SumTwoDimIntArray(编曲:TTwoDimIntArray):整数;
VAR
  I,J:整数;
开始
  结果:= 0;
  对于i:=低(ARR)到高(ARR)做
    对于j:=低(编曲[I])到高(编曲[I])做
      结果:=结果+编曲[I] [J]。
结束;VAR
  myArr,该:TTwoDimIntArray;
  I,J:整数;
开始
  SetLength函数(myArr,该,5);
  对于i:=低(myArr,该)到高(myArr,该)做
  开始
    SetLength函数(myArr,该[I],5);
    对于j:=低(myArr,该由[i])到高(myArr,该[I])做
      myArr,该由[i] [j]的:= J + 1;
  结束;
  WriteLn(格式('总和为%d,[SumTwoDimIntArray(myArr,该)]));
  ReadLn;
结束。

I have 2 2-dimensional arrays and a function.

I would like that function to take the array as an argument.

I tried code like this:

var array1:array[1..10,1..10] of integer;
    array2:array[1..20,1..10] of integer;
function name(var my_array:array of array of integer, n:integer);
function name(var my_array:array[1..n,1..10] of integer;const n:integer);

But I got errors while trying to compile the code. Any tips?

If you would like me to paste error codes for each version please leave a comment with a request.

For

function name(var my_array:array of array of integer, n:integer);

The error code is: "Incompatible type for arg no. : Got "Array[0..10] of Array[0..10] of SmallInt", expected "Open array od SmallInt" every time I call the function.

解决方案

You need to declare your own type, and then use that type as the parameter to your function. Instead of passing the array dimensions, use the Low and High functions from the System unit; they will work with both static (pre-declared) and dynamic arrays, and avoid hard-coding array sizes and iterators.

You want to avoid hard-coding integer indexes, becaue static arrays in Pascal don't have to start with index 0; the following is a perfectly legal Pascal array declaration, where the array bounds are from index -3 to 3:

var
  Arr: array[-3..3] of Integer;

Here's an example using a dynamic array (array of array of Integer) that loops through the two dimensional array and sums the values; it initializes a 5 x 5 array, populates it with data, and then calls the SumTwoDimIntArray function to sum the values.

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TTwoDimIntArray = array of array of Integer;

function SumTwoDimIntArray(Arr: TTwoDimIntArray): Integer;
var
  i, j: Integer;
begin
  Result := 0;
  for i := Low(Arr) to High(Arr) do
    for j := Low(Arr[i]) to High(Arr[i]) do
      Result := Result + Arr[i][j];
end;

var
  MyArr: TTwoDimIntArray;
  i, j: Integer;
begin
  SetLength(MyArr, 5);
  for i := Low(MyArr) to High(MyArr) do
  begin
    SetLength(MyArr[i], 5);
    for j := Low(MyArr[i]) to  High(MyArr[i]) do
      MyArr[i][j] := j + 1;
  end;
  WriteLn(Format('Sum is %d', [SumTwoDimIntArray(MyArr)]));
  ReadLn;
end.

这篇关于数组作为函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆