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?

Closed due to the fact that off-topic participants Kromster , aleksandr barakin , freim , 0xdb , Suvitruf Feb 16 at 10:19 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, freim, 0xdb, Suvitruf
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Of course I am not an expert. But why not read immediately and in structure? After that it will be more convenient to sort the entire structure by salary, rather than 5 separate arrays. - Andrey Polovinkin


2 answers 2

To begin with, why do you have a function val has only two parameters, when there are three of them?

Converts a string value to its numeric representation.

Declaration: Procedure Val (S; Var V; Var Code: Integer); Where: S is a variable with a string type. Must be a sequence of characters that form a signed integer. V is a variable of type Real or Integer Code is a variable of type Integer

Mode: Windows, Protected, Real

Remarks: Converts a string value (S) to its numeric representation, as it does when reading from a text file with Read. Code is the position at which an error occurred during the conversion, or zero if there was no error.

Secondly, the type Integer (in a 16-bit system it is only 2 ^ 16-1 = 32767) is not enough for your task, use the Longint type for salary.

I checked the program, it works great. Although you must understand that if the file structure is false, then you will have errors.

    Error 201 occurs when one of the following situations occurs : the array index expression was out of the allowed range;

    To know more on what line swears then it would be possible to say where the exit is beyond. I don’t know pascal so that I’ll not say a quick glance where the error is, although judging by how you use global variables in the procedure, it seems that there is an error somewhere.