Search code examples
functionpascalprocedurefreepascal

Pascal Change a function into a procedure


I am trying to change the function inside the following program into a procedure. The program is supposed to read 5 different integers and then sort them from small numbers to large ones. The version with the function works fine, but the one with the procedure doesn't sort the numbers. It only prints the numbers I typed in. For example, when I type 4, 5 , 7, 3, 1 into the console, it would print 4, 5, 7, 3, 1 instead of the desired 1, 3, 4, 5, 7.

So the question is: how do I get the procedure version to work in the same way as the function version?

I am pretty sure there is something here that I don't understand, but I cannot seem to find out... Any help is greatly appreciated!

This is the version that uses a function:

 program FeldSortFunction(input, output);   { sorts a field of integers}

  FELDGROESSE = 5;

type   tIndex = 1..FELDGROESSE;   tFeld = array [tIndex] of integer;

var   EingabeFeld : tFeld;   MinPos,
    i : tIndex;   Tausch : integer;

function FeldMinimumPos (Feld : tFeld; von, bis:tIndex): tIndex; { finds the Position of the minimum inside the field between von and bis, 1 <= von <= bis <= FELDGROESSE }

   var    MinimumPos,    j:tIndex;

   begin
    MinimumPos := von;
    for j:= von + 1 to bis do
      if Feld[j] < Feld[MinimumPos] then
        MinimumPos := j;
    FeldMinimumPos := MinimumPos
    end; { FeldMinimumPos }

begin   { Read the field }   writeln ('Geben Sie ', FELDGROESSE, ' Werte ein:');   for i := 1 to FELDGROESSE do
    readln (EingabeFeld[i]);

  { sort the integers }

  for i := 1 to FELDGROESSE - 1 do   begin
    MinPos := FeldMinimumPos (EingabeFeld, i, FELDGROESSE);
    {The minimum has been found, now we need to exchange this value with       the element on position i}
    Tausch := EingabeFeld[MinPos];
    EingabeFeld[MinPos] := EingabeFeld[i];
    Eingabefeld[i] := Tausch;   end;

  { print the sorted field }   for i := 1 to FELDGROESSE do
      write (EingabeFeld[i]:6);   writeln;

  readln; end.     { FeldSort }

And this is the version that uses a procedure:

program FeldSortProcedure(input, output);
{ sorts a field of integers and defines the value of the minimum}


const
  FELDGROESSE = 5;

type
  tIndex = 1..FELDGROESSE;
  tFeld = array [tIndex] of integer;

var
  SortierFeld : tFeld;
  MinPos,MinimumPos,
    i : tIndex;
  MinimumWert : integer;

procedure FeldMinimumPosUndWert (Feld : tFeld; von, bis:tIndex; MinPos:tIndex; MinWert : integer);
{ finds  Position and value of the Minimums inside Feld between von and bis }

   var
   ind:tIndex;

begin
    MinPos := von;
    MinWert := Feld[von];
    for ind := von + 1 to bis do
    begin
      if Feld[ind] < Feld[MinPos] then
      begin
        MinPos := ind;
        MinWert := Feld[ind]
      end;
    end;

end; { FeldMinPosUndWert }

begin
  { Read the field }
  writeln ('Please key in ', FELDGROESSE, ' numbers:');
  for i := 1 to FELDGROESSE do
    readln (SortierFeld[i]);

  { sort the field }
  FeldMinimumPosUndWert (SortierFeld, i, FELDGROESSE, MinimumPos, MinimumWert);

  { prints the sorted field }
  for i := 1 to FELDGROESSE do
      write (SortierFeld[i]:6);
  writeln;

  readln;
end.     { FeldSort }

Solution

  • Obviously you aren't getting anywhere with this, so I'll give a hint:

    turn your function

    function FeldMinimumPos (Feld : tFeld; von, bis:tIndex): tIndex;
    

    into a procedure with:

    procedure FeldMinimumPos(feld: TFeld; von, bis: TIndex; var pos: TIndex);
    { finds the position of the minimum inside the field between von and bis,
      1 <= von <= bis <= FELDGROESSE }
    var
      j: TIndex;
    begin
      pos := von;
      for j := von + 1 to bis do
        if feld[j] < feld[pos] then
          pos := j;
    end; { FeldMinimumPos }
    

    and call it with:

    FeldMimimumPos(EingabeFeld, i, FELDGROESSE, MinPos);
    

    and you should be fine.


    A few remarks, though:

    • Your way of sorting is less than optimal, even for a field of only 5 elements. Learn about selection sort or even bubble sort (just google them).
    • Format your code properly. It is very hard to read. Put comments on their own line or after the code. Put every end on its own line and align it properly, don't put it after the previous command. Align your variable declarations, and start loops on their own line too. Etc...
    • You forgot a const above the FELDGROESSE declaration. The code you posted doesn't compile. Always post the actual code, copied from your editor and pasted (verbatim) into the edit box for your question (or answer). Do not post code from memory or by typing it off a screen or a sheet of paper.