CSci 1101 Problem set 4. Due Friday, March 28 at midnight

After you complete a question, please make a copy of your file with a different name and start next question in the new file. This way you always have a working file with a partial solution and are not running a risk of overwriting something that is already working. It is OK to submit only one file at the end if everything is working properly, but if there are issues with some questions, it's better to submit them separately.
Please send me an e-mail with the file name(s) when you are done.

Question 1

Write and test a function that takes an array of planet moons, indexed by the planet name (see below) and prints it out as an ordered list of planets, where all planets that have moons are followed by an ordered list of moons. Also, for each planet print out how many moons it has.

Here is the data array, copy/paste it into your program.

<?php 
// larger moons of planets of Solar system (those bigger than 45 km in diameter)
// Source: http://en.wikipedia.org/wiki/Planet
$moons = array("Mercury" => array(),
		 "Venus" => array(),
		 "Earth" => array("Moon"),
		 "Mars" => array("Phobos","Deimos"),
		 "Jupiter" => array("Metis", "Amalthea","Thebe", "Io", "Europa",
		 "Ganymede", "Callisto", "Himalia", "Elara", "Carme", "Pasiphae"),
		 "Saturn" => array("Prometheus", "Pandora", "Epimetheus",
		 "Janus", "Mimas", "Enceladus", "Tethys", "Dione", "Rhea",
		 "Titan", "Hyperion", "Iapetus", "Phoebe"),
		 "Uranus" => array("Bianca", "Cressida", "Desdemona", "Juliet",
		 "Portia","Rosalind","Belinda","Puck","Miranda","Ariel",
		 "Umbriel","Titania", "Oberon", "Caliban", "Sycorax"),
		 "Neptune" => array("Naiad", "Thalassa", "Despina", "Galatea",
		 "Larissa", "Proteus", "Triton", "Nereid"));
?>
The output should start like this:
  1. Mercury has no moons
  2. Venus has no moons
  3. Earth has 1 moon:
    1. Moon
  4. Mars has 2 moons:
    1. Phobos
    2. Deimos
To test if an array is empty, you may use

if ($array == array())
where instead of $array you use the array that you want to check.
It's OK if you print "Earth has 1 moons", but you get extra credit if you handle the plural/singualr correctly.

Question 2

Given the same data as above, write and test a function that finds the planet with the largest number of moons. The function should return the name of the planet.

Question 3

Write a function add_moon that takes the array of moons, a name of a planet, and a name of a moon, and adds the moon to the array of moons for that planet. For instance, the call to the function

add_moon($moons, "Saturn", "Siarnaq"); 
will add Siarnaq to the array of Saturn's moons. Use the function that you wrote for question 1 to test the add_moon function.

Important hints:


This page is a part of CSci 1101 course web site.