FLORIDA HIGH SCHOOLS COMPUTING COMPETITION '82
BASIC PROGRAM SOLUTIONS
 
 
'1.1
' This program will allow a user to guess a generated #.
'
RANDOMIZE TIMER
X = INT(RND(3) * 100) + 1:  I = 1
WHILE (I <= 7) AND (G <> X)
  INPUT "I AM THINKING OF A NUMBER. WHAT IS IT"; G
  IF G < X THEN PRINT "TOO LOW"
  IF G > X THEN PRINT "TOO HIGH"
  IF G = X THEN PRINT "RIGHT"
  I = I + 1
WEND
PRINT "NUMBER IS "; X
 
 
'1.2
' This program will find #s that are the sum of 2 squares.
'
DIM A(100)
FOR I = 1 TO 5
  FOR J = I TO 7
    A(I * I + J * J) = -1
  NEXT J
NEXT I
FOR I = 1 TO 49
  IF A(I) THEN PRINT LTRIM$(STR$(I)); ",";
NEXT I: PRINT
 
 
'1.3
' This program will sum numbers divisible by 14.
'
FOR I = 100 TO 1000
  IF I MOD 14 = 0 THEN S = S + I
NEXT I
PRINT S
 


'1.4
' This program will add 2 random clock times.
'
RANDOMIZE TIMER
FOR I = 1 TO 2
  H(I) = INT(RND(3) * 12) + 1
  M(I) = INT(RND(3) * 60)
  IF H(I) < 10 THEN PRINT " ";
  PRINT H(I); ": ";
  IF M(I) < 10 THEN PRINT " 0";
  PRINT LTRIM$(STR$(M(I)))
NEXT I
PRINT "--------"
M = M(1) + M(2)
IF M > 59 THEN M = M - 60: H = 1
H = H + H(1) + H(2): IF H > 12 THEN H = H - 12
IF H < 10 THEN PRINT " ";
PRINT H; ": ";
IF M < 10 THEN PRINT "0";
PRINT LTRIM$(STR$(M)): PRINT
 
 
'1.5
' This program will compute roots of equation.
'
INPUT "Enter a, b, c: "; A, B, C
S = B * B - 4 * A * C
IF S < 0 THEN PRINT "COMPLEX": END
PRINT (-B + SQR(S)) / (2 * A);
PRINT (-B - SQR(S)) / (2 * A)
 
'1.6
' This program will print prime factors.
'
INPUT "Enter number: "; N
FOR I = 2 TO N
  IF N MOD I = 0 THEN
    IF I = 2 THEN
      PRINT 2;
    ELSE
      J = 1
      DO
        J = J + 1
      LOOP UNTIL J > SQR(I) OR (I MOD J = 0)
      IF I MOD J > 0 THEN PRINT I;
    END IF
  END IF
NEXT I: PRINT
 


'1.7
' This program will calculate future value of investment.
'
INPUT "Enter P, i, N, Y: "; P, I, N, Y
' P * (1 + I/N)^(N*Y)
FOR J = 1 TO N * Y
  P = P + P * I / N
NEXT J
PRINT "$"; INT(P * 100 + .5) / 100
 
 
 
'1.8
' This program will find 3 #s whose sum is 43.
'
FOR I = 1 TO 41
  FOR J = 1 TO 42 - I
    K = 43 - I - J
    IF I * I * I + J * J * J + K * K * K = 17299 THEN
      PRINT I; J; K: END
    END IF
  NEXT J
NEXT I
 
 
 
'1.9
' This program will print a symbol for 45 seconds.
'
INPUT "Enter a symbol: "; A$
CLS : PRINT A$
FOR I = 1 TO 45
   FOR J = 1 TO 3600: NEXT J  'About one second on 386
NEXT I
CLS
 
 
 
