CS1109 Lab 15

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

Part 1: Don't foul your own nest

In a file called nested.php, write a PHP script that uses nested for-loops to output each of the following:

Square

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

Triangle 1

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Triangle 2

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

Part 2: Christmas is coming

Write christmas.php, which outputs the lyrics of The Twelve Days of Christmas, but with Call of Duty gifts (from WilzoHD's post to the Call of Duty forum). The gifts are as follows:

  1. a Spas-12 Hitmarker x3
  2. two Juggernauts
  3. three Moroccan Hens
  4. four Camping Nerds
  5. five Kills Confirmed
  6. six Man Collateral
  7. seven Suicides
  8. eight Host Migrations
  9. nine Stun Grenades
  10. ten Overpowered Weapons
  11. eleven Point Streak
  12. twelve Noobs-a-Running

Each verse introduces the next gift, but all the previously introduced gifts are repeated — in reverse order! For example, here are the first three verses:

On the first day of Christmas, Call of Duty sent to me
a Spas-12 Hitmarker x3.

On the second day of Christmas, Call of Duty sent to me
two Juggernauts,
and a Spas-12 Hitmarker x3.

On the third day of Christmas, Call of Duty sent to me
three Moroccan Hens,
two Juggernauts,
and a Spas-12 Hitmarker x3.

You will need one or two arrays and a nested loop. It may be better to use for-loops, rather than foreach-loops.

If you want to increase the challenge of the exercise a little, then try to get the commas and the word "and" to come out in exactly the right places (see above).

Part 3: The Anatomy of Grey

In this exercise, you will take a colour image and convert it to greyscale. The idea is simple: visit each pixel and set its colour to some shade of grey. You may have realised that in RGB colours, if the amount of red, green and blue are the same, then the colour is some of shade of grey. So, for each pixel, we will calculate its luminescence or intensity, which is simply the average of its amount of red, green and blue, i.e. L = (R + G + B) /3. And we will set the pixel so that the amount of red is L, and similarly the amount of green and blue.

Save copies of these files:

Complete the program. Here are the built-in functions that you may need:

Important: You must not use the imagefilter function!

Part 4: Life's a blur

In this exercise, you will take an image and blur it. How? Take a pixel. Add together its R value and the R values of all of its neighbours. (There may be up to 8 of them.) Then divide by how ever many things you added together. In other words, you've calculated the average R value. Do the same for G and B values. Set this pixel's colour using the three averages. Do this to all the pixels.

Take a copy of blur.phps. Save it as blur.php.

Complete the program using the same built-in functions that you used in Part 3. Again, you must not use the imagefilter function!

Deadline: 1pm, Tuesday 14th February.

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

Challenge exercise: Junior Photoshop

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.

Write a photo editing program, like PhotoShop! For example, here's mine. It allows a user to upload a photo, and request a greyscale version, or a blurred version, etc. The real fun is in offering a wider range of editing operations. Mine, for example, allows edge detection, which finds the outline of the object in the photo and produces a kind of pencil sketch.

Some ideas (which I can discuss with you):

All of this becomes easier if you think about developing a library of helpful functions.

Call the files pshop.html, pshop.php, etc. I'll take a look at any files whose name begins "pshop".