Previous Section  < Day Day Up >  Next Section

4.7 Exercises

  1. According to the U.S. Census Bureau, the 10 largest American cities (by population) in 2000 were as follows:

    • New York, NY (8,008,278 people)

    • Los Angeles, CA (3,694,820)

    • Chicago, IL (2,896,016)

    • Houston, TX (1,953,631)

    • Philadelphia, PA (1,517,550)

    • Phoenix, AZ (1,321,045)

    • San Diego, CA (1,223,400)

    • Dallas, TX (1,188,580)

    • San Antonio, TX (1,144,646)

    • Detroit, MI (951,270)

    Define an array (or arrays) that holds this information about locations and population. Print a table of locations and population information that includes the total population in all 10 cities.

  2. Modify your solution to the previous exercise so that the rows in result table are ordered by population. Then modify your solution so that the rows are ordered by city name.

  3. Modify your solution to the first exercise so that the table also contains rows that hold state population totals for each state represented in the list of cities.

  4. For each of the following kinds of information, state how you would store it in an array and then give sample code that creates such an array with a few elements. For example, for the first item, you might say, "An associative array whose key is the student's name and whose value is an associative array of grade and ID number," as in the following:

    $students = array('James D. McCawley' => array('grade' => 'A+','id' => 271231),
    
                      'Buwei Yang Chao' => array('grade' => 'A', 'id' => 818211));

    1. The grades and ID numbers of students in a class.

    2. How many of each item in a store inventory is in stock.

    3. School lunches for a week ?the different parts of each meal (entree, side dish, drink, etc.) and the cost for each day.

    4. The names of people in your family.

    5. The names, ages, and relationship to you of people in your family.

    Previous Section  < Day Day Up >  Next Section