'1.10
' This program will convert decimal to fraction
'
INPUT "Enter decimal: "; N$
L = LEN(N$) - 1
N = VAL(RIGHT$(N$, L))
D = 1
FOR I = 1 TO L: D = D * 10: NEXT I
FOR I = N TO 1 STEP -1
  IF N MOD I = 0 AND D MOD I = 0 THEN
    PRINT N / I; "/"; D / I: END
  END IF
NEXT I
 


'1.11
' This program will move an asterisk by pressing keys.
'
CLS : R = 10: C = 40
LOCATE R, C: PRINT "*"
WHILE A$ <> " "
  DO: A$ = INKEY$: LOOP UNTIL A$ <> ""
  CLS
  IF A$ = "U" THEN R = R - 1
  IF A$ = "D" THEN R = R + 1
  IF A$ = "L" THEN C = C - 1
  IF A$ = "R" THEN C = C + 1
  LOCATE R, C: PRINT "*"
WEND
 
 


'2.1
' This program will print day of week of a date.
' January 1 was a Friday (in 1982).
'
DIM M(12)
DATA 31,28,31,30,31,30,31,31,30,31,30,31
FOR I = 1 TO 12: READ M(I): NEXT I
INPUT "Enter month, day: "; MO, DA
IF MO > 1 THEN
  FOR I = 1 TO MO - 1: S = S + M(I): NEXT I
END IF
S = S + DA
X = S - INT(S / 7) * 7
A$ = "THUFRISATSUNMONTUEWED"
PRINT MID$(A$, X * 3 + 1, 3)
 
 
'2.2
' This program will calculate the area of a polygon.
'
INPUT "Enter n: "; N
FOR I = 1 TO N
  INPUT "Enter vertex (X, Y): "; X(I), Y(I)
NEXT I
X(N + 1) = X(1): Y(N + 1) = Y(1)
FOR I = 1 TO N
  SUM = SUM + X(I) * Y(I + 1) - Y(I) * X(I + 1)
NEXT I
PRINT "AREA ="; ABS(SUM) / 2
 
 
'2.3
' This program will find 5 digit number.
'Strategy: # is less than 25000 because 4 * # would be
'          a 6 digit # otherwise.
'          # can't be 1XXXY since 4 * Y can't give us
'          a 1 in the units place.
'          # must therefore begin with 2 and end with
'          8 since 4*8 = 32.  So we can step 10.
'
FOR I = 20008 TO 24998 STEP 10
  N$ = MID$(STR$(I), 2, 5):  S$ = MID$(STR$(I * 4), 2, 5)
  J = 0
  DO
    J = J + 1
  LOOP UNTIL (J = 5) OR (MID$(N$, J, 1) <> MID$(S$, 6 - J, 1))
  IF MID$(N$, J, 1) = MID$(S$, 6 - J, 1) THEN PRINT I: END
NEXT I
 


'2.4
' This program will find interesting numbers.
'
FOR I = 1 TO 9
  FOR J = 0 TO 9
    FOR K = 0 TO 9
      NUM = I * 100 + J * 10 + K
      POW = I * I * I + J * J * J + K * K * K
      IF (NUM = POW) AND (NUM <> 153) THEN
        PRINT I * 100 + J * 10 + K;
      END IF
    NEXT K
  NEXT J
NEXT I: PRINT
 
 
'2.5
' This program will make user's name zigzag.
'
INPUT "Enter name: "; NAM$:  L = LEN(NAM$)
CLS
X = INT(159 / (L - 1))
FOR I = 1 TO L
  M$ = MID$(NAM$, I, 1)
  S = (I - 1) * X: IF S > 79 THEN S = 159 - S
  PRINT TAB(S); M$
