Deutsch English Français Italiano |
<viurhe$2bces$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com> Newsgroups: comp.lang.awk Subject: Re: 100 Random Single Variable Linear Equations Date: Fri, 6 Dec 2024 13:43:25 +0100 Organization: A noiseless patient Spider Lines: 94 Message-ID: <viurhe$2bces$1@dont-email.me> References: <vits2o$240vr$1@dont-email.me> <vitvta$24sm3$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Fri, 06 Dec 2024 13:43:26 +0100 (CET) Injection-Info: dont-email.me; posting-host="5e82f14568e0d0b780fce3b92e6d3a5e"; logging-data="2470364"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZPRsU4ovla6AsGUzIxrSr" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:HD6rto/UK4J2fm8q2MQt/AnhJQ4= X-Enigmail-Draft-Status: N1110 In-Reply-To: <vitvta$24sm3$1@dont-email.me> Bytes: 4385 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.) Janis On 06.12.2024 05:51, Mike Sanders wrote: > 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 > 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) } }