## WeBWorK Problems by Hedley Pinsent -
Number of replies: 2
Is it possible to use the first response [first answer_rule] to calculate the answer for the second response [second answer_rule]
Refer to my commented-out attempt in the evaluation section to see what I am trying to do.

## DESCRIPTION
## Statistics
## ENDDESCRIPTION

## KEYWORDS('Statistics','Confidence Intervals')
## Tagged by

## DBsubject('Statistics')
## DBchapter('Confidence Intervals')
## DBsection('Sample Size - Proportion')
## Date('')
## Author('')
## Institution('')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')

DOCUMENT();

"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGnumericalmacros.pl",
"PGstatisticsmacros.pl" ,
"PGauxiliaryFunctions.pl",
"PGinfo.pl"

);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1;$ppercent = random(40,80,10);
$p =$ppercent / 100;

$confidence = list_random(90,95,99);$deg = $confidence / 100;$E = list_random (4,5,6,8,10);
$dE =$E /100;

$z=udistr (0.5 -$deg/2);
$ans1 =$z;

#

BEGIN_TEXT
Problem text taken from " Lind, Marchal , Wathen, Waite" $BR$BR
It is claimed that $ppercent$PERCENT of households now subscribe to cable TV. You would like to verify this statement for your class in mass communications. You want an estimate within $E percentage points, with a$confidence $PERCENT level of confidence.$BR
What z-value would you use?
\{ans_rule(15)\}
(z from tables - loosely evaluated)$BR How large of a sample is required?$BR
\{ans_rule(15)\} $BR - stringently evaluated using $$\bar p (1 - \bar p) (\frac{z}{E})^2$$$BR Additionally we always round up. $BR We wish to use the z-value provided by the student to calculate the correct value for sample size. END_TEXT # \{listVariables()\}$test = $ans_hash {student_ans}; ANS(num_cmp($ans1, tolerance=>'absolute', tol => 0.01));

#$z =$ans_hash{student_ans}; #<<<<<<<<<<<<need student response<<<<<
$ans2 = ceil ($p* (1 - $p) * ($z /$dE ) **2); ANS(num_cmp($ans2));
ENDDOCUMENT(); by Gavin LaRose -
Hi Hedley,

This looks like a good candidate for a MultiAnswer MathObject. See http://webwork.maa.org/wiki/MultiAnswerProblems.

In this case, a MultiAnswer object that looks something like the following might work. (I haven't tested this.) Also, this will keep the absolute tolerance of 0.01 for both parts of the problem, so we'd need to reset that in the answer checker if that's not desirable. I also didn't check that ceil exists as an available MathObject function.

Gavin

Context()->flags->set(
tolType => 'absolute',
tolerance => 0.01
);
$ans1 =$z;
$ans2 = Compute("ceil($p*(1 - $p)*($z/$dE )^2)");$ans12 = MultiAnswer( $ans1,$ans2 )->with(
singleResult => 0,
checker => sub {
my ( $correct,$student, $self ) = @_; my ($stu1, $stu2 ) = @{$student};
my $stuCalc = Compute("ceil($p*(1 - $p)*($stu2/$dE)^2)"); my ($cor1, $cor2 ) = @{$correct};

if ( $cor1 ==$stu1 && $stuCalc ==$stu2 ) {
return [1,1];
} elsif ( $cor1 ==$stu1 ) {
return [1,0];
} elsif ( $stuCalc ==$stu2 ) {
return [0,1];
} else {
return [0,0];
}
}
);

BEGIN_TEXT
$$z$$-value: \{ $ans12->ans_rule(25) \}$BR
sample size: \{ $ans12->ans_rule(25) \} END_TEXT ANS($ans12->cmp() ); by Hedley Pinsent -
Well here it is, for whoever has been following the discussion.
I think it is ok now. I am quite pleased with how it works.
I tried lots of stuff; this is probably the only way to do it.
The subroutines seem to make a copy everything they receive, and are very reluctant to give anything back.

Thanks - hp

## DESCRIPTION
## Statistics
## ENDDESCRIPTION

## KEYWORDS('Statistics','Confidence Intervals')
## Tagged by

## DBsubject('Statistics')
## DBchapter('Confidence Intervals')
## DBsection('Sample Size - Proportion')
## Date('')
## Author('')
## Institution('')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')

DOCUMENT();

"PGbasicmacros.pl",
"PGnumericalmacros.pl",
"PGstatisticsmacros.pl" ,
"PGauxiliaryFunctions.pl",
"MathObjects.pl",
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1;$ppercent = random(40,80,10);
$p =$ppercent / 100;

$confidence = list_random(80,90,95,98,99);$deg = $confidence / 100;$E = list_random (1,2,3,4,5,6,8,10);
$dE =$E /100;

$z=udistr (0.5 -$deg/2);
#$ans1 =$z;

$ans2 = ceil($p*(1 - $p)*($z/$dE )**2); Context()->flags->set( tolType => 'absolute', tolerance => 0.0001 );$ans1 = $z;$ans12 = MultiAnswer( $ans1,$ans2 )->with(
singleResult => 0,
checker => sub {
my ( $correct,$student, $self ) = @_; my ($stu1, $stu2 ) = @{$student};
my $stuCalc = Compute("int(0.99999 +$p*(1 - $p)*($stu1/$dE)^2)"); my ($cor1, $cor2 ) = @{$correct};

if ( abs($cor1 -$stu1)<0.01 && ( ($stuCalc ==$stu2) || ($cor2 ==$stu2) )) {
return [1,1];
} elsif ( abs($cor1 -$stu1)<0.01 ) {
return [1,0];
} elsif ( ($stuCalc ==$stu2) || ($cor2 ==$stu2) ) {
return [0,1];
} else {
return [0,0];
}
}
);

BEGIN_TEXT

It is claimed that $ppercent$PERCENT of households now subscribe to cable TV. You would like to verify this statement for your class in mass communications. You want an estimate within $E percentage points, with a$confidence $PERCENT level of confidence.$BR
What z-value would you use?
\{ $ans12->ans_rule(25) \}$BR

How large of a sample is required? $BR \{$ans12->ans_rule(25) \} $BR $$\bar p (1 - \bar p) (\frac{z}{E})^2$$$BR Additionally we always round up. $BR END_TEXT ANS($ans12->cmp() );

ENDDOCUMENT();