K
K
Kotorkovsciy2021-12-03 15:56:45
Pascal
Kotorkovsciy, 2021-12-03 15:56:45

What is the error in the code, pascal ((30): Run-time error: Object reference does not point to an object instance.)?

const
  n = 100;

type
  massive = array[1..n] of integer;

function ProstMin(a: massive; x: integer): char;
var
  i, j, k: longint;
  min: integer;
  f: boolean;
  b: array of integer;
begin
  for j := 1 to x do 
    if a[j] < 2 then f := false
      else
    begin
      k := 0;
      f := true;
      for i := 2 to round(sqrt(a[j])) do
        if a[j] mod i = 0 then f := false;
      if f then 
      begin
        setlength(b,k+1);
        b[k] := a[j];
        inc(k);
      end;
      for i := 1 to x do
        begin
             write(b[i], ' ');
        end;
    end;
end;

function massiv(x: byte): char;
var
  i: byte;
  a: massive;
begin
  for i := 1 to x do          
  begin
    a[i] := random(100);        
    write(a[i]:3);
  end;
  writeln;
  ProstMin(a, x);
end;

var
  x: byte;

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

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
HemulGM, 2021-12-03
@Kotorkovsciy

you pulled up

type
  TArray = array of integer;

function IsPrime(const Value: integer): Boolean;
var
  N: integer;
begin
  for N := 2 to Value - 1 do
    if Value mod N = 0 then
      Exit(False);
  Result := True;
end;

function PrimeMin(const A: TArray): Integer;
var
  i: Integer;
begin
  Result := -1;
  for i := 0 to High(A) do
    if IsPrime(A[i]) then
      if (Result = -1) or (Result > A[i]) then
        Result := A[i];
end;

function FillRandom(Count: Integer): TArray;
var
  i: Integer;
begin
  Randomize;
  SetLength(Result, Count);
  for i := 0 to Count - 1 do
  begin
    Result[i] := Random(1000 - 2) + 2;
    write(Result[i]: 4);
  end;
  writeln;
end;

var
  Count: Integer;
  Items: TArray;

begin
  Write('Введите размер массива: ');
  Read(Count);
  Write('Массив: ');
  Items := FillRandom(Count);
  Write('Мин. простое: ', PrimeMin(Items));
  Readln;
  Readln;
end.

Just learn a little about the algorithm

V
Vasily Bannikov, 2021-12-03
@vabka

There are no errors - the program compiles and does exactly what is written in the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question