FLORIDA HIGH SCHOOLS COMPUTING COMPETITION '84 BASIC PROGRAM SOLUTIONS '1.1 ' This program will produce a table of Fahrenheit for Celcuis. ' PRINT "CELCIUS", "FAHRENHEIT" FOR C = 50 TO 200 STEP 25 PRINT C, INT(1.8 * C + 32 + .5) NEXT C '1.2 ' This program will determine time a person slept in seconds. ' INPUT "WHAT TIME DID YOU GO TO BED (H, M, S)"; H1, M1, S1 INPUT "WHAT TIME DID YOU GET UP (H, M, S)"; H2, M2, S2 T = (11 - H1) * 3600 + (59 - M1) * 60 + (60 - S1) PRINT "YOU SLEPT FOR"; T + H2 * 3600 + M2 * 60 + S2; "SECONDS" '1.3 'This program will display distance/height of a golf ball. ' PRINT "T", "H", "V" WHILE (V > 0) OR (T < 1) H = 120 * T: V = 120 * T - 16 * T * T PRINT T, H, V T = T + .5 WEND '1.4 ' This program will produce table of mice population and food. ' PRINT "NUMBER OF YEARS", "POPULATION", "FOOD SUPPLY FOR" Y = 0: P = 10: F = 100 PRINT Y, , P, F WHILE P < F Y = Y + 1: P = P * 2: F = F + 40 PRINT Y, , P, F WEND '1.5 ' This program will determine time that a savings doubles. ' INPUT "Enter amount, %"; N, P X = N: Y = 0 WHILE X < 2 * N X = X * (1 + P / 100) Y = Y + 1 WEND PRINT Y; "YEARS" '1.6 ' This program will determine name at beginning and end. ' MIN$ = "ZZZZZ": MAX$ = "AAAAAA" FOR I = 1 TO 5 INPUT NM$ IF NM$ < MIN$ THEN MIN$ = NM$ IF NM$ > MAX$ THEN MAX$ = NM$ NEXT I PRINT "NAME CLOSEST TO BEGINNING: "; MIN$ PRINT "NAME CLOSEST TO END: "; MAX$ '1.7 ' This program will determine longest run of heads of tosses. ' RANDOMIZE TIMER INPUT "N: "; N: H = 0: MAX = 0 FOR I = 1 TO N X = RND(2): IF X < .5 THEN H = H + 1 IF X >= .5 THEN IF H > MAX THEN MAX = H: H = 0 ELSE H = 0 NEXT I IF H > MAX THEN MAX = H PRINT MAX; "CONSECUTIVE HEADS" '1.8 ' This program will display numbers with 7s zapped. ' FOR I = 1 TO 100 T = INT(I / 10): O = I - T * 10: D7 = (I / 7 = INT(I / 7)) IF (T = 7 OR O = 7) AND D7 THEN PRINT "ZAPZAP", ELSEIF (T = 7 OR O = 7) THEN PRINT "ZAP", ELSE PRINT I, END IF NEXT I '1.9 ' This program will print the # of double letters. ' INPUT "Enter text: "; A$ FOR I = 1 TO LEN(A$) C$ = MID$(A$, I, 1) IF C$ = LC$ THEN D = D + 1 LC$ = C$ NEXT I PRINT D '1.10 ' This program will display sevens multiplication facts. ' FOR I = 0 TO 9 W = 0 DO PRINT I; "X 7 = "; : INPUT ANS IF ANS <> I * 7 THEN IF W = 0 THEN W = 1 ELSE PRINT I * 7: W = 2 END IF END IF LOOP UNTIL (I * 7 = ANS) OR (W = 2) NEXT I '2.1 ' This program will print number of vowels in text. ' INPUT "Enter text: "; A$ FOR I = 1 TO LEN(A$) C$ = MID$(A$, I, 1) IF C$ = "A" OR C$ = "E" OR C$ = "I" OR C$ = "O" OR C$ = "U" THEN V = V + 1 END IF NEXT I PRINT V; "VOWELS" '2.2 ' This program sorts rational numbers in increasing order. ' INPUT "Enter N, M: "; N, M WHILE N > 0 AND M > 0 S = S + 1: A$(S) = STR$(N) + "/" + STR$(M): V(S) = N / M INPUT "Enter N, M: "; N, M WEND FOR I = 1 TO S - 1 FOR J = I + 1 TO S IF V(I) > V(J) THEN SWAP V(I), V(J): SWAP A$(I), A$(J) NEXT J NEXT I FOR I = 1 TO S: PRINT A$(I): NEXT I '2.3 ' This program displays #s that sum of cubes of digits= #. ' FOR I = 1 TO 9 FOR J = 0 TO 9 FOR K = 0 TO 9 NUM = I * 100 + J * 10 + K IF NUM = I * I * I + J * J * J + K * K * K THEN PRINT NUM NEXT K NEXT J NEXT I '2.4 ' This program will print a triangle of #s by an algorithm. ' INPUT "Enter # of rows: "; N FOR I = 1 TO N PRINT SPACE$(N - I); FOR J = I TO 2 * I - 1 X = J - INT(J / 10) * 10 PRINT MID$(STR$(X), 2); NEXT J IF I > 1 THEN FOR J = 2 * I - 2 TO I STEP -1 X = J - INT(J / 10) * 10 PRINT MID$(STR$(X), 2); NEXT J END IF PRINT NEXT I '2.5 ' This program will display a page of multipilcation drills. ' RANDOMIZE TIMER: CLS PRINT " MULTIPLICATION DRILL" FOR I = 1 TO 6 H = INT((I - 1) / 3): V = I - H * 3: H = H * 20 + 1 X = INT(RND(3) * 90 + 10): Y = INT(RND(3) * 9 + 1) LOCATE V * 5, H: PRINT MID$(STR$(I), 2); ". "; X LOCATE V * 5 + 1, H: PRINT " X"; Y LOCATE V * 5 + 2, H: PRINT " ----" NEXT I '2.6 ' This program will simulate throwing darts. ' RANDOMIZE TIMER: INPUT "Enter N: "; N FOR I = 1 TO N X = INT(RND(3) * 5): Y = INT(RND(3) * 5) A(X, Y) = 1 NEXT I FOR I = 0 TO 4 FOR J = 0 TO 4 IF A(I, J) THEN PRINT "* "; : S = S + 1 ELSE PRINT ". "; NEXT J: PRINT NEXT I PRINT "NUMBER OF THROWS ="; N PRINT "NUMBER OF HITS ="; S '2.7 ' This program will determine if text is palindrome. ' PRINT "Enter text: "; : A$ = "" WHILE A$ <> CHR$(13) DO: A$ = INKEY$: LOOP UNTIL A$ <> "" PRINT A$; : IF A$ >= "A" AND A$ <= "Z" THEN S$ = S$ + A$ WEND L = LEN(S$) FOR I = 1 TO L / 2 IF MID$(S$, I, 1) <> MID$(S$, L - I + 1, 1) THEN PRINT "NOT A PALINDROME": END END IF NEXT I PRINT "A PALINDROME" '2.8 ' This program will display the frequency of letters. ' DIM A(26): INPUT "Enter sentence:"; A$: L = LEN(A$) FOR I = 1 TO L C$ = MID$(A$, I, 1) IF C$ >= "A" AND C$ <= "Z" THEN X = ASC(C$) - ASC("A") + 1: A(X) = A(X) + 1: T = T + 1 END IF NEXT I PRINT "LETTER", "FREQUENCY", "PERCENT" FOR I = 1 TO 26 IF A(I) > 0 THEN PRINT CHR$(I + 64), A(I), INT(A(I) / T * 100 + .5) END IF NEXT I PRINT "TOTAL"; T '2.9 ' This program will print longest word in sentence. ' INPUT "Enter sentence:"; A$: A$ = A$ + " ": L = LEN(A$) FOR I = 1 TO L C$ = MID$(A$, I, 1) IF C$ <> " " THEN W$ = W$ + C$ ELSE IF LEN(W$) > LEN(MAX$) THEN MAX$ = W$ W$ = "" END IF NEXT I PRINT MAX$ '2.10 ' This program will play rock, scissors, and paper. ' RANDOMIZE TIMER INPUT "Enter R, S, P, or Q: "; A$ WHILE A$ <> "Q" X = INT(RND(3) * 3) IF X = 0 AND A$ = "R" THEN T = T + 1: PRINT "TIE" ELSEIF X = 1 AND A$ = "S" THEN T = T + 1: PRINT "TIE" ELSEIF X = 2 AND A$ = "P" THEN T = T + 1: PRINT "TIE" ELSEIF X = 0 AND A$ = "P" THEN W = W + 1: PRINT "YOU WIN" ELSEIF X = 1 AND A$ = "R" THEN W = W + 1: PRINT "YOU WIN" ELSEIF X = 2 AND A$ = "S" THEN W = W + 1: PRINT "YOU WIN" ELSE L = L + 1: PRINT "I WIN" END IF INPUT "Enter R, S, P, or Q: "; A$ WEND PRINT T; "TIES" PRINT W; "WINS (YOURS)" PRINT L; "LOSSES (MINE)" '3.1 ' This program will display a random trail of asterisks. ' The program description is poorly worded and ambiguous. ' The judging criteria is also described poorly. ' RANDOMIZE TIMER DIM A(24, 80) DO CLS : FOR I = 1 TO 24: FOR J = 1 TO 80: A(I, J) = 0: NEXT J, I V = 12: H = 40: A(V, H) = 1 LOCATE V, H: PRINT "S": SAMERUN = -1 WHILE SAMERUN DO X = INT(RND(3) * 4) LOOP UNTIL (X - 2 <> Y) AND (Y - 2 <> X) H = H + (X = 0) - (X = 2) V = V + (X = 1) - (X = 3) IF A(V, H) OR V = 0 OR V = 23 OR H = 0 OR H = 80 THEN LOCATE 22, 1: PRINT "THE MAXIMUM DISTANCE FROM START = "; PRINT ABS(40 - H) + ABS(12 - V) DO: A$ = INKEY$: LOOP UNTIL A$ <> "" SAMERUN = 0 ELSE A(V, H) = 1: LOCATE V, H: PRINT "*": Y = X END IF WEND LOOP UNTIL A$ = "Q" '3.2 ' This program will decode a message with frequent letters. ' DIM A$(32), A(32), B$(32) B$ = "ETAOINSHRDLU" INPUT "Message: "; A$: L = LEN(A$) FOR I = 1 TO L: A$(I) = MID$(A$, I, 1): NEXT I FOR I = 1 TO L K = 0 WHILE (A$(K) <> A$(I)) AND (K <= I - 1) K = K + 1 WEND ' Found 1st occurence of a letter, count occurences after it IF K = I THEN FOR J = I TO L IF A$(I) = A$(J) THEN A(I) = A(I) + 1 NEXT J IF A(I) > G THEN G = A(I) END IF NEXT I ' Replace letters in message FOR I = G TO 1 STEP -1 J = 1 WHILE (A(J) <> I) AND (J <= L) J = J + 1 WEND IF J <= L THEN S = S + 1 FOR K = J TO L IF A$(K) = A$(J) THEN B$(K) = MID$(B$, S, 1) NEXT K END IF NEXT I FOR I = 1 TO L: PRINT B$(I); : NEXT I '3.3 ' This program will produce the digital product root. ' INPUT "ORIGINAL VALUE (1 TO 7 DIGITS): "; N PRINT N WHILE N > 9 A$ = MID$(STR$(N), 2): L = LEN(A$): N = 1 FOR I = 1 TO L X = VAL(MID$(A$, I, 1)) IF X > 0 THEN N = N * X NEXT I PRINT N WEND '3.4 ' This program will display twin primes. ' INPUT "Enter N: "; N PRINT "TWIN PRIMES NOT GREATER THAN"; N FOR I = 3 TO N - 2 FOR J = 2 TO SQR(I) IF I MOD J = 0 THEN GOTO NextPrime NEXT J T = I + 2 FOR J = 2 TO SQR(T) IF T MOD J = 0 THEN GOTO NextPrime NEXT J: PRINT I, T NextPrime: NEXT I '3.5 ' This program will print subsets of m people. ' DIM A(26), A$(26): INPUT "INPUT NUMBER, CAPACITY"; L, M FOR I = 1 TO M: A(I) = M - I + 1: NEXT I FOR I = 1 TO L: A$(I) = CHR$(64 + I): NEXT I N = 1: A(1) = A(1) - 1 WHILE N <= M A(N) = A(N) + 1 IF N > 1 THEN FOR I = N - 1 TO 1 STEP -1: A(I) = A(I + 1) + 1: NEXT I END IF IF A(N) <= L - N + 1 THEN FOR I = M TO 1 STEP -1: PRINT A$(A(I)); : NEXT I: PRINT , S = S + 1: N = 0 END IF N = N + 1 WEND PRINT : PRINT "THERE ARE"; S; "SUBSETS" '3.6 ' This program will display histogram of letter frequency. ' DIM A(256): CLS DATA "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG." DATA "THIS IS AN EXAMPLE OF HOW" DATA "TO TEST YOUR HISTOGRAM PROGRAM. YOU" DATA "CAN USE THIS EXAMPLE." DATA "*END*" READ A$ WHILE A$ <> "*END*" FOR I = 1 TO LEN(A$) X = ASC(MID$(A$, I, 1)) IF X >= 65 AND X <= 90 THEN A(X) = A(X) + 1 IF A(X) > G THEN G = A(X) NEXT I READ A$ WEND FOR I = G TO 1 STEP -1 FOR J = 65 TO 90 IF A(J) >= I THEN LOCATE G - I + 1, J - 64: PRINT "*"; END IF NEXT J: PRINT NEXT I FOR I = 65 TO 90: PRINT CHR$(I); : NEXT I '3.7 ' This program will display a repeating decimal. ' DIM R(100) INPUT "Enter N, D: "; N, D PRINT N; "/"; D; "= "; : X = INT(N / D) IF X > 0 THEN PRINT MID$(STR$(X), 2); PRINT "."; DO I = I + 1: R = N - D * X IF R = 0 THEN PRINT A$: END R(I) = R: N = R * 10: X = INT(N / D) ' Display decimal if remainder repeats itself FOR J = 1 TO I - 1 IF R(J) = R THEN PRINT LEFT$(A$, J - 1); "("; RIGHT$(A$, I - J); ")": END END IF NEXT J A$ = A$ + MID$(STR$(X), 2) LOOP '3.8 ' This program will print # of round numbers less than N. ' INPUT "INPUT NUMBER"; N FOR I = 2 TO N M = I: S = 0: K = INT(LOG(M) / LOG(2)) FOR J = K TO 0 STEP -1 X = INT(M / 2 ^ J) S = S + X: M = M - X * 2 ^ J NEXT J IF S + S = K + 1 THEN T = T + 1 NEXT I PRINT "THERE ARE"; T; "ROUND NUMBERS LESS THAN OR EQUAL TO"; N '3.9 ' This program will provide automated price increases. ' INPUT "Enter %"; P: P = P / 100 READ A$ WHILE A$ <> "*END*" L = LEN(A$): I = 0 DO WHILE (I < L) AND (X$ <> "$") I = I + 1: X$ = MID$(A$, I, 1): PRINT X$; WEND IF X$ <> "$" THEN PRINT ELSE J = I: X = 50 ' WHILE (J < L) AND (X$ = "." OR (X > 47 AND X < 58)) AND PER < 2 J = J + 1: X$ = MID$(A$, J, 1): X = ASC(X$) IF X$ = "." THEN PER = PER + 1 WEND ' T = VAL(MID$(A$, I + 1, J - I)) T = T + T * P: T = INT(T * 100 + .5) / 100 PRINT MID$(STR$(T), 2); I = J - 1: PER = 0 END IF LOOP UNTIL I >= L READ A$ WEND DATA "THE CURRENT COST OF BUCKLES IS" DATA "3 FOR $2.50, OR $10.00 A DOZEN." DATA "*END*" '3.10 ' This program will simulate tennis sets between 2 players. ' RANDOMIZE TIMER INPUT "NUMBER OF SETS ="; N INPUT "% CHANCE A WINS A POINT="; P DO IF RND(2) < P / 100 THEN A = A + 1 ELSE B = B + 1 IF A > 3 AND A > B + 1 THEN PRINT "A"; : AG = AG + 1: A = 0: B = 0 END IF IF B > 3 AND B > A + 1 THEN PRINT "B"; : BG = BG + 1: A = 0: B = 0 END IF IF AG > 5 AND AG > BG + 1 THEN PRINT " (A)": W = W + 1: AG = 0: BG = 0 END IF IF BG > 5 AND BG > AG + 1 THEN PRINT " (B)": L = L + 1: AG = 0: BG = 0 END IF LOOP UNTIL W + L = N IF W > L THEN PRINT "PLAYER 'A' WON"; W; "SETS OUT OF"; N: END PRINT "PLAYER 'B' WON"; L; "SETS OUT OF"; N