H
H
HNKHENM2014-10-09 02:11:40
Pascal
HNKHENM, 2014-10-09 02:11:40

The Pascal compiler produces a result (with the desired one) with a runtime error... Where is the error hidden?

I understand that the code is far from ideal, but after all, the desired result, although with an error, gives out!
In the function, it was necessary to find the minimum element in the row of a two-dimensional array, and swap the element of the main diagonal in the same row with this minimum element.

<code lang="delphi">
program four;

const n = 3;

type

matrix = array [1..n,1..n] of integer;

 var a:matrix; {b:secondMatrix;}
  mainJ, mainI,i, j,buf, min: integer;
{++++++++++++++++  <Функция>  +++++++++++++++++++++}

function anotherSort(var a: matrix):integer;

  var i1, j1,mi,mj: integer;
  begin
  
    min := a[1,1]; 
    for i1 := mainI to mainI do
    begin
      for j1 := 1 to n do
      begin
        if (min > a[i1,j1]) then
        begin
      
          min:=a[i1,j1];
          mi:=i1;
          mj:=j1;
          

        end;        
      end;
    end;
    buf := a[mi,mj];
    a[mi,mj] := a[mainI,mainJ];
    a[mainI,mainJ] := buf;      
    result := a[i,j];
  end;
{++++++++++++++++  </Функция>  +++++++++++++++++++++}
{НАЧАЛО ОСНОВНОЙ ПРОГРАММЫ}
begin

  for i:=1 to n do
  begin
    for j:=1 to n do
    begin
      a[i,j]:=(random(8)-9);
      write(a[i,j]:5);
    end;
    writeln;
  end;
  
  for i := 1 to n do
    for j := 1 to n do
    begin
      if ( i = j ) and (a[i,j] < 0) then
      begin
      
        mainI := i; 
        mainJ := j;  
        anotherSort(a);
        
      end;  
    end; 
  
writeln;
  
  for i := 1 to n do
  begin
    for j := 1 to n do
      begin
        write(a[i,j]:5);
      end;
      writeln;
  end;
     
 end.
{КОНЕЦ ОСНОВНОЙ ПРОГРАММЫ}
</code>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mcroodberg, 2014-10-09
@mcroodberg

I think that there should be some reasonable counter in the i1 loop of the anotherSort function, and not "mainI to mainI"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question