如何在 FreePascal 中正确传递动态数组

发布于 2024-12-04 05:42:28 字数 1848 浏览 0 评论 0原文

在我编写的 FreePascal 代码中,我发现在长度为“n”的动态数组中,它始终在元素“n”中包含随机值。

我明白为什么会这样,但是,我想知道我编写代码的方式是否存在缺陷。我已经把它粘贴在下面了。我欢迎任何指正。

program LinearSearch;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils
  { you can add units after this };

{$R *.res}

type
  ArrayOfByte = array of Byte;

Function findMaximum(studentMarks : ArrayOfByte; numberOfStudents : integer)
: integer;
var
  maximumMark : Integer;
  studentWithMaximumMark : Integer;
  index : integer;
begin
  setLength(studentMarks, numberOfStudents);
  maximumMark := studentMarks[0];

  for index:=0 to numberOfStudents do
  begin
    write(IntToStr(studentMarks[index]));
    if index = numberOfStudents then writeln('.')
    else write(',');
  end;

  for index:= 0 to (numberOfStudents - 1) do
  begin
      if studentMarks[index] > maximumMark then
      begin
           maximumMark := studentMarks[index];
           studentWithMaximumMark := index;
        end;
  end;
  write('Maximum score of ',IntToStr(maximumMark),' was achieved by ');
  Result := studentWithMaximumMark+1;
end;

var
  studentMarks : ArrayOfByte;
  numberOfStudents : Integer;
  studentWithMaximumMarks : Integer;
  counter : integer;
begin
  write('Enter the number of students: ' );
  readln(numberOfStudents);
  setLength(studentMarks,numberOfStudents);

  writeln('Input the grades for the following students:');
  for counter := 0 to (numberOfStudents - 1) do
  begin
    write('Student ',counter+1,': ');
    readln(studentMarks[counter]);
  end;
  writeln('Data has been input. Finding student with maximum marks.');

  studentWithMaximumMarks := findMaximum(studentMarks,numberOfStudents);
  write('student no. ',IntToStr(studentWithMaximumMarks));
  writeln;
  writeln('Press ANY key to continue');
  readln;
end.

In this FreePascal code I write, I found that in a dynamic array of length 'n', it always contained a random value in element 'n'.

I understand why that is, however, I am wondering if perhaps there is a flaw in the way i've written my code. I've pasted it below. I'd welcome any corrections.

program LinearSearch;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils
  { you can add units after this };

{$R *.res}

type
  ArrayOfByte = array of Byte;

Function findMaximum(studentMarks : ArrayOfByte; numberOfStudents : integer)
: integer;
var
  maximumMark : Integer;
  studentWithMaximumMark : Integer;
  index : integer;
begin
  setLength(studentMarks, numberOfStudents);
  maximumMark := studentMarks[0];

  for index:=0 to numberOfStudents do
  begin
    write(IntToStr(studentMarks[index]));
    if index = numberOfStudents then writeln('.')
    else write(',');
  end;

  for index:= 0 to (numberOfStudents - 1) do
  begin
      if studentMarks[index] > maximumMark then
      begin
           maximumMark := studentMarks[index];
           studentWithMaximumMark := index;
        end;
  end;
  write('Maximum score of ',IntToStr(maximumMark),' was achieved by ');
  Result := studentWithMaximumMark+1;
end;

var
  studentMarks : ArrayOfByte;
  numberOfStudents : Integer;
  studentWithMaximumMarks : Integer;
  counter : integer;
begin
  write('Enter the number of students: ' );
  readln(numberOfStudents);
  setLength(studentMarks,numberOfStudents);

  writeln('Input the grades for the following students:');
  for counter := 0 to (numberOfStudents - 1) do
  begin
    write('Student ',counter+1,': ');
    readln(studentMarks[counter]);
  end;
  writeln('Data has been input. Finding student with maximum marks.');

  studentWithMaximumMarks := findMaximum(studentMarks,numberOfStudents);
  write('student no. ',IntToStr(studentWithMaximumMarks));
  writeln;
  writeln('Press ANY key to continue');
  readln;
end.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗忘曾经 2024-12-11 05:42:28

在 Free Pascal 中,动态数组是基于零的。因此,第一个元素是 MyArray[0],最后一个元素是 MyArray[High(MyArray)],它等于 MyArray[Length(MyArray) - 1]。您永远不应该访问 MyArray[Length(MyArray)]

In Free Pascal dynamic arrays are zero based. So the first element is MyArray[0] and last element is MyArray[High(MyArray)] which is equal to MyArray[Length(MyArray) - 1]. You should never access MyArray[Length(MyArray)].

浅黛梨妆こ 2024-12-11 05:42:28

...我发现在长度为“n”的动态数组中,它总是在元素“n”中包含一个随机值。这是因为您正在访问超出数组边界的值,检查程序中的 findMaximum

for index:=0 to numberOfStudents do

必须是

for index:=0 to numberOfStudents-1 do

您也可以删除同一程序的第一行 setLength(studentMarks, numberOfStudents);

...I found that in a dynamic array of length 'n', it always contained a random value in element 'n'. this is because you are accessing a value beyond of the bounds of the array, check in the procedure findMaximum the line

for index:=0 to numberOfStudents do

must be

for index:=0 to numberOfStudents-1 do

Also you can remove the first line of the same procedure setLength(studentMarks, numberOfStudents);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文