## WeBWorK Problems

### generate file with data for students to analyze? ### Re: generate file with data for students to analyze?

by George Jennings -
Number of replies: 0
Thanks Felipe and Arnie,

Felipe's flash looks great but daunting to learn so I chickened out and cooked up something in javascript that seems to work pretty well. It displays a window with a tab-separated table, then students can copy and paste it into a spreadsheet. Separating with tabs seems to be the key to get it to work with
Internet Explorer. Thank, Arnie, for that suggestion.

Here is a demo that shows how it works in a very simple problem.

DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
);

## Set-up

Context("Numeric");
# intercept, slope
$b = random(2,5,.1);$m = random(3,7);
# create fake noisy linear data (x[i],y[i]), i=0,1,...
$count = 10;$sumx = 0; $sumy = 0; for($i=0;$i<$count;$i++){$x[$i] =$i+random(-.4,.5,.1);
$y[$i] = $b +$m*( $x[$i]+random(-.4,.5,.1) );
# remove extra digits possibly caused by binary arithmetic
$x[$i] = sprintf("%.1f",$x[$i]);
$y[$i] = sprintf("%.1f",$y[$i]);
# one line in the tab-separated data table
$jsDat[$i] = '"'.join('\t',$x[$i],$y[$i]).'"';
$sumx +=$x[$i];$sumy += $y[$i];
}

# stringify table for passing to javascript
$jsDatStr = join(",",@jsDat); # data for javascript # javascript for opening data window HEADER_TEXT(<<EOS); <script Xlanguage="javascript" type="text/javascript"> <!--// function openDataWindow(){ var xyRow=new Array($jsDatStr);
var i=0;
dataWindow=window.open('','','width=200,height=300');
dataWindow.document.writeln("<pre>");
dataWindow.document.writeln("x\ty");
dataWindow.document.writeln("---\t---");
for (i=0;i<$count;i++){ dataWindow.document.writeln(xyRow[i]); } dataWindow.document.writeln("</pre>"); } //--> </script> EOS # answers$meanx = Compute("$sumx/$count");
$meany = Compute("$sumy/$count"); ## Main text TEXT(beginproblem()); Context()->texStrings; BEGIN_TEXT Here are some fake data. \{ begintable(12) \} \{ row( "x", @x)\} \{ row( "y", @y)\} \{ endtable() \} Open the \{htmlLink("javascript:openDataWindow();","easy copy")\} window, copy-and-paste the data from the window into a spreadsheet or other application, then use the application to find the mean of $$x$$ and the mean of $$y$$.$PAR
$$\bar{x}$$ = \{ans_rule(10)\} $SPACE $$\bar{y}$$ = \{ans_rule(10)\} END_TEXT Context()->normalStrings; ## Answer evaluation ANS($meanx->cmp());
ANS(\$meany->cmp());

ENDDOCUMENT();

--George