WeBWorKdocs

sq_webwork:


Home
Current UR
courses
Visitor
page
Intro to
WeBWorK
WeBWorK 2
Twiki
WW
Community
Grant Support
& awards
Discussion
group
Problem
library
Write/modify
problems
Create &
manage course
Tutorial on
running a course
HowTos
FAQ
WeBWorK2
FAQ
Software
Download
How to
Install WW server
Feedback
Site Map
Change
preferences
Change
password





Prev | Next | tutorial

This page is no longer being maintained. The new URL for this content is http://webwork.maa.org/wiki/Category:Sample_Problems

To obtain the following problem:


(1 pt) rochesterLibrary/setMAAtutorial/standardexample.pg

Standard Example

Complete the sentence:
world;

Enter the sum of these two numbers:
3 + 5 =

Enter the derivative of

f(x) = x^{5}

f '(x) =


WARNINGS
µ¦å{h­

you would type the following:

       
001:
002:
003: DOCUMENT();
004: loadMacros(PGbasicmacros.pl,
005: PGchoicemacros.pl,
006: PGanswermacros.pl,
007: PGauxiliaryFunctions.pl
008: );
009:
010: TEXT(beginproblem(), $BR,$BBOLD, Standard example, $EBOLD, $BR,$BR);
011:
012: # A question requiring a string answer.
013: $str = 'world';
014: #$str = "Dolly";
015: BEGIN_TEXT
016: Complete the sentence: $BR
017: \{ ans_rule(20) \} $str;
018: $PAR
019: END_TEXT
020:
021: ANS( str_cmp( "Hello") );
022:
023: # A question requiring a numerical answer.
024: #define the variables
025: $a = 3;
026: $b = 5;
027: #$a=random(1,9,1);
028: #$b=random(2,9,1);
029:
030: BEGIN_TEXT
031: Enter the sum of these two numbers: $BR
032: \($a + $b = \) {ans_rule(10) }
033: $PAR
034: END_TEXT
035:
036: $sum = $a + $b;
037: ANS( num_cmp( $sum ) );
038:
039: # A question requiring an expression as an answwer
040: BEGIN_TEXT
041: Enter the derivative of \[ f(x) = x^{$b} \] $BR
042: \(f '(x) = \) \{ ans_rule(30) \}
043: $PAR
044: END_TEXT
045: $new_exponent = $b-1;
046: $ans2 = "$b*x^($new_exponent)";
047: ANS( fun_cmp( $ans2 ) );
048: #<<<#########################################################
049: BEGIN_TEXT
050: $HR
051:
052: You can view the
053: \{ htmlLink(sourceAlias("links/set$setNumber/standardexample.html"),
054: "source", q!TARGET="source"!)\}
055: for this problem.
056: END_TEXT
057: #########################################################>>>
058: ENDDOCUMENT();
059:
060:

Comments:

Perl code:

In addition to the printed text, there are programming statements which define the coefficients used in the problem. Any statements that are not within BEGIN_TEXT / END_TEXT statements are interpreted as Perl code using additional subroutines (also called macros) written for the PG language. Even the DOCUMENT() and loadMacros(...) and ANS statements are progamming statements in Perl which call predefined subroutines.

PG macros: See Hello world example for info on the most basic PG macros. For more information on Perl see PG language basics.

Each Perl statement must end with a semi-colon.

Any line which begins with # is a comment line.

  1. The first group of statements repeats the Hello world question.
  2. The next group of statements defines a question requiring a numerical answer. In lines 25 and 26 we set $a equal to 3 and $b equal to 5.

    If you want to choose random numbers instead, comment out lines 25 and 26 (insert # at the beginning of the line) and uncomment the next two lines.

    The macro random(1,9,1) chooses random numbers uniformly distributed between 1 and 9 with step size 1. (To obtain fractional numbers use random(1,5,0.25) which will give you a uniformly distributed choice from the numbers 1, 1.25, 1.50,... 5. A short hand version for random(1,5,1) is random(1,5)). The variables named $a and $b now contain two integers. (In Perl all scalar variable names begin with a dollar sign. A scalar variable can hold a single number (integer or real number) or a string (of characters). A scalar variable can also hold "pointers" and "objects" as will be explained later.

  3. Variable Interpolation Inserting the phrase $a + $b in the text portion of the problem causes the quantities stored in $a and $b to be printed (interpolated). Note: In text mode these numbers are not added, the plus sign is printed just as a plus sign (line 32).
  4. Math mode The construction \( ...\) means that anything inside the parentheses will be printed or typeset in "math mode" (line32). This is the same construction used in LaTeX and it means that the formatting of formulas and numbers inside these parentheses will conform as closely as possible to the standards for typesetting equations.
  5. Display math mode This is almost the same as math mode, but it uses the construction \[...\] to display a mathematical formula in a paragraph by itself (line 41).
  6. All mathematics formulas, including numbers, should be enclosed in either the math mode or the display math mode construction or else they will not look like math formulas.
  7. In the statement

    $sum = $a + $b;

    the contents of the variables $a and $b are added and placed in the variable $ans (line 36).
  8. More answer evaluators:

    The answer evaluator num_cmp($ans) compares the student's answer with the number stored in the variable $ans (line 37). For real numbers the answers are expected to agree to within .01%. See the documentation for answer evaluators and PGanswermacros.pl

    Then answer evaluator fun_cmp($ans2) compares the student answer with the function defined by the string in the variable $ans2 (line 47).

    The three answer evaluators str_cmp(..) for strings, num_cmp($ans) for numbers, and fun_cmp(...) for functions are the three most commonly used methods of checking answers. It is however possible to customize these answer_evaluators and even to create new ones.

  9. More examples:
    {linkToProblem("setAlgebra5Equations/srw1_5_22.pg")} -- {linkToSource("setAlgebra5Equations/srw1_5_22.pg","source")}
    {linkToProblem("setDerivatives13Higher/s2_7_10.pg")} -- {linkToSource("setDerivatives13Higher/s2_7_10.pg","source")}


Prev | Next | tutorial

Last update: Tuesday, March 4, 2008 at 12:30:33 AM.
This site maintained using Manila and Frontier software.