CSci 1101 Problem set 4. Due Thursday, March 29 at 10pm

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.

// larger moons of planets of Solar system (those bigger than 45 km in diameter)
// Source:
$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",
		 "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.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.