NEXT I
 
 
'2.6
' This program will print a stick figure.
'
R = 5: C = 12
DO
  FOR I = 0 TO 5
    CLS
    PRINT " *     ***** "
    PRINT "  *    *   * "
    PRINT "   *   ***** "
    PRINT "    **   *   "
    PRINT "      *****  "
    PRINT "         *   "
    PRINT "         *   "
    PRINT "        * *  "
    PRINT "       *   * "
    PRINT "      *     *"
    INC = (R - I) / 7
    FOR K = 0 TO 6
      LOCATE R - INC * K, C + K: PRINT "*"
    NEXT K
  NEXT I
  A$ = INKEY$
LOOP UNTIL A$ = CHR$(27)
 

'2.7
' This program will display permutations of letters.
'
RANDOMIZE TIMER
INPUT "How many letters: "; N
FOR I = 1 TO N: INPUT "Enter letter: "; A$(I): NEXT I
DO
  FOR I = 1 TO N
    X = INT(RND(5) * N + 1)
    T$ = A$(X): A$(X) = A$(I): A$(I) = T$
  NEXT I
  FOR I = 1 TO N: PRINT A$(I); : NEXT I: PRINT
  A$ = INKEY$
LOOP UNTIL A$ = CHR$(27)
 
 
'2.8
' This program will drill typing skills.
'
RANDOMIZE TIMER
FOR I = 1 TO 4
  X = INT(RND(3) * 58) + 33
  A$(I) = CHR$(X): PRINT A$(I); " ";
NEXT I: PRINT : J = 1
WHILE J < 5
   DO: S = S + 1: A$ = INKEY$: LOOP UNTIL A$ <> ""
   B$(J) = A$: PRINT B$(J); " ";
   J = J + 1
WEND
'
PRINT : PRINT
FOR I = 1 TO 4
  IF A$(I) <> B$(I) THEN PRINT A$(I), B$(I), "NO": C = 1
NEXT I
IF C = 0 THEN PRINT INT(S / 3000); " SECONDS"
 
 
'2.9
' This program will return change in fewest coins.
'
DATA $20,2000,$10,1000,$5,500,DOLLARS,100
DATA QUARTERS,25,DIMES,10,NICKELS,5,PENNIES,1
INPUT "Enter price $:"; P
INPUT "Enter denomination $:"; D
N = (D - P) * 100
FOR I = 1 TO 8
  READ A$, A
  X = INT(N / A)
  IF X > 0 THEN PRINT X; " "; A$
  N = INT(N - X * A + .01)
NEXT I
 


'2.10
' This program will make unit conversions.
'
DATA IN,CM,FT,CM,FT,M,YD,M,MI,KM
FOR I = 1 TO 5
  READ A$, B$: PRINT I; "  "; A$; "-> "; B$
NEXT I: RESTORE
INPUT "Enter Choice #: "; X
FOR I = 1 TO X: READ A$, B$: NEXT I
PRINT "Enter "; A$; : INPUT N
S = N * 2.54
IF X = 1 THEN PRINT S;
IF X = 2 THEN PRINT S * 12;
IF X = 3 THEN PRINT S * 12 / 100;
IF X = 4 THEN PRINT S * 36 / 100;
IF X = 5 THEN PRINT S * 5280 * 12 / 100000!;
PRINT " "; B$
 
 
'2.11
' This program will find A^B x C^D = ABCD.
'
FOR A = 1 TO 9
  FOR B = 0 TO 9
    FOR C = 0 TO 9
      FOR D = 0 TO 9
        APOW = 1: CPOW = 1
        FOR J = 1 TO B: APOW = APOW * A: NEXT J
        FOR J = 1 TO D: CPOW = CPOW * C: NEXT J
        NUM = A * 1000 + B * 100 + C * 10 + D
        IF APOW * CPOW = NUM THEN
          PRINT "A="; A; " B="; B; " C="; C; " D="; D: END
        END IF
      NEXT D
    NEXT C
  NEXT B
NEXT A
 
