FLORIDA HIGH SCHOOLS COMPUTING COMPETITION '83 BASIC PROGRAM SOLUTIONS '1.1 ' This program will round a number to nearest whole number. ' INPUT "Enter number: "; N PRINT INT(N + .5) '1.2 ' This program will display 5 numbers in descending order. ' FOR I = 1 TO 5 INPUT "Enter number: "; A(I) NEXT I FOR I = 1 TO 4 FOR J = I + 1 TO 5 IF A(I) < A(J) THEN SWAP A(I), A(J) NEXT J NEXT I FOR I = 1 TO 5: PRINT A(I): NEXT I '1.3 ' This program will print the factors of a given number. ' INPUT "Enter number: "; N FOR I = 1 TO N IF N MOD I = 0 THEN PRINT I NEXT I '1.4 ' This program will produce a birthday card w/name centered. ' INPUT "Enter name: "; N$ FOR I = 1 TO 5 PRINT IF I = 1 OR I = 5 THEN FOR J = 1 TO 12: PRINT "*"; : NEXT J IF I = 2 THEN PRINT "* HAPPY *"; IF I = 3 THEN PRINT "* BIRTHDAY *"; IF I = 4 THEN PRINT "*"; : L = LEN(N$) SP = INT((10 - L) / 2 + .5) PRINT SPACE$(SP); N$; SPACE$(10 - L - SP); "*"; END IF NEXT I '1.5 ' This program will print a ? in random locations. ' CLS : RANDOMIZE TIMER FOR I = 1 TO 6 C = INT(RND(3) * 80) + 1: R = INT(RND(3) * 23) + 1 LOCATE R, C: PRINT "?"; FOR J = 1 TO 3000 * 5: NEXT J NEXT I '1.6 ' This program will print a B for a, C for B, ... Z for A. ' INPUT "Enter character: "; CH$ IF CH$ < "Z" THEN PRINT CHR$(ASC(CH$) + 1) ELSE PRINT "A" '1.7 ' This program will print 4 distinct rectangles in corners. ' CLS R = 1: C = 1: GOSUB DrawRec R = 1: C = 65: GOSUB DrawRec R = 19: C = 1: GOSUB DrawRec R = 19: C = 65: GOSUB DrawRec END DrawRec: FOR I = R TO R + 3 IF I <> R + 1 AND I <> R + 2 THEN LOCATE I, C FOR J = 1 TO 10: PRINT "*"; : NEXT J END IF LOCATE I, C: PRINT "*" LOCATE I, C + 9: PRINT "*" NEXT I RETURN '1.8 ' This program will count the number of E's in a sentence. ' INPUT "Enter sentence: "; S$ FOR I = 1 TO LEN(S$) C$ = MID$(S$, I, 1) IF C$ = "E" THEN E = E + 1 NEXT I PRINT E '1.9 ' This program will calculate the average socre for a person. ' DATA JOHN,20,70,32 DATA BILL,71,40,30 DATA MARY,80,42,73 INPUT "Enter name: "; N$ FOR I = 1 TO 3 READ B$, A, B, C IF B$ = N$ THEN PRINT (A + B + C) / 3 NEXT I '1.10 ' This program will reverse the digits of a 4 digit number. ' INPUT "Enter number: "; N$ FOR I = 4 TO 1 STEP -1 PRINT MID$(N$, I, 1); NEXT I '2.1 ' This program will calculate the area of a regular hexagon. ' INPUT "Enter perimeter: "; P S = P / 6 PRINT SQR(3) * S / 2 * S / 2 * 6 '2.2 ' This program will convert a base 8 num to a base 2 num. ' INPUT "Enter number: "; N$: L = LEN(N$) FOR I = 1 TO L N = VAL(MID$(N$, I, 1)) FOR J = 2 TO 0 STEP -1 X = INT(N / 2 ^ J): PRINT USING "#"; X; N = N - X * 2 ^ J NEXT J NEXT I: PRINT '2.3 ' This program will add several items with tax (5%). ' INPUT "Enter item: "; IT WHILE IT <> -999 T = T + IT INPUT "Enter item: "; IT WEND PRINT "SUBTOTAL = $"; T TAX = INT((T * .05) * 100 + .5) / 100 PRINT "TAX = $ "; TAX PRINT "TOTAL = $"; T + TAX '2.4 ' This program will divide the screen into 4 rectangles. ' INPUT "Enter character: "; N$ CLS FOR I = 1 TO 24 IF I <> 12 THEN PRINT SPACE$(39); N$ IF I = 12 THEN FOR J = 1 TO 79: PRINT N$; : NEXT J NEXT I '2.5 ' This program will print the greatest and least in a set. ' MAX = -900: MIN = 900 INPUT "Enter number: "; NUM WHILE NUM <> -999 IF NUM < MIN THEN MIN = NUM IF NUM > MAX THEN MAX = NUM INPUT "Enter number: "; NUM WEND PRINT "GREATEST = "; MAX PRINT "LEAST = "; MIN '2.6 ' This program will print the sum, mean, median. ' FOR I = 1 TO 10 INPUT "Enter number: "; A(I): S = S + A(I) NEXT I FOR I = 1 TO 9 FOR J = I + 1 TO 10 IF A(I) > A(J) THEN SWAP A(J), A(I) NEXT J NEXT I PRINT "SUM ="; S PRINT "MEAN ="; S / 10 PRINT "MEDIAN ="; (A(5) + A(6)) / 2 '2.7 ' This program will reverse the words in a sentence. ' Assume 1 space between each word. ' INPUT "Enter sentence: "; A$: NUM = 1 FOR I = 1 TO LEN(A$) C$ = MID$(A$, I, 1) IF C$ <> " " THEN W$(NUM) = W$(NUM) + C$ ELSE NUM = NUM + 1 NEXT I FOR I = NUM TO 1 STEP -1: PRINT W$(I); " "; : NEXT I '2.8 ' This program will convert cubic feet to cubic meters. ' INPUT "Enter cubic feet: "; CF C3 = CF * (12 * 2.54) ^ 3 CM = C3 / 100 / 100 / 100 PRINT USING "###.#### CUBIC METERS"; CM '2.9 ' This program will find sum of Ys and Xs for Y=2(X+5). ' INPUT "Enter a, b: "; A, B FOR X = A TO B: S = S + 2 * (X + 5): NEXT X PRINT "SUM ="; S '2.10 ' This program will print 1 char. for 10 sec, 2 for 10 sec... ' INPUT "Enter character: "; A$: CLS FOR I = 1 TO 10 FOR J = 1 TO I: PRINT A$; : NEXT J FOR J = 1 TO 2500 * 10: NEXT J CLS NEXT I '3.1 ' This program converts a number from one base to another. ' INPUT "NUMBER"; N$ INPUT "BASE"; B INPUT "CONVERT TO BASE"; C L = LEN(N$) FOR I = 1 TO L X = VAL(MID$(N$, I, 1)) S = S + X * B ^ (L - I) NEXT I J = INT(LOG(S) / LOG(C)) FOR I = J TO 0 STEP -1 Y = INT(C ^ I + .1) X = INT(S / Y): NUM = NUM + X * 10 ^ I S = S - X * Y NEXT I PRINT NUM '3.2 ' This program will determine what triangle is made w/3 points. ' INPUT "Enter X1, Y1: "; X1, Y1 INPUT "Enter X2, Y2: "; X2, Y2 INPUT "Enter X3, Y3: "; X3, Y3 D1 = SQR((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2)) D2 = SQR((X2 - X3) * (X2 - X3) + (Y2 - Y3) * (Y2 - Y3)) D3 = SQR((X3 - X1) * (X3 - X1) + (Y3 - Y1) * (Y3 - Y1)) IF (D1 + D2 = D3) OR (D1 + D3 = D2) OR (D2 + D3 = D1) THEN PRINT "NOT A TRIANGLE": END END IF IF (D1 = D2) AND (D2 = D3) THEN PRINT "EQUILATERAL": END IF (D1 = D2) OR (D2 = D3) OR (D1 = D3) THEN PRINT "ISOSCELES" ELSE PRINT "SCALENE" END IF '3.3 ' This program randomly selcts an X, Y in 10 x 10 grid. User ' guesses numbers; if guess is wrong, a direction is given. ' RANDOMIZE TIMER X = INT(RND(4) * 9) + 1: Y = INT(RND(4) * 9) + 1 WHILE (A <> X) OR (B <> Y) INPUT "Enter X, Y:"; A, B IF A = X AND B < Y THEN PRINT "UP" IF A = X AND B > Y THEN PRINT "DOWN" IF A > X AND B = Y THEN PRINT "LEFT" IF A < X AND B = Y THEN PRINT "RIGHT" IF A < X AND B < Y THEN PRINT "UP AND RIGHT" IF A < X AND B > Y THEN PRINT "DOWN AND RIGHT" IF A > X AND B < Y THEN PRINT "UP AND LEFT" IF A > X AND B > Y THEN PRINT "DOWN AND LEFT" WEND '3.4 ' This program will divide 1st number by 2nd out to N places. ' INPUT "FIRST NUMBER"; N INPUT "SECOND NUMBER"; D INPUT "NUMBER OF DECIMAL PLACES"; P X = INT(N / D): PRINT USING "#."; X; N = N - X * D FOR I = 1 TO P N = N * 10: X = INT(N / D): PRINT USING "#"; X; N = N - X * D NEXT I: PRINT '3.5 ' This program will display numbers 1-8 and a blank in a ' 3 x 3 array. When a digit is pressed, it moves into the ' blank (if possible). ' RANDOMIZE TIMER ' Assign numbers in array sequentially then scramble them FOR I = 1 TO 3 FOR J = 1 TO 3 A(I, J) = (I - 1) * 3 + J - 1 NEXT J NEXT I FOR I = 1 TO 3 FOR J = 1 TO 3 R1 = INT(RND(3) * 3) + 1: R2 = INT(RND(3) * 3) + 1 X = A(I, J): A(I, J) = A(R1, R2): A(R1, R2) = X NEXT J NEXT I ' WHILE (DIG <> 9) ' Display Array CLS FOR I = 1 TO 3 FOR J = 1 TO 3 IF A(I, J) > 0 THEN PRINT A(I, J); " "; IF A(I, J) = 0 THEN PRINT " "; : BX = I: BY = J NEXT J: PRINT NEXT I ' Accept valid digit or 9 (to end) VALID = 0 WHILE (VALID = 0) AND (DIG <> 9) A$ = "": WHILE A$ = "": A$ = INKEY$: WEND DIG = VAL(A$) FOR I = 1 TO 3 FOR J = 1 TO 3 IF DIG = A(I, J) THEN IX = I: IY = J NEXT J NEXT I IF ABS(BX - IX) + ABS(BY - IY) = 1 THEN VALID = 1 WEND ' IF VALID THEN ' Move digit into blank space X = A(IX, IY): A(IX, IY) = A(BX, BY): A(BX, BY) = X END IF WEND '3.6 ' This program will store a list of words and provide options. ' WHILE OP <= 3 PRINT PRINT "1. ADD A WORD TO THE LIST" PRINT "2. DELETE A WORD FROM THE LIST" PRINT "3. DISPLAY THE ENTIRE LIST" INPUT OP IF OP = 1 THEN NUM = NUM + 1: INPUT "Enter word: "; W$(NUM) ELSEIF OP = 2 THEN INPUT "Enter word: "; DEL$: I = 1 WHILE (I <= NUM) AND (W$(I) <> DEL$) I = I + 1 WEND FOR J = I TO NUM - 1: W$(J) = W$(J + 1): NEXT J NUM = NUM - 1 ELSEIF OP = 3 THEN FOR I = 1 TO NUM: PRINT W$(I): NEXT I END IF WEND '3.7 ' This program will solve crytorithms with two 2-letter addends ' and a 3-letter sum, using only the letters A, B, C, D, and E. ' INPUT "Enter first addend: "; S1$ INPUT "Enter second addend: "; S2$ INPUT "Enter sum: "; S3$ L$ = S1$ + S2$ + S3$ ' Store in FL() the index of the first occurence FOR I = 1 TO 7 CH$ = MID$(L$, I, 1) J = 1: WHILE MID$(L$, J, 1) <> CH$: J = J + 1: WEND FL(I) = J IF J = I THEN NL = NL + 1: UL(NL) = I 'A new letter NEXT I ' FOR N1 = 10 TO 98 'N1 must be 2 digits, >9 FOR N2 = 100 - N1 TO 98 'N2 must be 2 digits, >9 SUM = N1 + N2 'Sum must be 3 digits >99 N1$ = MID$(STR$(N1), 2) N2$ = MID$(STR$(N2), 2) SUM$ = MID$(STR$(SUM), 2) NS$ = N1$ + N2$ + SUM$ I = 1: SOL = 1 ' Check if similar letters correspond to similar #s WHILE (I <= 7) AND (SOL = 1) CH$ = MID$(NS$, I, 1) IF CH$ <> MID$(NS$, FL(I), 1) THEN SOL = 0 I = I + 1 WEND ' Check if unique letters correspond to unique digits FOR I = 1 TO NL - 1 FOR J = I + 1 TO NL C1$ = MID$(NS$, UL(I), 1) C2$ = MID$(NS$, UL(J), 1) IF C1$ = C2$ THEN SOL = 0 NEXT J NEXT I ' Display Solution IF SOL > 0 THEN FOR I = 1 TO NL PRINT MID$(L$, UL(I), 1); " = "; MID$(NS$, UL(I), 1), NEXT I PRINT : TOT = TOT + 1: END END IF NEXT N2 NEXT N1 IF TOT = 0 THEN PRINT "NO SOLUTION POSSIBLE" '3.8 ' This program will simulate random frog jumps on nine pads. ' RANDOMIZE TIMER CLS FOR I = 1 TO 10 LOCATE 1, 1: PRINT SPACE$(40); LOCATE 2, 1: PRINT "- - - - - - - - -" F = 5 LOCATE 1, F * 2 - 1: PRINT "F": NUM = 0 WHILE (F > 1) AND (F < 9) IF INT(RND(3) * 2) = 1 THEN F = F + 1 ELSE F = F - 1 LOCATE 1, 1: PRINT SPACE$(40); LOCATE 1, F * 2 - 1: PRINT "F" FOR J = 1 TO 25: NEXT J NUM = NUM + 1 WEND LOCATE 5, I * 3: PRINT NUM NEXT I '3.9 ' This program will allow a user to position a cursor under a ' sentence using the L and R keys. Space bar deletes letter. ' CLS : INPUT "Enter sentence:"; S$: COL = 18 WHILE (CH$ <> CHR$(27)) AND (LEN(S$) > 1) LOCATE 2, COL, 1 DO: CH$ = INKEY$: LOOP UNTIL CH$ > "" IF CH$ = "R" THEN COL = COL + 1 IF CH$ = "L" THEN COL = COL - 1 IF CH$ = " " THEN L = LEN(S$) S$ = LEFT$(S$, COL - 18) + RIGHT$(S$, L - (COL - 17)) END IF LOCATE 1, 18: PRINT S$; " " WEND '3.10 ' This program will simulate the movement of a pool ball on a ' rectangular pool table. It moves in a 45 degree angle. ' INPUT "Enter Width, Length: "; W, L CLS : SCREEN 1 WI = 8: LI = 8 FOR I = 0 TO W: LINE (0, I * WI)-(L * LI, I * WI): NEXT I FOR I = 0 TO L: LINE (I * LI, 0)-(I * LI, W * WI): NEXT I X = 0: Y = W * WI: XD = 1: YD = -1 WHILE FI = 0 PSET (X, Y), 0 X = X + XD: Y = Y + YD PSET (X, Y), 1 IF X = 0 OR X = L * LI THEN XD = -1 * XD IF Y = 0 OR Y = W * WI THEN YD = -1 * YD FINISHED = 1: LOCATE 20, 1 IF X = 0 AND Y = 0 THEN PRINT "LEFT-TOP": FI = 1 IF X = 0 AND Y = W * WI THEN PRINT "LEFT-BOTTOM": FI = 1 IF X = L * LI AND Y = 0 THEN PRINT "RIGHT-TOP": FI = 1 IF X = L * LI AND Y = W * WI THEN PRINT "RIGHT-BOTTOM": FI = 1 WEND DO: A$ = INKEY$: LOOP UNTIL A$ <> "" SCREEN 0: WIDTH 80: CLS