Assigned Th 6/19/2008, due Fr 6/27 at 9pm. 50 points.
http://www.cs.nyu.edu/courses/summer08/G22.3033-002/
These are example solutions. Please keep in mind that often, there is not just one correct solution to a question. If you come up with different answers, then it may be that both your answers and these answers here are correct. Of course, these answers here may also contain mistakes. If you spot a mistake, please let me know so I can correct it.
|2.00, 3.00| = 3.61
|2.77, 4.16| = 5.00
0 => (2, 3)<br/>
1 => (0, 4)<br/>
2 => (-3, 4)<br/>
"cmp_vector" as a parameter to library
function usort(). That way, the library function makes a
call-back to function cmp_vector() whenever it needs to compare
two vectors for sorting.
"cmp_vector", or write a new
function, and pass a different string for the call-back. Here is an
example for the second solution:
function cmp_vector_by_x($a, $b) {
$aa = $a->x; $bb = $b->x;
return $aa < $bb ? -1 : $aa == $bb ? 0 : 1;
}
usort($a, "cmp_vector_by_x");
No user name or password required.
User name grader, password hype3.
User name grader, password hype3.
<html>
<head>
<?php
function convert($qty, $unit, $ing) {
$cup2g = array('flour' => '110', 'sugar' => 225, 'butter' => 225);
$volume = array('cup' => 1, 'tbsp' => 16, 'tsp' => 48, 'ml' => 236);
$weight = array('lb' => 1, 'oz' => 16, 'g' => 453);
if (array_key_exists($ing, $cup2g) && array_key_exists($unit, $volume)) {
$qty = 1.0 * $qty * $cup2g[$ing] / $volume[$unit];
$unit = 'g';
} elseif (array_key_exists($unit, $volume)) {
$qty = 1.0 * $qty * $volume[ml] / $volume[$unit];
$unit = 'ml';
} elseif (array_key_exists($unit, $weight)) {
$qty = 1.0 * $qty * $weight[g] / $weight[$unit];
$unit = 'g';
}
return ((int)($qty + .5)) . " $unit $ing";
}
?>
</head>
<body>
<?php if(!empty($_GET['qty'])) { ?>
Converted result:
<?php echo convert($_GET['qty'], $_GET['unit'], $_GET['ing']); ?>
<br/>
<?php } ?>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method=get>
<table border=1>
<tr><td>Quantity
<td><input type="text" name="qty" value=<?php echo $_GET[qty] ?> />
<tr><td>Unit
<td><input type="text" name="unit" value=<?php echo $_GET[unit] ?> />
<tr><td>Ingredient
<td><input type="text" name="ing" value=<?php echo $_GET[ing] ?> />
</table>
<input type="submit" value="Submit" />
</form>
</body>
</html>