Reputation: 1087
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 x y z values?
Thank You so much !
Upvotes: 1
Views: 686
Reputation: 186
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);
Upvotes: 1
Reputation: 125728
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.
Upvotes: 0