CS1109 Lab 10

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

Part 1

Note: This is close to a couple of programs we've seen in this week's lectures.

Take a copy of sentence.html; do not modify it. It allows a user to enter some words, each separated by a space.

Write sentence.php which outputs the mean length of the words in the user's input, and also the standard deviation.

The mean (also called the average) is obviously the total lengths of the words (don't count the spaces!), divided by the number of words. For example, if the user enters "chicken and chips", then the mean is 15/3 = 5.

The standard deviation involves summing the squares of the differences between the lengths of the words and the mean; dividing by how many words there are; and then taking the square root. For example, for "chicken and chips", we already have the mean (5), so we calculate (7 - 5)2 + (3 - 5)2 + (5 - 5)2, which is 8. We then divide this by the number of words, i.e. 8/3. And we take the square root of this, which gives roughly 1.6.

If my explanation of standard deviation is no good for you, here is another. It explains it in words and pictures. Then, for those who prefer it, it also gives two formulae — it's the first formula (which it calls the Population Standard Deviation) that we are using here.

In your program, you'll want to use some built-in functions. You should be able to get by with just trim, explode, strlen and sqrt.

Part 2

Warning. I remind you that your work should be your own. If there is 'collusion', I will replace the lab sheet by a class test.

Take a copy of words_to_int.html; do not modify it. It allows a user to enter words separated by spaces, but the only words s/he is supposed to enter are the following: zero, one, two, three, four, five, six, seven, eight, and nine. E.g. s/he might enter "two four one". You can assume that your user is 100% cooperative: s/he never enters any incorrect input, e.g. s/he never enters "two  four one" (which has an extra space) or "two Four one" or "to for won". This will make the exercise a little bit easier.

You must write words_to_int.php, which computes and outputs the integer that corresponds to the words that the user has entered. For example, if the user enters "two four one", your program computes the integer 241 and outputs it; if the user enters "three one one two", your program computes the integer 3112 and outputs it.

Important

Deadline for Lab 10: 1pm, Tuesday 15th January.

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

Challenge exercise

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.

Warning: very challenging!

Take a copy of int_to_words.html do not modify it. The user enters an integer between 0 and 1 billion (which we'll take to be an American billion, 1000000000).

Write int_to_words.php, which takes the user's integer and outputs a corresponding phrase of English. E.g. if the input is 1200427, the output is one million two hundred thousand four hundred and twenty-seven.

You may want to use str_split, which is a built-in function, similar to explode.

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