You are on page 1of 2

Omar Carrillo

Solutions
Homework 2
Assignment

Problem 1
Problem 5

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

Problem 3 New York, NY (8,008,278 people)


Los Angeles, CA (3,694,820)
Problem 4 Chicago, IL (2,896,016)
Houston, TX (1,953,631)
Problem 5 Philadelphia, PA (1,517,550)
Phoenix, AZ (1,321,045)
Problem 6 San Diego, CA (1,223,400)
Problem 7 Dallas, TX (1,188,580)
San Antonio, TX (1,144,646)
Problem 8 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.

Solution
<?php

# define the population array


$array = array(
"New York, NY" => 8008278,
"Los Angeles, CA" => 3694820,
"Chicago, IL" => 2896016,
"Houston, TX" => 1953631,
"Philadelphia, PA" => 1517550,
"Phoenix, AZ" => 1321045,
"San Diego, CA" => 1223400,
"Dallas, TX" => 1188580,
"San Antonio, TX" => 1144646,
"Detroit, MI" => 951270
);

W eb page converted to PDF w ith the PDFmyURL PDF creation API!


?>

<table border="1" cellspacing="0" cellpadding="4">


<tr>
<th>City</th>
<th>Population</th>
</tr>
<?php foreach ($array as $key=>$value) {
echo "<tr><td>$key</td><td>".number_format($value)."</td></tr>";
}
echo "<tr><th>Total</th><th>".number_format(array_sum($array))."</th></tr>";
?>
</table>

Output

City Population
New York, NY 8,008,278
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
Total 23,899,236

CS 479 - PHP Programming - Summer 2009 - NMSU

W eb page converted to PDF w ith the PDFmyURL PDF creation API!

You might also like