'2.12
' This program calculates days between 2 dates.
'
DATA 31,28,31,30,31,30,31,31,30,31,30,31
INPUT "Enter Month1, Day1: "; M1, D1
INPUT "Enter Month2, Day2: "; M2, D2
M = M2 - M1
IF M > 0 THEN
  IF M1 > 1 THEN
    FOR I = 1 TO M1 - 1: READ D: NEXT I
  END IF
  FOR I = M1 TO M2 - 1
    READ D: S = S + D
  NEXT I
END IF
PRINT S + D2 - D1; "DAYS"
 
'2.13
' This program will print a check.
'
DATA JAN.,FEB.,MAR.,APRIL,MAY,JUNE,JULY,AUG.,SEPT.
DATA OCT.,NOV.,DEC.
DATA ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE
DATA TEN,ELEVEN,TWELVE,THIRTEEN,FOURTEEN,FIFTEEN
DATA SIXTEEN,SEVENTEEN,EIGHTEEN,NINETEEN
DATA TWENTY-,THIRTY-,FOURTY-,FIFTY-,SIXTY-,
DATA SEVENTY-,EIGHTY-,NINETY-
INPUT "Enter month, day, year: ", M, D$, Y$
INPUT "Enter amount $"; N
INPUT "Enter payee: "; N$
'    Display check border
CLS
FOR I = 1 TO 60: PRINT "*"; : NEXT I: PRINT
FOR I = 1 TO 7
  LOCATE I + 1, 1: PRINT "*": LOCATE I + 1, 60: PRINT "*"
NEXT I
FOR I = 1 TO 60: PRINT "*"; : NEXT I: PRINT
'    Display date
FOR I = 1 TO M: READ M$: NEXT I
LOCATE 2, 45: PRINT M$; " "; D$; ", 19"; Y$
IF M < 12 THEN
  FOR I = M + 1 TO 12: READ M$: NEXT I
END IF
'    Display Name and amount
LOCATE 4, 5: PRINT "PAY TO THE"
LOCATE 5, 5: PRINT "ORDER OF  "; N$
LOCATE 5, 50: PRINT "$"; N
LOCATE 7, 3
'    Display amount in words
CENT = INT((N - INT(N)) * 100 + .01): S = 1000
FOR I = 2 TO 0 STEP -1
  S = S / 10:  X = INT(N / S + .001)
  IF (I = 2) AND (X > 0) THEN
    FOR J = 1 TO X: READ E$: NEXT J
    PRINT E$; " HUNDRED ";
  END IF
  IF (I = 1) AND (X > 1) THEN
    FOR J = 1 TO 18 + X: READ E$: NEXT J
    PRINT E$;
  END IF
  IF I = 1 AND X = 1 THEN T = 1
  IF I = 0 THEN
    FOR J = 1 TO T * 10 + X: READ E$: NEXT
    PRINT E$;
  END IF
  RESTORE: FOR K = 1 TO 12: READ X$: NEXT K
  N = INT(N - X * S + .001)
NEXT I
PRINT " AND"; CENT; "/100 DOLLARS"
 
 


'3.1
' This program  will play mastermind.
'
RANDOMIZE TIMER
CLS : LOCATE 1, 5: PRINT "GUESS: W, Y, R, G, BL, BK"
DATA W,Y,R,G,BL,BK
'   Assign random colors
FOR I = 1 TO 4
  X = INT(RND(6) * 6) + 1
  FOR J = 1 TO X: READ A$(I): NEXT J: RESTORE
NEXT I
'    Allow user 10 sets of guesses
FOR K = 1 TO 10
  W = 0: B = 0
  FOR I = 1 TO 4: LOCATE K * 2, I * 6: INPUT B$(I): NEXT I
  FOR I = 1 TO 4: C$(I) = A$(I): NEXT I
  FOR I = 1 TO 4
    IF C$(I) = B$(I) THEN B = B + 1: B$(I) = "": C$(I) = " "
  NEXT I
  FOR I = 1 TO 4
    FOR J = 1 TO 4
       IF C$(I) = B$(J) THEN W = W + 1: B$(J) = "": C$(I) = " 
