Tutorial 029 Algebra Practice
This program illustrates how to give youngsters/oldsters some practice manipulating
simple equations :
REM: Algebra Practice
REM: Richard Weston 31.05.03
REM: after Alan Thomas
MODE0
R$="Right"
W$="Wrong"
@%=4
REPEAT
x=RND(20)-10
REPEAT
m=RND(10)-5
UNTIL m<>0
c=RND(10)-5
y=(m*x)+c
PRINT'"If ";
flag=0
CASE m OF
WHEN 1: flag=1
WHEN -1: flag=1
PRINT"
-";
ENDCASE
IF flag=0 THEN PRINT m;
PRINT "x";
IF c<>0 THEN
IF c>0 PRINT "+";ELSE
PRINT"-";
PRINT STR$(ABS(c));
ENDIF
PRINT" = ";y
REPEAT
INPUT'"what is x",A$
UNTIL A$<>""
XA=EVAL(A$)
IF XA=x PRINT' R$ ELSE PRINT'
W$
PRINT'" x is ";x
PRINT STRING$(39,"-")
UNTIL FALSE
Here is an amazingly unlikely series of questions
and answers :
If -4x-4 = -36
what is x? 8
Right
x is 8
---------------------------------------
If -4x-4 = -32
what is x? 7
Right
x is 7
---------------------------------------
If -4x-4 = 32
what is x? -7
Wrong
x is -9
---------------------------------------
Note that because EVAL is used in the program answers
other than the fully calculated one could be input:
---------------------------------------
If -2x+2 = 20
what is x? (20-2)/-2
Right
x is -9
---------------------------------------
If 4x+2 = 34
what is x? 32/4
Right
x is 8
---------------------------------------
If 3x-3 = 15
what is x? (15+3)/3
Right
x is 6
---------------------------------------
However, if you can do that you can probably go all the way.
Annotated Listing
REM: Algebra Practice
REM: Richard Weston 31.05.03
REM: after Alan Thomas
MODE0
R$="Right"
W$="Wrong"
@%=4 Print formatting
REPEAT
x=RND(20)-10
REPEAT
m=RND(10)-5
UNTIL m<>0
c=RND(10)-5
y=(m*x)+c Equation of a straight line
PRINT'"If ";
flag=0
Here we don't want m = 1 or m = -1 values to print
the 1 (in conventional algebraic notation)
CASE m OF
WHEN 1: flag=1
WHEN -1: flag=1
PRINT"
-";
ENDCASE
..................................................................................................................
IF flag=0 THEN PRINT m;
PRINT "x";
Similarly we don't want a c=0 value to be printed
IF c< >0 THEN
IF c>0 PRINT "+";ELSE
PRINT"-";
PRINT STR$(ABS(c));
Use ABS so a minus value does not print the minus
sign again
Use STR$ to convert the absolute value of the number c to a string
ENDIF
PRINT" = ";y
REPEAT
INPUT'"what is x",A$
UNTIL A$< >""
Avoids an error message if you hit the enter key on
its own
XA=EVAL(A$)
Means you can enter an algebraic expression if desired
IF XA=x PRINT' R$ ELSE PRINT'
W$
PRINT'" x is ";x
PRINT STRING$(39,"-")
Draws a line across
UNTIL FALSE
Keeps giving you questions patiently for ever so you
can get really good!
Exercise
Modify the program to repeat each question until you get the right answer
without telling you the answer until you give up.
Then send in your solution for publication....
Next Tutorial
Richard Weston's Homepage