CS1109 Lab 10

Put the work for this lab into a new folder public_html\cs1109\lab10.

Part 1: Mean and standard deviation

Take a copy of the following files:

Your task is to complete statistics.php so that it outputs the sample mean (i.e. average) of the five numbers and their sample standard deviation. You must do this with foreach-loops, so that your solution will work for any length of array.

The sample standard deviation is the square root of the square of the sum of the differences between each of the numbers and the mean divided by how many numbers there are less one. The Web has many pages explaining how to compute the sample standard deviation, including:

(If you take your explanation of standard deviation from some other Web page, be aware that I want the sample standard deviation, where we divide by one less than the size of the sample, not the population standard deviation, where we would divide by the size of the sample.)

In your progam, don't worry about users who leave one or more of the textfelds empty or who enter nonsense into the textfields.

In your program, apart from sqrt, do not use any built-in functions, such as count, array_sum, etc. If in doubt, ask me.

Part 2: Getting dates

Take a copy of dates.html. Do not edit this form. It allows a user to enter two dates. Assume that the user does enter the dates correctly. In other words, ignore the possibility that s/he enters, e.g., 29 February 2011 or 12 Bananas 2014.

Write dates.php. The program outputs the word before if the first date comes before the second; it outputs same if the first date is equal to the first; or it outputs after if the first date comes after the second.

In your program, do not use any built-in functions. If in doubt, ask me.

Hint: Use an array of months.

Deadline for Parts 1 & 2 of Lab 10: 1pm, Tuesday 10th January.

If you have named your files and folders correctly, your work will be collected automatically at that time by my software.

Challenge exercise I

Remember that challenge exercises are always optional. They do not form part of your year's work and they are not worth any marks. They are designed for those students who finish the main exercise quickly and easily, and wish to explore further.

I expect that your solution to part 1 uses a sequence of two foreach-loops. That's fine. Leave it like that.

But it's a bit inefficient. If you're good at maths, or good at finding things on the Web, you should be able come up with a program that uses just one foreach-loop.

Here's the form again: statistics_challenge.html. Put your PHP into a file called statistics_challenge.php. Leave them both in your lab10 folder, and I will take a look at them.

Challenge exercise II

Remember that challenge exercises are always optional. They do not form part of your year's work and they are not worth any marks. They are designed for those students who finish the main exercise quickly and easily, and wish to explore further.

RPS-15 is a variation on Rock-Paper-Scissors, but with fifteen different weapons, instead of three. This image shows you the weapons and their relationships to each other (i.e. which beats which). (The original image comes from the Web site of David C. Lovelace, where you will also find that he has also worked out RPS-25 and RPS-101 — yes, with 25 and 101 weapons!)

In a file called rps15.html, create a form (based on rsp.html from lab sheet 9) but with 15 radio buttons. And in a file called rps15.php, write the program that plays the 15-weapon version of the game. Work on it in stages as follows:

  1. First, write a simple three-armed conditional that outputs either Computer wins, User wins or Draw. The key is to work out the mathematical relationship (similar to the one I gave in the lecture) for determining who wins.
  2. Next, modify your program so that it outputs the weapons. For example, instead of just saying Computer wins, it might say Computer's dragon beats user's lightning or User's snake beats computer's sponge, or Computer's air draws with user's air. (Hint: use something you've just learned.)
  3. Finally (and this bit is quite challenging), modify it again so that not only does it output the name of the weapon but it also outputs a more specific word than "beats". For example, Computer's rock blunts user's scissors. Some suggestions for these words are given towards the foot of David C. Lovelace's RPS-15 page.

Leave both files in your lab10 folder, and I will take a look at them.