WeBWorK Problems

how to produce variable-size tables

how to produce variable-size tables

by Siman Wong -
Number of replies: 2
Is it possible to create tables of randomized sizes?

I would like to create a m-by-n table where m, n are randomized integers, and students have to fill in (some of) the entries with prescribed information. For (very) small m, n I ended up using a long list of if-then-else loop, where in each case I explicitly type out the table, but this makes the coding very difficult...

Thanks!
In reply to Siman Wong

Re: how to produce variable-size tables

by Gavin LaRose -
Hi Siman,

I'm not sure I'm understanding exactly what you're trying to do, but it sounds as if just creating the table with nested loops should do what you need. I'm thinking of something like the following (which I have not tested).

$m = random(3,5,1);
$n = random(3,5,1);
@header = ( '' );
for( my $i=1; $i<=$n; $i++ ) {
    push( @header, $i );
}
@rows = ();
@ans = ();
for( my $j=1; $j<=$m; $j++ ) {
    my @newRow = ( $j );
    for( my $i=1; $i<=$n; $i++ ) {
        push( @newRow, ans_rule(5) );
        push( @ans, Compute($i*$j) );
    }
    push(@rows, row(@newRow));
}

BEGIN_TEXT
Fill in the multiplication table:
$BR
$BCENTER
\{begintable($n)\}
\{ row(@header) \}
@rows
\{endtable()\}
$ECENTER
END_TEXT

foreach my $a ( @ans ) {
    ANS( $a->cmp() );
}

Is that at all helpful?

Gavin