Preparing for the exam in computer science (ege). I came across this problem:
At the entrance to the program, information about employees of a certain organization is submitted. The first line indicates the number of employees N, which is not less than 5, but does not exceed 1000, each of the following N lines has the following format:
<Last Name> <Position> <Experience> <Salary> <Premium>,Where:
<Last Name> is a string consisting of no more than 20 characters;
<Position> is a string consisting of no more than 15 characters;
<Experience> <Salary> <Premium> - integers.Under the experience refers to the number of full years worked by an employee in this organization. Salary and bonus are integers not exceeding 1,000,000. These elements of the input string are separated from each other by a single space. Example input line:
Иванов механик 15 25500 5000
It is required to write a program that will display the names, positions and experience of three employees with the highest salary. If among the rest there are employees with the same salary as one of these three, then their names, positions and length of service should also be removed. The salary of an employee is equal to the sum of his salary and bonus.
I solve the problem in Pascal (I do not know other languages). I work according to the following principle: I read the lines in a cycle from 1 to N. Then, in the Disturb procedure, I divide these lines into the corresponding cells of the arrays. The procedure works - checked. When n is 1, 2 everything is fine. When n = 3 and more, the program stops its execution with error # 201. What am I not saying so? Please do not redo my decision.
Var data, name, dol: Array[1..1000] of String; sta, zar: Array[1..1000] of Integer; n, i, j, max1, max2, max3, index: Integer; Procedure Disturb; Var t: string; t1, t2: Integer; Begin index := pos(' ', data[i]); name[i] := copy(data[i], 1, index-1); Delete(data[i], 1, index); index := pos(' ', data[i]); dol[i] := copy(data[i], 1, index-1); Delete(data[i], 1, index); index := pos(' ', data[i]); t := copy(data[i], 1, index-1); Val(t, sta[i]); Delete(data[i], 1, index); index := pos(' ', data[i]); t := copy(data[i], 1, index-1); Val(t, t1); Delete(data[i], 1, index); t := copy(data[i], 1, Length(data[i])); Val(t, t2); zar[i] := t1+t2; End; Begin Readln(n); For i := 1 to n do Readln(data[i]); For i := 1 to n do Begin Disturb; End; max1 := 0; max2 := 0; max3 := 0; End.
Why does it give an error? Where do I address a non-existent cell or what is wrong?
completed the program. Here is the final version:
Var data, name, dol: Array[1..1000] of String; sta, zar: Array[1..1000] of LongInt; n, i, j, max1, max2, max3, index: LongInt; Procedure Disturb; Var t: string; t1, t2, err: LongInt; Begin index := pos(' ', data[i]); name[i] := copy(data[i], 1, index-1); Delete(data[i], 1, index); index := pos(' ', data[i]); dol[i] := copy(data[i], 1, index-1); Delete(data[i], 1, index); index := pos(' ', data[i]); t := copy(data[i], 1, index-1); Val(t, sta[i], err); Delete(data[i], 1, index); index := pos(' ', data[i]); t := copy(data[i], 1, index-1); Val(t, t1, err); Delete(data[i], 1, index); t := copy(data[i], 1, Length(data[i])); Val(t, t2, err); zar[i] := t1+t2; End; Begin Readln(n); For i := 1 to n do Readln(data[i]); For i := 1 to n do Disturb; max1 := 0; max2 := 0; max3 := 0; for i := 1 to n do if (max1 < zar[i]) then max1:=zar[i]; for i := 1 to n do if ((max2 < zar[i]) and (zar[i] < max1)) then max2 := zar[i]; for i := 1 to n do if ((max3 <zar[i]) and (zar[i] < max2)) then max3 := zar[i]; for i := 1 to n do if (zar[i] = max1) then writeln(name[i], ' ', dol[i], ' ', sta[i]); for i := 1 to n do if (zar[i] = max2) then writeln(name[i], ' ', dol[i], ' ', sta[i]); for i := 1 to n do if (zar[i] = max3) then writeln(name[i], ' ', dol[i], ' ', sta[i]); Readln; End.
please answer: the right decision?