next up previous contents
Next: Sample Problem 2 Up: Learning to Walk Previous: Sample Problem 1   Contents

Sample Problem oldie

## DESCRIPTION
##   A very simple first problem with lots of comments
## ENDDESCRIPTION

## KEYWORDS('algebra')

## DOCUMENT();  should be the first executable line in the problem.
## 
DOCUMENT();      

## loadMacros(...):  Load basic macros to help build the problem set
## Most of these are located in .../webwork/system/courseScripts/
##
loadMacros(        
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl"
);


## TEXT(beginproblem());
## generates text listing number and the point value of 
## the problem. It will also print the file name containing
## the problem for users listed in the PRINT_FILE_NAMES_FOR
## PG_environment variable.
##
TEXT(beginproblem());

## If showPartialCorrectAnswers = 1 students are told which 
## of their answers in a problem are wrong.
##
$showPartialCorrectAnswers = 1;


## random(begin, end, incr) produces a pseudo-random number between 
## <begin> and <end> in increments of <incr>.  $a, $b, $c, $d are now 
## variables to work with in the problem.  The default increment is 1,
## so the statements below could be written as (say)
## $a = random(-10,-1);
##
$a = random(-10,-1,1);
$b = random(1,11,1);
$c = random(1,11,1);
$d = random(1,11,1);

## Before proceeding, you must understand that the code you write here
## for a problem is interpreted by three different programs, for
## rendering by text, LaTeX, and HTML.  Thus control sequences
## specific to each language must be denoted in a generic way which
## later gets translated into the specific control sequence required
## by an individual language.  Thus for example, a paragraph break in
## LaTeX is denoted \par, while in HTML it is denoted <P>.  In the pg
## language it is denoted $PAR which gets translated as appropriate.

## A list of control sequence is found in PGbasicmacros.pl.  
## They include (not an exhaustive list here)
## $PAR --- paragraph break
## $BR  --- line break 
## $LQ, $RQ --- left and right quotes: `` and ''

## $BM, $EM --- begin and end math mode (for LaTeX math constructs)
## $BDM, $EDM --- begin and end displayed math mode
## Note:  the use of $BM, $BDM, $EM, $EDM has been deprecated
## Instead of $BM use \(
## Instead of $EM use \)
## Instead of $BDM use \[
## Instead of $EDM use \]

## $LTS, $GTS, $LTE, $GTE --- <, >, <=, >= (inequalities)
## $US --- underscore (without this LaTeX wants to be in math mode)
## $SPACE --- a space (\ in LaTeX, &nbsp; in HTML)
## $BBOLD, $EBOLD --- begin and end bold face type
## $HR --- horizontal rule across page
## $LBRACE, $RBRACE --- left and right braces: {, }
## $LB, $RB synonymous with the above

## Also important is the construct \{ans_rule(10)\}
## ans_rule(width) creates (HTML code for) an answer box ``width'' 
## characters wide.  Of course if you just type ans_rule(10), all you
## get is the text "ans_rule(10)", not the output of the function.
## The \{ \} have to do with the way in which EV3 evaluates the expression


## Begin laying out text to be presented
##
BEGIN_TEXT
$PAR
This problem demonstrates how you enter numerical answers into WeBWorK. 
$PAR

Evaluate the expression \(3($a )($b -$c -2($d ))\): 
\{ ans_rule(10) \}
$BR

END_TEXT

## We put the exact answer in the variable $ans 
## 
$ans = 3*($a)*($b-$c-2*($d));
## To check the student's answer, we apply the ANS function.
## There are many answer evaluators: we'll address some of them in
## answer_evaluatorsChapter 14.
## 
## strict_num_cmp demands a numerical answer accurate to within
## .1% of the correct answer $ans.
##
ANS(strict_num_cmp($ans));




