# Robson ▸ Little Red Book ▸ Sailing Race

## Chapter 6 - Question 3

In a team sailing competition, six races are sailed and, in each race, each boat is allocated points for the position in which it finishes, the most points going to the winner.

To calculate the final score for a boat, only its five best races are considered; the worst score for each boat is ignored.

A team consists of four boats, but only the three highest scoring boats are counted in the final total for the team.

Design a program, to process the points scores by each boat in a team and calculate the total score for the team.

### Solution 1

PHP:

`<?` `    // i'm going to assume they are playing one other team, so the possible scores are` `    // first (3 points), second (2 points), third (1 points) and everything else is nothing` `    \$scores = array(3, 2, 1, 0, 0, 0, 0, 0);` `    ` `    // simulate six races` `    for (\$a = 0; \$a < 6; \$a++)` `    {` `        // randomize positions` `        shuffle(\$scores);` `        // get the score for each boat` `        for (\$b = 0; \$b < 4; \$b++)` `            // store the score` `            \$boats[\$b][\$a] = \$scores[\$b];` `    }` `    ` `    // loop through each boat` `    for (\$n = 0; \$n < 4; \$n++)` `        // and display it's score` `        echo 'Boat ' . (\$n+1) . ' race scores: ' . implode(', ', \$boats[\$n]) . '<br/>';` `        ` `    // loop through each boat` `    for (\$n = 0; \$n < 4; \$n++)` `    {` `        // sort its scores` `        rsort(\$boats[\$n]);` `        // take off the lowest` `        array_pop(\$boats[\$n]);` `    }` `    ` `    echo '<br/>';` `    // loop through each boat` `    for (\$n = 0; \$n < 4; \$n++)` `    {` `        // it's final score is it's top five scores added together` `        \$boats[\$n] = array_sum(\$boats[\$n]);` `        // show the final boat score` `        echo 'Boat ' . (\$n+1) . ' final score: ' . \$boats[\$n] . '<br/>';` `    }` `    ` `    // sort the scores for all the boats` `    rsort(\$boats);` `    // take off the lowest` `    array_pop(\$boats);` `    ` `    // add up all the remaining scores` `    echo '<br/>Team score: ' . array_sum(\$boats);` `            ` `?>`

Which produces:

Boat 1 race scores: 0, 0, 0, 0, 0, 0
Boat 2 race scores: 0, 0, 2, 3, 3, 0
Boat 3 race scores: 0, 0, 0, 1, 0, 2
Boat 4 race scores: 3, 0, 0, 0, 0, 3

Boat 1 final score: 0
Boat 2 final score: 8
Boat 3 final score: 3
Boat 4 final score: 6

Team score: 17

## Log

• September 11, 2006 - Added solution 1.
© 2004-20 robson | cc unless stated