Lab 7, Part 2
-------------
This was the conversion from feet and inches to metres and centimetres.
It was marked out of 4. If you didn't get 4, here's possible reasons why...
We start by getting the user's input and converting to ints:
$feet = (int) $_GET['feet'];
$inches = (int) $_GET['inches'];
If you converted $feet to (float) or failed to convert, you lost marks.
Next, we need the person's total height in inches:
$total_in_inches = $feet * 12 + $inches;
If you threw an extra (int) into this, you're overdoing the conversions
and are on your way to losing more marks.
Some of you decided instead to omit this step and to convert the feet and
the inches separately. While it works OK here, in general I think it's
not the best solution.
Then, we convert the total height from inches to cm:
$total_in_cm = $total_in_inches * 2.54;
Again, we don't need to use (float), and (int) would be totally wrong.
(You could round this, which is OK - see later. Some of you used ceil
here - that's not correct.)
Now, we can get the metres. There are two possibilities. Here's one:
$metres = floor( $total_in_cm / 100 );
Here's the alternative (note the parentheses):
$metres = (int) ( $total_in_cm / 100 );
And now, we get the remaining centimetres and round to the nearest
integer. There are two options.
a) Suppose you computed $total_in_cm without rounding it, in the way
I showed above. Then you cannot use modulus (%) here. This is because
modulus always gives an integer by rounding down, whereas I asked
you to round to the nearest cm. So you had to do the calculation of
the remainder yourself and round it:
$centimetres = round( $total_in_cm - $metres * 100);
Some of you threw in an extra (int) - I don't know why. And some of you
used round's optional parameter, round( $total_in_cm - $metres * 100, 1),
but that doesn't round to an integer.
b) Suppose instead that you did round $total_in_cm earlier. Then you
can use modulus:
$centimetres = $total_in_cm % 100;
Finally, you had to output $metres and $centimetres in a table.
You may have lost marks if your program did a lot of redundant
work. And, of course, you lost marks (or scored zero) if your program had
compile-time or run-time errors.
Lab 7, Part 4
-------------
This was the water tax. It ws marked out of 6.
I presented a good solution to this in the lectures.