"
    NEXT J
  NEXT I
'      Black pegs = Correct color and correct position
'      White pegs = Correct color but wrong position
  LOCATE K * 2, 40: PRINT "BLACKS = "; B; "   WHITES ="; W
  IF B = 4 THEN PRINT "YOU WIN IN "; K; " TURNS": END
NEXT K
PRINT "YOU LOSE"
FOR I = 1 TO 4: PRINT A$(I); " "; : NEXT I
 
'3.2
' This program will plot points on a new axis.
'
CLS
INPUT "Enter end point of x-axis: "; X1, Y1
INPUT "Enter end point of y-axis: "; X2, Y2
INPUT "Enter increment: "; IT
INPUT "How many points: "; N
FOR I = 1 TO N
  INPUT "Enter point: "; X(I), Y(I)
NEXT I
CLS : R = 3: C = 1
PRINT "INTERSECTION AT ("; X2; ","; Y1; ")"
PRINT
FOR I = Y1 TO Y2 STEP IT:   PRINT "*"; : NEXT I: PRINT
FOR I = X2 + 1 TO X1 STEP IT: PRINT "*": NEXT I
FOR I = 1 TO N
  LOCATE R + (X(I) - X2) / IT, C + (Y(I) - Y1) / IT: PRINT "+"
NEXT I
 


'3.3
' This program will generate magic squares.
' -- Correct for odd sizes and 4x4.
'
CLS
INPUT "Enter size: "; N:  DIM A(N, N): PRINT
IF N MOD 2 > 0 THEN
  X = 1: Y = (N + 1) / 2: A(X, Y) = 1
'   Routine for Odd matrix
  FOR I = 2 TO N * N
    X = X - 1: Y = Y - 1
    IF X = 0 THEN X = N
    IF Y = 0 THEN Y = N
    IF A(X, Y) = 0 THEN
      A(X, Y) = I
    ELSE
      X = X + 2: Y = Y + 1
      IF X > N THEN X = X - N
      IF Y > N THEN Y = 1
      A(X, Y) = I
    END IF
  NEXT I
ELSE
'  Routine for Even matrix
  FOR I = 1 TO N
    FOR J = 1 TO N
      S = S + 1
      IF I = J OR I = N + 1 - J THEN
        A(I, J) = S
      ELSE
        A(I, J) = N * N + 1 - S
      END IF
   NEXT J
 NEXT I
END IF
FOR I = 1 TO N
  FOR J = 1 TO N
    LOCATE I * 2, J * 4: PRINT A(I, J)
  NEXT J
NEXT I
PRINT : PRINT "MAGIC NUMBER="; (N * N * N + N) / 2
 


'3.4
' This program will add and multiply 2 Roman Numerals.
'
DATA M,1000,D,500,C,100,L,50,X,10,V,5,I,1
FOR I = 1 TO 7: READ RN$(I), RV(I): NEXT I
FOR E = 1 TO 2
  INPUT "Enter Roman Numeral: "; ROM$(E)
  L = LEN(ROM$(E)): I = 1: AR = 0
  WHILE I < L
    C$ = MID$(ROM$(E), I, 1)
    I1 = 1: WHILE C$ <> RN$(I1): I1 = I1 + 1: WEND
    NC$ = MID$(ROM$(E), I + 1, 1)
     I2 = 1: WHILE NC$ <> RN$(I2): I2 = I2 + 1: WEND
     IF I1 <= I2 THEN
       AR = AR + RV(I1)
     ELSE
       AR = AR + RV(I2) - RV(I1): I = I + 1
     END IF
     I = I + 1
   WEND
   IF I <= L THEN
'       Last numeral was not done
     C$ = MID$(ROM$(E), I, 1)
     I1 = 1: WHILE C$ <> RN$(I1): I1 = I1 + 1: WEND
     AR = AR + RV(I1)
   END IF
   A(E) = AR
