Task

Find the top   N   salaries in each department,   where   N   is provided as a parameter.

Use this data as a formatted internal data structure (adapt it to your language-native idioms, rather than parse at runtime), or identify your external data source:

Employee Name,Employee ID,Salary,Department
Tyler Bennett,E10297,32000,D101
John Rappl,E21437,47000,D050
George Woltman,E00127,53500,D101
Adam Smith,E63535,18000,D202
Claire Buckman,E39876,27800,D202
David McClellan,E04242,41500,D101
Rich Holcomb,E01234,49500,D202
Nathan Adams,E41298,21900,D050
Richard Potter,E43128,15900,D101
David Motsinger,E27002,19250,D202
Tim Sampair,E03033,27000,D101
Kim Arlich,E10001,57000,D190
Timothy Grove,E16398,29900,D190



100 PROGRAM "Employee.bas"(N)
110 LET NR=100:LET X=0
120 STRING NAME$(1 TO NR)*20,ID$(1 TO NR)*6,DEPT$(1 TO NR)*4
130 NUMERIC SALARY(1 TO NR)
140 DO UNTIL N>0 AND N<=NR
150   INPUT PROMPT "Enter the number of ranks: ":N$
160   LET N=VAL(N$)
170 LOOP
180 CALL READDATA
190 CALL SORT
200 CALL LST
210 END
220 DEF READDATA
230   LET EOF=0
240   OPEN #1:"Employee.dat"
250   WHEN EXCEPTION USE IOERROR
260     DO
270       LET X=X+1
280       INPUT #1:NAME$(X),ID$(X),SALARY(X),DEPT$(X)
290     LOOP UNTIL EOF OR X=NR
300   END WHEN
310   HANDLER IOERROR
320     IF EXTYPE<>8001 THEN PRINT EXSTRING$(EXTYPE)
330     CLOSE #1
340     LET X=X-1:LET EOF=-1
350     IF X=0 THEN PRINT "No data.":STOP 
360     CONTINUE
370   END HANDLER
380 END DEF
390 DEF SORT
400   LET GAP=X:LET SW=1
410   DO WHILE GAP>1 OR SW
420     LET GAP=MAX(INT(GAP/1.3),1):LET SW=0
430     FOR I=1 TO X-GAP
440       IF DEPT$(I)>DEPT$(I+GAP) OR DEPT$(I)=DEPT$(I+GAP) AND SALARY(I)<SALARY(I+GAP) THEN
450         LET T$=NAME$(I):LET NAME$(I)=NAME$(I+GAP):LET NAME$(I+GAP)=T$
460         LET T$=DEPT$(I):LET DEPT$(I)=DEPT$(I+GAP):LET DEPT$(I+GAP)=T$
470         LET T$=ID$(I):LET ID$(I)=ID$(I+GAP):LET ID$(I+GAP)=T$
480         LET T=SALARY(I):LET SALARY(I)=SALARY(I+GAP):LET SALARY(I+GAP)=T
490         LET SW=1
500       END IF
510     NEXT
520   LOOP
530 END DEF
540 DEF LST
550   LET J=1:LET PREV$=""
560   FOR I=1 TO X
570     IF DEPT$(I)<>PREV$ THEN PRINT "Department ";DEPT$(I):LET PREV$=DEPT$(I):LET J=1
580     IF J<=N THEN PRINT "  ";NAME$(I);TAB(23);ID$(I),SALARY(I):LET J=J+1
590   NEXT
600 END DEF