## The next piece of the problem
##
## Note that in the problem to display the product, they write 
## ($a) * ($b).  The parentheses are there for visual clarity 
## (remember you are in text display mode).
##
## Also the line \{ ans_rule()\} is the same as \{ ans_rule(20)\}
##
BEGIN_TEXT 
In the case above you need to enter a number, since we're
testing whether you can multiply out these numbers. (You can use a
calculator if you want.)
$PAR

For most problems, you will be able to get WeBWorK to
do some of the work for you.  For example
$BR
Calculate ($a) * ($b): \{ ans_rule()\}
$BR

END_TEXT

$ans = $a*$b;

## Finally, std_num_cmp demands answer within .01% of the correct
## answer, but allows answers containing elementary expressions
##
ANS(std_num_cmp($ans));


## In the problem below, we also see the function htmlLink used to add
## a link to another web page for reference.  Also we use \{$a*$b\} within
## a BEGIN_TEXT ... END_TEXT block to force $a*$b to be evaluated by Perl
## resulting in a single number which will be displayed.  We use \{  \}
## three other times in this section including  \{ ans_rule(20) \}. Now we
## are begining to give away some of WeBWorK's secrets.
##


## Also note we cannot add comments inside a BEGIN_TEXT ... END_TEXT
## so we do them here.  You should look at the oup[ut of this problem
## as well as the input.  For example, the line (below) ``sin(pi/4) 
## equals \{1/sqrt(2)\}'' prints ``sin(pi/4) equals .70710678'' whereas
## the line ``\(1/\sqrt{2} \)'' drops into LaTeX so actually prints

## the value $ 1/\sqrt2$.

BEGIN_TEXT

The asterisk is what most computer languages use to denote
multiplication, and you can use this with WeBWorK.  But WeBWorK will
also allow you to use a space to denote multiplication.  You can
either write \($a * $b\) or \{$a*$b\} or even \($a \ $b\).  All will
work. Try them.  $PAR

Now try calculating the sine of 45 degrees ( that's sine of pi over 4
in radians and numerically sin(pi/4) equals \{1/sqrt(2)\} or, more
precisely, \(1/\sqrt{2} \) ).  You can enter this as sin(pi/4) , as
sin(3.1415926/4), as 1/sqrt(2), as 2**(-.5), etc. This is because
WeBWorK knows about functions like sin and sqrt (square root). (Note:
exponents can be indicated by either a "caret" or **). Try it.

$BR 
\( \sin(\pi/4) = \) \{ ans_rule(20) \} 
$PAR

 Here's the 
\{htmlLink(
  qq!/webwork_system_html/docs/techdescription/pglanguage/availableFunctions.html!,
"list of the functions") 
\} 
which WeBWorK understands.  WeBWorK ALWAYS uses radian mode for trig functions.  
$PAR

END_TEXT

ANS( std_num_cmp(sin(3.1415926/4)) );

BEGIN_TEXT

Sometimes you need to use ( )'s to make your meaning clear. E.g. 1/2+3
is 3.5, but 1/(2+3) is .2 Why?  Try entering both and use the
${LQ}Preview${RQ} button below to see the difference.  In addition to
( )'s, you can also use [ ]'s and $LB ${RB}'s.  
$BR \{ ans_rule(20)\}
$PAR

END_TEXT

ANS( std_num_cmp(.2));


## Note that above ${LQ} is just the $LQ variable (i.e. $var and ${var} are always
## the same variable). We write ${LQ}Preview because we don't want a space between
## $LQ and Preview (and $LQPreview wouldn't work --- why?).
## Notice that below no question is being asked; just information for the
## student is displayed.


BEGIN_TEXT

You can always try to enter answers and let WeBWorK do the
calculating.  WeBWorK will tell you if the problem requires a strict
numerical answer.  The way we use WeBWorK in this class there is no
penalty for getting an answer wrong.  What counts is that you get the
answer right eventually (before the due date).  For complicated
answers, you should use the ${LQ}Preview${RQ} button to check for
syntax errors and also to check that the answer you enter is really
what you think it is.


END_TEXT

ENDDOCUMENT();   # This should be the last executable line in the problem.



Thomas R. Shemanske 2002-03-05