Writing a Truth Table Problem

This first problem will be a simple truth table problem. 

Pasted Graphic.tiff


Variables in Perl are a little different than variables in many other languages.

Suppose you want a variable say (a).  To declare this variable and assign a value would simply type $a.  Each time you use this variable you will need to use the dollar sign.


To define an array say (p) you would type @p.

Here is a sample Perl program.  The text of the program will be in bold, my own comments will be inserted in normal text.  Recall that ## denotes a comment section.  To view the source code click here.  The source code for this problem



## DESCRIPTION

## Mathematics and the Liberal Arts

## ENDDESCRIPTION


## KEYWORDS('algebra','graph',)


## DBsubject('Mathematics and the Liberal Arts')

## DBchapter('Logic')

## DBsection('Truth Tables')

## Date('7/07')

## Author('N.Wakefield')

## Institution('University of Northern Colorado')

## TitleText1('')

## EditionText1('')

## AuthorText1('')

## Section1('')

## Problem1('')


#KEYWORDS('graphs')


This can be thought of as the header to the file, DBsubject identifies what topic the problem covers.  The other  sections should be self explanatory.


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

Every problem should have this code immediately following the header.  Also once the Document() command has been used each line should end with a semicolon.


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

loadMacros('PG.pl',

        'PGbasicmacros.pl',

        'PGchoicemacros.pl',

        'PGanswermacros.pl',

        'PGgraphmacros.pl'

        );


These macros are necessary for the problem to be interpreted.  A good practice is to only include the macros that will be necessary.  


TEXT(beginproblem()); 


TEXT(beginproblem()) is the standard preamble to each problem and should always be included.

Depending on the type of problem you may or may not want to show partial correct solutions.  To disable partial correct solutions add the following command


$showPartialCorrectAnswers = 0;


Once your document includes all of the header and preamble you are ready to begin writing the actual problem. 


A WebWork problem includes two main parts.  


The first part of a WebWork problem is the solution and problem functions.  For our problem we want the correct answers to the truth table to be filled in.  For this problem I am going to make the problem a four-part problem.  To do this I will need to define four questions with answers.


However, before we write out the question and answer we need to define variables in which we can store the question and answer.  The following code defines four such variables.


# Make a new select list

$partone = new_select_list();

$parttwo= new_select_list();

$partthree= new_select_list();

$partfour= new_select_list();


A new_select_list() variable allows one to input questions and answers in a list format.  The next thing we will want to do is actually store questions and answers in the list.  We will define these questions inside of a variable and use qa() to tell the computer we are defining a questions and an answer.


The following code defines our question and answer variables.  In this case the question would be an empty string (i.e. a blank space in the truth table).  The answer would be T.


$partone->qa(" ", "T")


The qa() command works like this.  qa("what is your question","what should they answer")

The rest of the variables can be defined as


$parttwo->qa(" ","T");

$partthree->qa(" ","T");

$partfour-> qa(" ","F");


Recall that we made each of the variables into a list of questions.  Therefore, in some instances more than one question may be stored in a single variable.  It is important that you tell the computer how many of these questions to use.  The choose()  command handles this.


# Choose two the questions

$partone ->choose(1);  

$parttwo ->choose(1);  

$partthree ->choose(1);  

$partfour ->choose(1);  


We are now ready to move onto the actual displayed part of the problem.

To display text you want to utulize the BEGIN_TEXT, and END_TEXT commands.  For example,


BEGIN_TEXT

Fill in the missing spaces in the following truth table.  Type "T" for true and "F" for false. 

END_TEXT


The next step is to actually form the truth table.  To draw a truth table we will use the begintable command.  Begintable is a function that calls in the number of columns.  For our example we will create a three column table.  Each row in the table is denoted by row().


TEXT( begintable(3));

BEGIN_TEXT

\{  row('\(\ p \ \)  ',' \(\ q \ \)   ','\(p \wedge q \)' ) \}

\{  row('t ','t  ',$partone-> print_q) \}

\{  row('t ','f  ',$parttwo-> print_q) \}  

\{  row('f ','t  ', $partthree-> print_q) \} 

\{  row('f  ','f  ', $partfour-> print_q) \}

END_TEXT

TEXT( endtable() );


There are several important concepts that we will hit in this truth table.  First \{ \} denotes the beginning and end of Perl code.  Second, \( \) denotes the beginning and end of LaTex code.   The final important note stems from the use of $partone-> print_q.  This code tells the computer to print the questions that is stored in the variable partone.  Recall that we stored an empty string (i.e. " ") in partone and thus we should see a blank box in the third column.  


The last piece of code in a problem is the code to check an answer.  


# Enter the correct answers to be checked against the answers to the students.

ANS( str_cmp( $partone->ra_correct_ans )   ) ;

ANS( str_cmp( $parttwo->ra_correct_ans )   ) ;

ANS( str_cmp( $partthree->ra_correct_ans )   ) ;

ANS( str_cmp( $partfour->ra_correct_ans )   ) ;


This code compares the answer given to the answer you stored in the variables.


Every problem should end with


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


The source code for this problem