Tutorial: Using Algorithms in LaTeX Questions

Algorithmic Questions

Randomized variables can be incorporated into any question. Rules and recommendations for effective use of randomized variables are given below, illustrated by related examples.

 

 

$variable=expression;

 

The final semicolon separating multiple variable definitions in a list is not optional.

 

Note: TeX comments (%) cannot appear in the argument to \code. To include comments, use the comment control sequence.

 

The usual arithmetic operations and functions can be used in the expression portion of the code statement. However, unlike most other programming languages, variables cannot be redefined in terms of themselves (for example, the statement $a=$a+1; is not allowed).  For a description of the additional functions that can be used in the code sections, see Functions within Algorithms.

 

\var{a}

 

Consider the following simple example.

 

Example 1

\begin{question}{Formula}

\qutext{Find the solution of the linear equation

$\var{a}x+\var{b}=\var{c}$.}

\answer{(\var{c}-\var{b})/\var{a}}

\code{

$a=rint(12)+1;

$b=rint(12)+1;

$c=rint(12)+1;

}

\end{question}

 

The above code randomly determines integer coefficients $a, $b, and $c between 1 and 12. The answer is calculated in the \answer section.

 

Although the above example does not cause errors, there are several side effects:

To avoid these issues, rewrite this example as follows, using a new variable $ans (so that you return only the final answer).

 

Example 2

\begin{question}{Formula}

\qutext{Find the solution of the linear equation

$\var{a}x+\var{b}=\var{c}$.}

\answer{\var{ans}}

\code{

$a=rint(12)+1;

$b=rint(12)+1;

$c=rint(12)+1;

$ans=($c-$b)/$a;

}

\end{question}

 

In this code, there are variable type issues.

 

 

The statement $a=rint(11); creates an integer variable, but the line $a=rint(11)+1; creates a floating-point variable as a result of the +1 operation. The same is true for the definitions of $b and $c. Using the above question, equations in the form 2.0x+9.0=4.0, which is correct, but not as simple as possible. Change the code so that $a, $b, and $c are integers.

 

Example 3

\begin{question}{Formula}

\qutext{Find the solution of the linear equation

$\var{a}x+\var{b}=\var{c}$.}

\answer{\var{ans}}

\code{

$a=int(rint(12)+1);

$b=int(rint(12)+1);

$c=int(rint(12)+1);

$ans=($c-$b)/$a;

}

\end{question}

 

There is one more problem. The variable $a can equal 1, in which case the equation has the form 1x+5=8. To avoid this situation, impose the condition that $a be an integer between 2 and 12.

Although $c can be 0 or negative,  if $b is 0 or negative the equation is again unsimplified. Generalize the code to allow $c to be any integer between -12 and 12.

 

Example 4

\begin{question}{Formula}

\qutext{Find the solution of the linear equation

$\var{a}x+\var{b}=\var{c}$.}

\answer{\var{ans}}

\code{

$a=int(rint(11)+2);

$b=int(rint(12)+1);

$c=int(rint(25)-12);

$ans=($c-$b)/$a;

}

\end{question}

 

There is another minor problem. The answer $ans is a floating-point number. If the student gives an incorrect response, the correct answer is displayed as a (potentially inexact) floating-point number, for example, .666666667, instead of 2/3. The system grades the question properly, but the displayed feedback may not have the correct form. To solve the problem, use a string variable to display the answer as a fraction.

 

Example 5

\begin{question}{Formula}

\qutext{Find the solution of the linear equation

$\var{a}x+\var{b}=\var{c}$.}

\answer{\var{ans}}

\code{

$a=int(rint(11)+2);

$b=int(rint(12)+1);

$c=int(rint(25)-12);

$num=int($c-$b);

$ans="$num/$a";

}

\end{question}

 

Note: Writing $ans="($c-$b)/$a"; displays answers of the form (11-6)/3. First, calculate the numerator and store it as an integer in $num, and then create the fraction with the string "$num/$a".

 

This last change introduces a new problem. The system displays correct answers of the form 4/2 or 3/3. To make integer answers display as integers, implement one more change.

 

Example 6

\begin{question}{Formula}

\qutext{Find the solution of the linear equation

$\var{a}x+\var{b}=\var{c}$.}

\answer{\var{ans}}

\code{

$a=int(rint(11)+2);

$b=int(rint(12)+1);

$c=int(rint(25)-12);

$num=int($c-$b);

$ansint=int($num/$a);

$ans=if(int(eq($num/$a,$ansint)),"$ansint","$num/$a");

}

\end{question}

 

By using the intermediate variable $ansint and the string "$ansint" in the last statement (instead of directly using the string "int($num/$a)", which produces answers of the form "int(6/3)"), integers display properly.

 

This final version allows some nonreduced fractions, for example, 2/4, to appear as answers, but, in general, both the problem and the answer appear in a simple, standard form.

 

Use the following guidelines for algorithms: