DOCUMENT();

loadMacros(
    'PGstandard.pl',       'PGML.pl',
    'StatisticalPlots.pl', 'contextPercent.pl',
    'PGcourse.pl'
);


Context('Percent');

@grades = map { random(1, 5) } (0 .. 3);

$statPlot = StatPlot(
    xmin            => 0,
    xmax            => 5,
    xtick_delta     => 1,
    xminor          => 0,
    ymin            => 0,
    ymax            => 6,
    yminor          => 0,
    ytick_delta     => 1,
    xtick_labels    => { 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D' },
    show_grid       => 0,
    rounded_corners => 1
);

$statPlot->add_barplot(
    [ 1 .. 4 ], ~~@grades,
    fill_colors  => [ 'Red,', 'Salmon', 'LightYellow', 'LightGreen' ],
    stroke_width => 1,
    bar_width    => 0.9
);

$alt_text =
    "A bar graph with vertical bars.  The height of the bar labelled A "
    . "is $grades[0]. The height of the bar labelled B is $grades[1].  "
    . " The height of the bar labelled C is $grades[2] and finally the "
    . "bar labelled D is $grades[3].";

# This code adds up all the numbers in the @grades array.
$num_students = 0;
$num_students += $_ for @grades;

$perA = Compute($grades[0] / $num_students);

BEGIN_PGML
The following is a distribution of grades on a Statistics quiz.
>>[! [$alt_text] !]{$statPlot}{400}<<

What percentage of students earned an A? [_]{$perA}
END_PGML

BEGIN_PGML_SOLUTION
The total number of students that took the quiz is [$total] so the percentage is
the number of A students or [`[$grades[0]]/[$num_students] = [$perA] `]
END_PGML_SOLUTION

ENDDOCUMENT();