WeBWorK Problems

Sorting an array in PGML?

Sorting an array in PGML?

by tim Payer -
Number of replies: 1
Greetings,

I was trying to sort a small numeric array of 10 to 12 values.
I used a code snippet from a correspondence with Michael Gage, 2001, as this was the only reference I could find in webwork circles.

It seems I am not using it correctly though.

Could you please let me know where my glitch is?

Thank you for being to take a look.

Tim

The snippet of sorting code:

sub num_sort {
       PGsort sub {$_[0] <=> $_[1]}, @_;
 }

@out_array = num_sort(@count);

And the problem the code is embedded within:

# DESCRIPTION   
# Stat 109 Data summarization
# WeBWorK problem written by TimPayer <tsp1@humboldt.edu>
# ENDDESCRIPTION

## DBsubject(Statistics)
## DBchapter(Exploratory data analysis/descriptive statistics)
## DBsection(Summary statistics)
## Institution(Humboldt State University)
## Author(Tim Payer)
## KEYWORDS(summations, mean, sd)

DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGbasicmacros.pl",
"MathObjects.pl",
"PGML.pl",
"niceTables.pl",
"parserFormulaUpToConstant.pl"
);

Context("Numeric");
install_problem_grader(~~&std_problem_grader);
$showPartialCorrectAnswers = 1;

## The beginning of 2.1 Stat 109 problem:
## Given 10 values (at random) find the mean and median.

$parsum = 0;
@count = ();
foreach my $i (1..10) {
   $count [$i] = random(0,27,1);
   $sum = Compute("$count[$i] + $parsum");
   $parsum = $sum; 
}

sub num_sort {
       PGsort sub {$_[0] <=> $_[1]}, @_;
 }

@out_array = num_sort(@count);

#$a = 3;
#do {
#    $a = $a + 1;
#} until ( $a == 10 );

## For a randomized sample that gives each student a different random sample,
## un-comment(#) the loop above. For a fixed sample that permit each student to share 
## a common answer, comment out the loop above and un-comment the 10 
## line of code below.

#$count [1] = 13;
#$count [2] = 15;
#$count [3] = 10;
#$count [4] = 6;
#$count [5] = 9;
#$count [6] = 18;
#$count [7] = 20;
#$count [8] = 23;
#$count [9] = 11;
#$count [10] = 12;

# Edit answers soon....

$ans1 = Compute("4 ");
$ans2 = Compute("4 ");

BEGIN_PGML
 
2.1)  A nurse at a rural college health center surveyed ten students for their history of exposure to antibiotics. The results of the sample were tallied in the table below.


