为什么我仍然收到退出代码 201?
Why am I still getting the exitcode 201?
我做了一些搜索,说它是关于在不适合的地方存储一个值,但我不知道它是如何在我的代码中发生的。我检查了我的代码很多次,但我仍然得到这个。我做了一些搜索,说它是关于在不适合的地方存储一个值,但我不知道它是如何在我的代码中发生的。我检查了我的代码很多次,但我仍然得到这个。
program ACT2;
uses crt;
var
inputs : array[1..5] of integer;
index1 : integer;
choice : char;
toPrint : integer;
function getSUM(var inputArray : array of integer) : integer;
var
SUM, sumIndex : integer;
begin
SUM := 0;
sumIndex := 1;
repeat
SUM := SUM + inputArray[sumIndex];
sumIndex := sumIndex + 1;
until (sumIndex > 5);
getSUM := SUM;
end;
begin
clrscr;
for index1 := 1 to 5 do
begin
write('Input Integer[', index1); write(']: ');
readln(inputs[index1]);
end;
clrscr;
write('Integers:');
for index1 := 1 to 5 do
begin
write(' ', inputs[index1]);
end;
writeln(''); writeln(''); writeln('');
writeln('[1] SUM');
writeln('[2] AVERAGE');
writeln('');
write('INPUT: ');
readln(choice);
if(choice = '1') then
toPrint := getSUM(inputs);
writeln(toPrint);
readkey;
end.
在函数语句中
function getSUM(var inputArray : array of integer) : integer;
inputArray 在open array(参见 FPC 参考手册 14.4.5,Open array 参数)。索引范围为 0 到 4,更好
使用 low(inputArray)
和 high(inputArray)
。在您使用的重复循环中
inputArray[5],其中索引 5 超出范围。
你的函数有误GetSum
。
参数var inputArray : array of integer
是一个从零开始索引的开放数组。
201 Range check error
If you compiled your program with range checking on, then you can get this error in the following cases:
* An array was accessed with an index outside its declared range.
* Trying to assign a value to a variable outside its range (for instance an enumerated type).
您在这里使用了声明范围之外的索引。
让你的循环像这样:
for sumIndex := 0 to High(inputArray) do ...
这将使函数通用,与数组大小无关。
其他两个答案已正确确定了问题的原因,即您使用的是两种不同基数的数组类型,1 与 0。
但是,您可以首先通过声明自己的整数数组类型然后始终如一地使用它来避免这个问题,
如
type
TInputArray = array[1..5] of Integer;
var
inputs : TInputArray; //array[1..5] of integer;
index1 : integer;
choice : char;
toPrint : integer;
//function getSUM(var inputArray : array of integer) : integer;
function getSUM(var inputArray : TInputArray) : integer;
var
SUM, sumIndex : integer;
begin
SUM := 0;
sumIndex := Low(inputArray);
repeat
SUM := SUM + inputArray[sumIndex];
sumIndex := sumIndex + 1;
until (sumIndex > High(inputArray));
getSUM := SUM;
end;
注意整数5在这个版本的程序中只出现一次,使用标准的Low
和High
函数意味着只有一个地方5
如果要更改数组的大小,则需要更改。事实上,将元素的数量定义为常量并在数组声明中使用该常量可能会更好,如
const NumOfElements = 5;
type
TInputArray = array[1..NumOfElements] of Integer;
我做了一些搜索,说它是关于在不适合的地方存储一个值,但我不知道它是如何在我的代码中发生的。我检查了我的代码很多次,但我仍然得到这个。我做了一些搜索,说它是关于在不适合的地方存储一个值,但我不知道它是如何在我的代码中发生的。我检查了我的代码很多次,但我仍然得到这个。
program ACT2;
uses crt;
var
inputs : array[1..5] of integer;
index1 : integer;
choice : char;
toPrint : integer;
function getSUM(var inputArray : array of integer) : integer;
var
SUM, sumIndex : integer;
begin
SUM := 0;
sumIndex := 1;
repeat
SUM := SUM + inputArray[sumIndex];
sumIndex := sumIndex + 1;
until (sumIndex > 5);
getSUM := SUM;
end;
begin
clrscr;
for index1 := 1 to 5 do
begin
write('Input Integer[', index1); write(']: ');
readln(inputs[index1]);
end;
clrscr;
write('Integers:');
for index1 := 1 to 5 do
begin
write(' ', inputs[index1]);
end;
writeln(''); writeln(''); writeln('');
writeln('[1] SUM');
writeln('[2] AVERAGE');
writeln('');
write('INPUT: ');
readln(choice);
if(choice = '1') then
toPrint := getSUM(inputs);
writeln(toPrint);
readkey;
end.
在函数语句中
function getSUM(var inputArray : array of integer) : integer;
inputArray 在open array(参见 FPC 参考手册 14.4.5,Open array 参数)。索引范围为 0 到 4,更好
使用 low(inputArray)
和 high(inputArray)
。在您使用的重复循环中
inputArray[5],其中索引 5 超出范围。
你的函数有误GetSum
。
参数var inputArray : array of integer
是一个从零开始索引的开放数组。
201 Range check error
If you compiled your program with range checking on, then you can get this error in the following cases:
* An array was accessed with an index outside its declared range. * Trying to assign a value to a variable outside its range (for instance an enumerated type).
您在这里使用了声明范围之外的索引。
让你的循环像这样:
for sumIndex := 0 to High(inputArray) do ...
这将使函数通用,与数组大小无关。
其他两个答案已正确确定了问题的原因,即您使用的是两种不同基数的数组类型,1 与 0。
但是,您可以首先通过声明自己的整数数组类型然后始终如一地使用它来避免这个问题, 如
type
TInputArray = array[1..5] of Integer;
var
inputs : TInputArray; //array[1..5] of integer;
index1 : integer;
choice : char;
toPrint : integer;
//function getSUM(var inputArray : array of integer) : integer;
function getSUM(var inputArray : TInputArray) : integer;
var
SUM, sumIndex : integer;
begin
SUM := 0;
sumIndex := Low(inputArray);
repeat
SUM := SUM + inputArray[sumIndex];
sumIndex := sumIndex + 1;
until (sumIndex > High(inputArray));
getSUM := SUM;
end;
注意整数5在这个版本的程序中只出现一次,使用标准的Low
和High
函数意味着只有一个地方5
如果要更改数组的大小,则需要更改。事实上,将元素的数量定义为常量并在数组声明中使用该常量可能会更好,如
const NumOfElements = 5;
type
TInputArray = array[1..NumOfElements] of Integer;