NEXT E
'    Convert Arabic numbers to Roman Numerals
N(1) = A(1) + A(2): N(2) = A(1) * A(2)
FOR K = 1 TO 2: NUM = N(K)
  FOR I = 1 TO 7
    X = NUM / RV(I)
    BOOL = (X < 2) AND (X >= 9 / 5)
    BOOL = BOOL AND ((I = 2) OR (I = 4) OR (I = 6))
    IF NOT BOOL THEN
      XX = INT(X)
      IF XX = 9 THEN
        R$(K) = R$(K) + RN$(I) + RN$(I - 2)
      ELSEIF XX = 4 THEN
        R$(K) = R$(K) + RN$(I) + RN$(I - 1)
      ELSEIF XX > 0 THEN
        FOR J = 1 TO XX: R$(K) = R$(K) + RN$(I): NEXT
      END IF
      NUM = NUM - RV(I) * XX
    END IF
  NEXT I
NEXT K
'     Display sum and product
PRINT ROM$(1); " + "; ROM$(2); " = "; R$(1)
PRINT A(1); "+"; A(2); "="; N(1)
PRINT
PRINT ROM$(1); " * "; ROM$(2); " = "; R$(2)
PRINT A(1); "*"; A(2); "="; N(2)
 
'3.5
' This program will find 4 digit squumbers.
'
FOR I = 1000 TO 9999
  L = INT(I / 100)
  R = I MOD 100
  X = L + R
  IF X * X = I THEN PRINT I
NEXT I
 
'3.6
' Write a program to play NIM with a user.
' Since the rules are not given with this problem,
' it is very difficult to write the program.
 
 
'3.7
' This program will determine where a # falls in a list.
'
DIM A(16)
FOR I = 1 TO 16
  INPUT "Enter #: "; A(I)
NEXT I
INPUT "Enter another number: "; NUM
I = 1
WHILE A(I) <> NUM: I = I + 1: WEND
PRINT "BETWEEN"; A(I - 1); "AND"; A(I + 1)
 
 
'3.8
' This BONUS program will guess the user's state.
'
DATA ALABAMA,ALASKA,ARIZONA,ARKANSAS,CALIFORNIA
DATA COLORADO,CONNECTICUT,DELEWARE,FLORIDA,GEORGIA
DATA HAWAII,IDAHO,ILLINOIS,INDIANA,IOWA,KANSAS
DATA KENTUCKY,LOUISIANA,MAINE,MARYLAND,MASSACHUSETTS
DATA MICHIGAN,MINNESOTA,MISSISSIPPI,MISSOURI,MONTANA
DATA NEBRASKA,NEVADA,NEW HAMPSHIRE,NEW JERSEY,NEW YORK
DATA NEW MEXICO,NORTH CAROLINA,NORTH DAKOTA,OHIO,OKLAHOMA
DATA OREGON,SOUTH CAROLINA,SOUTH DAKOTA,PENNSYLVANIA
DATA RHODE ISLAND,TENNESSEE,TEXAS,UTAH,VERMONT,VIRGINIA
DATA WASHINGTON,WEST VIRGINIA,WISCONSIN,WYOMING
DIM ST$(50)
FOR I = 1 TO 50: READ ST$(I): NEXT I
G = 1: B = 1: M = 25: E = 50
DO
  PRINT G; "- IS YOUR STATE ALPHABETICALLY BEFORE "; ST$(M)
  INPUT "Enter YES or NO: "; A$
  IF A$ = "YES" AND B + 1 = M THEN PRINT ST$(B); " IS IT": END
  IF A$ = "NO" AND M = E THEN PRINT ST$(M); " IS IT": END
  IF A$ = "YES" THEN E = M - 1: M = M - INT((M - B) / 2 + .5)
  IF A$ = "NO" THEN B = M:  M = M + INT((E - M) / 2 + .5)
  G = G + 1
LOOP UNTIL G > 12