K
K
Kotorkovsciy2021-12-04 01:53:03
Pascal
Kotorkovsciy, 2021-12-04 01:53:03

What is the error in the code, pascal ((12): Run-time error: Index was out of array bounds.)?

program p1;

type
  massive = array of integer;

function PolMos(a: massive; n: byte): char;
var
  c, i: byte;
begin
  c := 0;
  for i := low(a) to high(a) div 2 do
    if a[i] <> a[high(a) - i + 1] then c := 1;
  if c = 0 then writeln(chr(13), 'Массив является палиндромом')
  else writeln(chr(13), 'Массив не является палиндромом');
end;

function massiv(n: byte): char;
var
  i: byte;
  a: massive;
begin
  setlength(a, n);
  randomize;
  for i := low(a) to high(a) do        
  begin
    a[i] := random(n);        
    write(a[i]:4);
  end;
  PolMos(a, n);
end;

var
  n: byte;

begin
  write('Введите размер массива:   '); read(n);
  write('Массив: '); massiv(n);
  
end.


61aa9f2b2b50e828286684.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-12-04
@Kotorkovsciy

The index was outside the bounds of the array

It is written in Russian in white.
Let's imagine that we have an array a[] of size 2
Then
low(a)=0
high(a)=1
On the first iteration:
i=0
high(a) - i + 1 = 2
2 is outside the bounds of the array.
Also, no value is returned from your function.
And in PascalABC, you can set breakpoints and see what values ​​​​of variables you got there before the error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question