简体   繁体   中英

Finding maximum salary value with Pascal Record

I've written a programm which fill the different data about workers into the table. (Name, last Name and salary)

Help me write a procesure or function which look for the maximum salary value and a name of this worker and write it in console

Can i make it using a loop?

program labasix;

type firma = record
  name : string;
  lastName : string;
  salary : integer;
end;

var
  svitoch : array[1..12] of firma;
  i : integer;
  countOfWorkers : integer;
begin
  write('Number of workers (not more than 12): ');
  readln(countOfWorkers);
  writeln();

  for i := 1 to countOfWorkers do
    begin
      write('Name: '); readln( svitoch[i].name );
      write('lastName: '); readln( svitoch[i].lastName );
      write('Salary: '); readln( svitoch[i].salary );
      writeln();
    end;

   for i := 1 to countOfWorkers  do
     begin
        { what code must be here ??? }
     end;
end.

There must be something like this

procedure findMax(x, y, z: integer; var m: integer); 

begin
   if x > y then
      m:= x
   else
      m:= y;
   if z > m then
      m:= z;
end;

But how to get xyz values?

Thank You so much !

This is a simple function that returns you the index that contains the maximum value of salary in your array. Paste it in your code after this:

type firma = record
  name : string;
  lastName : string;
  salary : integer;
end;

This is the function:

function getmax():integer;
var max:integer;
begin
     max:=1;
     for i:=2 to countOfWorkers do
     begin
         if svitoch[i].salary > svitoch[max].salary then
         max:=i;
     end;
     getmax:=max;
end;

So now you can earn the maximum salary value(and name) by using this structure after your first for cycle and instead of the second one.

i:=getmax();
writeln(svitoch[i].name);  {if you want to write in your case}
writeln(svitoch[i].lastName);
writeln(svitoch[i].salary);

Well, clearly you need to look through the list (array) of workers you have now, looking for the one with the highest salary.

So write a function (not a procedure) that accepts that array as a parameter.

The function should store the salary of the first worker into a variable, and then loop through the rest of the workers; if a worker's salary is higher than the one you've already stored, replace the stored value with that new higher one and continue your loop. When you've reached the end of the list, you've stored the highest salary, which you then return from your function.

Hint: You should use Low(YourArray) as the starting point of the loop, and High(YourArray) as the stopping point of your loop, so that there's no limit to the number of workers you can pass to the function in that array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM