Forum archive 2000-2006

Michael Gage - Standard example

Michael Gage - Standard example

by Arnold Pizer -
Number of replies: 0
inactiveTopicStandard example topic started 5/12/2000; 8:40:45 AM
last post 9/10/2001; 9:55:46 AM
userMichael Gage - Standard example  blueArrow
5/12/2000; 8:40:45 AM (reads: 2513, responses: 1)

The Standard example

This is example is a template for a great many WeBWorK problems.


Complete the sentence:

world!

Enter the sum of these two numbers:
2 + 6 =

Enter the derivative of

f(x) = x6
f'(x) =



you would type the following:


        
1 DOCUMENT();
2 loadMacros(
3 "PGbasicmacros.pl",
4 "PGchoicemacros.pl",
5 "PGanswermacros.pl",
6 "PGauxiliaryFunctions.pl"
7 );
8
9 # We start with a question requiring a string answer.
10 BEGIN_TEXT
11 Complete the sentence: $BR
12 { ans_rule(20) } world!
13 $PAR
14 END_TEXT
15 ANS( str_cmp( "Hello" ) ); # Here is the answer, a string.
16
17 # Next we do an example requiring a numerical answer.
18 # Choose a random integer betwen 1 and 9
19 $a = random(1, 9, 1);
20 # Choose random integer between 2 and 9
21 $b = random(2, 9, 1);
22 BEGIN_TEXT
23 Enter the sum of these two numbers: $BR
24 \($a + $b = \) \{ans_rule(10) \}
25 $PAR
26 END_TEXT
27 $ans = $a + $b;
28 ANS( num_cmp( $ans ) ); # In this case the answer is a number.
29
30 # Finally an example requiring an expression as an answwer
31 BEGIN_TEXT
32 Enter the derivative of \[ f(x) = x^{$b} \]$BR
33 \(f'(x) = \) \{ ans_rule(30) \}
34 $PAR
35 END_TEXT
36 $exponent = $b-1;
37 $ans2 = "$b*x^($exponent)";
38 ANS( fun_cmp( $ans2 ) ); # Here the students answer should be an expression.
39
40 ENDDOCUMENT();

Comments:

This problem is just a demo -- it doesn't actually work if you push the submit answer button. (You can test a live version of this problem.)

Perl code:

In addition to the printed text, this problem has statements which define the problem. Any statements that are not within BEGIN_TEXT / END_TEXT statements is interpreted as Perl code using additional subroutines (also called macros) written for the PG language (for more information on Perl see PG language basics). Even the DOCUMENT() and loadMacros(...) and ANS statements are progamming statements in Perl which call predefined subroutines. Each Perl statement must end with a semi-colon. Any line which begins with # is a comment line.

PG macros: (see Hello world example for info on the basic PG macros).

Any line which begins with # is a comment line.

  1. First we choose two random numbers (lines 18 and 20). The macro random(1,5,0.5) chooses random numbers uniformly distributed between 1 and 5 with step size 0.5. (To obtain only integers use random(1,5,1) or the shorthand version random(1,5)).

    These integers are stored in the variables named $a and $b (lines 18 and 20). (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" which will be explained later.

  2. 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 24).
  3. Math mode The construction \( ...\) means that anything inside the parentheses will be printed or typeset in "math mode" (lines 24 and 33). 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.
  4. 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 32).
  5. 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.
  6. In the statement

    $ans = $a + $b;

    the contents of the variables $a and $b are added and placed in the variable $ans (line 27).

     

  7. More answer evaluators:

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

    Then answer evaluator function_cmp($ans2) compares the student answer with the function defined by the string in the variable $ans2 (line 38). In defining the correct answer, instructors must use * for multiplication and either ^ or ** for exponentiation. Failure to do so may cause mysterious warnings and a "pink screen" indicating an error in writing the problem.

    The three answer evaluators str_cmp(..) for strings, num_cmp($ans) for numbers, and function_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.

 

<| Post or View Comments |>


userMichael Gage - Standard example  blueArrow
9/10/2001; 9:55:46 AM (reads: 4746, responses: 0)
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:
    setAlgebra5Equations/srw1_5_22.pg -- source
    setDerivatives13Higher/s2_7_10.pg -- source

<| Post or View Comments |>