[@DataTable(
  [
    [ ["".PGML::Format('[`\text{Student}`]')."",
           halign  => 'r|',
           rowcss  => 'border-bottom: 2px solid;',
           cellcss => 'border-top: 2px solid; 
                   border-left: 2px solid; 
                   border-right: 2px solid; ',],
      ["".PGML::Format('[`\text{Number of courses}`]')."",
           rowcss  => 'border-bottom: 2px solid;',
           cellcss => 'border-top: 2px solid; 
                   border-right: 2px solid; ',],
    ],
    [ ["1",
           midrule => '1',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-bottom: 1px solid; 
                   border-left: 1px solid; 
                   border-right: 1px solid; ',],
      ["".PGML::Format('[`[$count[1]]`]')."",
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-top: 1px solid; 
                   border-right: 1px solid; ',],
    ],
    [ ["2",
           halign  => '|r|',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-left: 1px solid; 
                   border-right: 1px solid; ',],
      ["".PGML::Format('[`[$count[2]]`]')."",
           colspan => '1',
           halign  => 'c|',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-right: 1px solid; ',],
    ],
    [ ["3",
           midrule => '1',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-bottom: 1px solid; 
                   border-left: 1px solid; 
                   border-right: 1px solid; ',],
      ["".PGML::Format('[`[$count[3]]`]')."",
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-top: 1px solid; 
                   border-right: 1px solid; ',],
    ],
    [ ["4",
           halign  => '|r|',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-left: 1px solid; 
                   border-right: 1px solid; ',],
      ["".PGML::Format('[`[$count[4]]`]')."",
           colspan => '1',
           halign  => 'c|',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-right: 1px solid; ',],
    ],
    [ ["5",
           midrule => '1',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-bottom: 1px solid; 
                   border-left: 1px solid; 
                   border-right: 1px solid; ',],
      ["".PGML::Format('[`[$count[5]]`]')."",
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-top: 1px solid; 
                   border-right: 1px solid; ',],
    ],
    [ ["6",
           halign  => '|r|',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-left: 1px solid; 
                   border-right: 1px solid; ',],
      ["".PGML::Format('[`[$count[6]]`]')."",
           colspan => '1',
           halign  => 'c|',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-right: 1px solid; ',],
    ],
    [ ["7",
           midrule => '1',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-bottom: 1px solid; 
                   border-left: 1px solid; 
                   border-right: 1px solid; ',],
      ["".PGML::Format('[`[$count[7]]`]')."",
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-top: 1px solid; 
                   border-right: 1px solid; ',],
    ],
    [ ["8",
           halign  => '|r|',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-left: 1px solid; 
                   border-right: 1px solid; ',],
      ["".PGML::Format('[`[$count[8]]`]')."",
           colspan => '1',
           halign  => 'c|',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-right: 1px solid; ',],
    ],
    [ ["9",
           midrule => '1',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-bottom: 1px solid; 
                   border-left: 1px solid; 
                   border-right: 1px solid; ',],
      ["".PGML::Format('[`[$count[9]]`]')."",
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-top: 1px solid; 
                   border-right: 1px solid; ',],
    ],
    [ ["10",
           halign  => '|r|',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-left: 1px solid; 
                   border-right: 1px solid; ',],
      ["".PGML::Format('[`[$count[10]]`]')."",
           colspan => '1',
           halign  => 'c|',
           rowcss  => 'border-bottom: 1px solid;',
           cellcss => 'border-right: 1px solid; ',],
    ],
  ],
  caption   => 'Number of antibiotic courses administered to each student since birth:',
  align     => '|r|c|l|',
  columnscss => ['border-left: 0px solid; ',
                 'border-right: 0px solid; ',
                 ' ',],
);@]*  

 
Determine the mean number of courses administered to these ten students:  
 
The total sum of courses is [``[$sum]``]
 

2.1a)    [``\bar x``]  = [__________]{"$ans1"}  

Determine the median number of courses administered to these ten students:

2.1b)    [``\tilde x``]  = [__________]{"$ans2"}

2.1c) Determine the first Quartile:

2.1d) Determine the third Quartile:




END_PGML

BEGIN_PGML_SOLUTION
*SOLUTION*

10.) The tenth summation expression is just a sum of the respective products formed by sequential pairs of both data sets.  
We merely substitute for the given values in each term, and then sum accordingly.

Edit solution soon...
 
[``\begin{aligned}&\\
\sum_{i = 1}^{n}x_i \cdot f_i&=  x_1\cdot f_1 + x_2\cdot f_2 +x_3\cdot f_3 +x_4\cdot f_4 && \text{Expand for each term. }\\  
\sum_{i = 1}^{n}x_i \cdot f_i&= [$x1] \cdot [$f1] +[$x2] \cdot ([$f2]) +( [$x3]) \cdot [$f3]  +[$x4] \cdot [$f4]   && \text{Substituting for each term.}\\  
\sum_{i = 1}^{n}x_i \cdot f_i&= [$xf1] ? [$xf2] ? [$xf3] +[$xf4] && \text{Evaluate each product.}\\  
\sum_{i = 1}^{n}x_i \cdot f_i&= [$ans10]  && \text{Collect the terms.}  
\end{aligned}``]  


END_PGML_SOLUTION

ENDDOCUMENT();

In reply to tim Payer

Re: Sorting an array in PGML?

by Danny Glin -
It's possible that the problem is that your array indices start at 1.  The first index in a perl array is 0, so when you're sorting the array it could be tripping on the fact that $count[0] is empty.

Try changing your indices to run from 0 to 9, and see if that fixes the problem.