Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 42 |
Nodes: | 6 (0 / 6) |
Uptime: | 00:55:47 |
Calls: | 220 |
Calls today: | 1 |
Files: | 824 |
Messages: | 121,521 |
Posted today: | 6 |
# algebra.awk: 2024 - Michael Sanders
#
# usage: awk -f algebra.awk > solve.txt
[...]
Hi Mike,
is my guess correct that you want to create linear equation samples
to be printed (on paper) and solved? Or is it meant as a programming
course sample? - My suggestions depend on being one or the other...
[...]
Mike Sanders <porkchop@invalid.foo> wrote:
# algebra.awk: 2024 - Michael Sanders
#
# usage: awk -f algebra.awk > solve.txt
[...]
# subtle tweak: every equation unique (no duplicates)...
BEGIN {
srand() # seed the random number generator
# keep generating until we have exactly 100 unique equations
while (u < 100) {
a = int(rand() * 20) + 1 # random value for 'a' (1 to 20)
b = int(rand() * 20) + 1 # random value for 'b' (1 to 20)
c = int(rand() * 50) + 1 # random value for 'c' (1 to 50)
opc = (rand() < 0.5 ? "*" : "/") # random operator
lhs = sprintf("%dx %s %d", a, opc, b) # left-hand side
rhs = c # right-hand side
equ = lhs " = " rhs # full equation
# store equation in array if it doesn't already exist
if (!(equ in equations)) {
equations[equ] = 1 # mark element as 'reserved'...
u++ # increment u for each unique equation
}
}
# print equations
for (e in equations) printf("%03d. %s\n\n\n\n\n\n\n\n\n", ++q, e)
}
# eof
Hi Mike,
is my guess correct that you want to create linear equation samples
to be printed (on paper) and solved? Or is it meant as a programming
course sample? - My suggestions depend on being one or the other...
I've learned linear equations to contain an additive term, as in
a x + b = c (i.e. a*x + b == c written as Awk expression),
so I'd have expected the operator to be '+' or '-' (not '*' or '/'). (Otherwise, with a*x * b , you could just calculate a*b first and
a/b respectively, in case of a division a*x / b, before then doing
the single final lhs/rhs operation. The "both sides" procedures
you describe in your introductory comment would be unnecessarily
complicate if you really meant * and / .)
I wonder about the many temporary variables and technical comments;
most don't contribute to legibility or clearness and are unnecessary.
There could be used better naming for the remaining fewer variables.
It could gain from more structuring, like using a 'random' function
for integers to make the random expressions simpler.
Control structure could be simplified, made clearer; do { } while . Re-iterating over the stored equations is unnecessary, you can just
print them.
(I've added code reflecting these suggestions at the end of my post
in case you'd like to pick an idea or two. I've also changed a few
more details, just in case you wonder about any differences to the
original code.)
function rnd (n) # n -> 1..n
{
return int(rand() * n) + 1
}
BEGIN {
srand()
while (++serial_number <= 100) {
do {
opc = rand() < 0.5 ? "+" : "-" # choose random operator
equ = sprintf("%d x %c %d = %d", rnd(20), opc, rnd(20), rnd(50))
} while (equ in equations_store) # avoid duplicates
equations_store [equ] # memorize generated equation
printf("%3d.\t%s\n", serial_number, equ)
}
}
[...]
But I do wonder about: 5x vs. 5 * x or even (5 * x)... I've read so many opinions on this matter. If there an offical standard? I dont know.
One older book I have (from 1917!) has 1-2 paragraphs saying 5x without
an intervening * is very bad form & yet, everybody seems to use it, at
least here the USA.
[...]
porkchop@invalid.foo (Mike Sanders) writes:
[...]
One older book I have (from 1917!) has 1-2 paragraphs saying 5x without[...]
an intervening * is very bad form & yet, everybody seems to use it, at
least here the USA.
Can you identify the book? It's likely to be in the public domain, and perhaps available online.
Yes, certainly, let me study & consider your code & see if I can weave
it into the project. Sounds interesting.
One older book I have (from 1917!) has 1-2 paragraphs saying 5x without
an intervening * is very bad form & yet, everybody seems to use it, at
least here the USA.
It's amazing that the old book you're referring to mentions '*' as multiplication.
If the '*' is [in the USA] suggested in books that would probably
explain the choice of that character for computer programs' syntax.
I suspect Mike used '*' as as short-hand for "a multiplication
sign" and not specifically the asterisk.
greator than
Hi Keith. I'll check that, stay tuned...
[...]
SEED = SEED ? SEED : 1
[...]
The program could be extended by two principle additions/changes.
One is to allow negative integral numbers for a, b, c; in that case
you don't even need the "random operator" logic as a side effect.
Since you are using only natural numbers in your formulas you may
want to stay within the domain of integral numbers also in the
results; that would require to check that condition before storing
an accepted formula, or to synthesize such formulas in the first
place.
Mike Sanders <porkchop@invalid.foo> wrote:
[...]
SEED = SEED ? SEED : 1
[...]
no, no, what am i thinking, better expressed as:
if (!SEED) SEED = 1
The program could be extended by two principle additions/changes.
One is to allow negative integral numbers for a, b, c; in that case
you don't even need the "random operator" logic as a side effect.
Since you are using only natural numbers in your formulas you may
want to stay within the domain of integral numbers also in the
results; that would require to check that condition before storing
an accepted formula, or to synthesize such formulas in the first
place.
Just some more ideas.
# outputs 100 random single variable linear equations in the form: ax+b=c
A deliberately chosen seed of 0 gets overwritten?
How about (since you're expecting a number)
if (SEED=="") SEED = 1
or (for good measure) the more general pattern for
an "uninitialized" variable 'var'
if (var=="" && var==0) ... # uninitialized
else ... # initialized (including "" and 0)
But is a seed of 1 "better" than a seed of 0 ?
Both create deterministic random number sequences.
Only srand() (i.e. without argument) creates a
time-depending quasi non-deterministic sequence.
My choice would probably be
if (var=="" && var==0) srand() # random start
else srand(var) # deterministic
to have both options.
and unless there's something out of whack, i'm using this version...
} else if (f == 2) {
# medium complexity: ax op1 bx = c
b2 = rnd(1, 20) # new/different coefficient for 2nd x
op2 = rop() # 2nd random operator
e = sprintf("%s%dx %s %dx = %d", n, a, op1, b2, op2, c)
}...
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
[snip]
See downthread, tell me what you think please kind sir...
(You know, since many/most of my suggestions aren't reflected
in all those versions I don't want to bother or bore you with
repetitions of my suggestions, and therefore I'm not sure what
kind of feedback you actually want. - I think it's certainly
better to ask others here for their opinions on your code and
on the algorithms, which would [potentially] provide you with
different views that may also match more with yours.)
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
(You know, since many/most of my suggestions aren't reflected
in all those versions I don't want to bother or bore you with
repetitions of my suggestions, and therefore I'm not sure what
kind of feedback you actually want. - I think it's certainly
better to ask others here for their opinions on your code and
on the algorithms, which would [potentially] provide you with
different views that may also match more with yours.)
Janis, just thanking for your time & input. Its true that I
always consider the input others provide but also always go
my own way. [...]
Bottom-line: No worries, its all good. =)