10 PRINT "FUNCTIONS AND SUBROUTINES DEMO"
20 PRINT "================================"
30 GOSUB 1000  ' Initialize data
40 GOSUB 1100  ' Display menu
50 GOSUB 1200  ' Process choice
60 GOTO 40
70 END
80
1000 ' Initialize data
1010 DIM NAMES$(5)
1020 DIM SCORES(5)
1030 LET NAMES$(1) = "Alice"
1040 LET NAMES$(2) = "Bob"
1050 LET NAMES$(3) = "Charlie"
1060 LET NAMES$(4) = "Diana"
1070 LET NAMES$(5) = "Eve"
1080 FOR I = 1 TO 5
1090 LET SCORES(I) = INT(RND * 100) + 1
1100 NEXT I
1110 RETURN
1120
1100 ' Display menu
1110 PRINT
1120 PRINT "Choose an option:"
1130 PRINT "1 - Display all scores"
1140 PRINT "2 - Find highest score"
1150 PRINT "3 - Calculate average"
1160 PRINT "4 - Search for name"
1170 PRINT "5 - Sort scores"
1180 PRINT "6 - Exit"
1190 PRINT
1200 RETURN
1210
1200 ' Process choice
1210 INPUT "Enter your choice (1-6): ", CHOICE
1220 PRINT
1230 IF CHOICE = 1 THEN GOSUB 2000
1240 IF CHOICE = 2 THEN GOSUB 2100
1250 IF CHOICE = 3 THEN GOSUB 2200
1260 IF CHOICE = 4 THEN GOSUB 2300
1270 IF CHOICE = 5 THEN GOSUB 2400
1280 IF CHOICE = 6 THEN GOTO 70
1290 RETURN
1300
2000 ' Display all scores
2010 PRINT "Student Scores:"
2020 PRINT "==============="
2030 FOR I = 1 TO 5
2040 PRINT NAMES$(I); ": "; SCORES(I)
2050 NEXT I
2060 RETURN
2070
2100 ' Find highest score
2110 LET MAX_SCORE = 0
2120 LET MAX_NAME$ = ""
2130 FOR I = 1 TO 5
2140 IF SCORES(I) > MAX_SCORE THEN
2150 LET MAX_SCORE = SCORES(I)
2160 LET MAX_NAME$ = NAMES$(I)
2170 END IF
2180 NEXT I
2190 PRINT "Highest Score:"
2200 PRINT MAX_NAME$; " with "; MAX_SCORE; " points"
2210 RETURN
2220
2200 ' Calculate average
2210 LET TOTAL = 0
2220 FOR I = 1 TO 5
2230 LET TOTAL = TOTAL + SCORES(I)
2240 NEXT I
2250 LET AVERAGE = TOTAL / 5
2260 PRINT "Average Score: "; AVERAGE
2270 RETURN
2280
2300 ' Search for name
2310 INPUT "Enter name to search: ", SEARCH$
2320 LET FOUND = 0
2330 FOR I = 1 TO 5
2340 IF NAMES$(I) = SEARCH$ THEN
2350 PRINT "Found "; SEARCH$; " with score: "; SCORES(I)
2360 LET FOUND = 1
2370 END IF
2380 NEXT I
2390 IF FOUND = 0 THEN PRINT "Name not found."
2400 RETURN
2410
2400 ' Sort scores (bubble sort)
2410 FOR I = 1 TO 4
2420 FOR J = 1 TO 5 - I
2430 IF SCORES(J) < SCORES(J + 1) THEN
2440 LET TEMP = SCORES(J)
2450 LET SCORES(J) = SCORES(J + 1)
2460 LET SCORES(J + 1) = TEMP
2470 LET TEMP$ = NAMES$(J)
2480 LET NAMES$(J) = NAMES$(J + 1)
2490 LET NAMES$(J + 1) = TEMP$
2500 END IF
2510 NEXT J
2520 NEXT I
2530 PRINT "Scores sorted in descending order:"
2540 GOSUB 2000
2550 RETURN