Lab 12, Part 1 -------------- This was where you re-wrote the program to compute how much cash (in cents) you have in your pockets. It wasn't graded. But I want to show you my answers - because some of yours were much longer than they needed to be. First, you needed to create an array, showing the value (in cents) of each type of coin/note: $values = array('num_1c_coins' => 1, 'num_2c_coins' => 2, 'num_5c_coins' => 5,..., 'num_50e_notes' => 5000); Then initialise a total: $total = 0; Then you needed a foreach-loop. In this program, you can choose which array the foreach-loop will cycle through: will it cycle through $values, or will it cycle through $_GET? For the solution to this exercise, you can get either to work. Here's the solution when you cycle through $_GET: foreach ( $_GET as $key => $qty ) { $total += $values[$key] * $qty; } You look at each piece of data from the user, obtain the corresponding value from $values, and add their product into the running total. Here's the ALTERNATIVE, where you cycle through $values: foreach ( $values as $key => $worth ) { $total += $_GET[$key] * worth; } One line completes the program: echo "

{$total}

"; Just seven lines in all (including squiggly brackets)! Lab 12, Part 4 -------------- This was checking a student's choice of second-year modules. It was marked out of 10. You scored 5 out of 10 if you had a working solution but one that hard-coded most of the checking into if-statements. Well done on getting a working program, but your program lacks generality. Your scored 8 out of 10 if you had a working solution but you relied on the fact that, at the moment, there are 8 core modules in each stream, or you relied on ther fact that, at the moment, the core modules sum to 50 credits in each stream, or if you relied on other similar facts. Well done on getting a working program. But, the problem with your solution is it lacks generality and therefore it lacks maintainability. Suppose, for example, that CS2501 (5 credits) and CS2502 (credits) are deleted and replaced by CS2512 (10 credits). In a good solution, we'd just remove two elements from the arrays and an insert another element into the arrays (In other words, we just update the DATA). But in your solution, we have to modify the rest of the program too (if-statements, etc.) You scored 10 out of 10 if you had a working solution and did not hard-code any of these facts into your code. Well done! (If you scored 4 rather than 5, or if you scored 6 or 7 rather than 8, or if you scored 9 rather than 10, then your program has a small extra bug or inefficiency in it. If you scored less than 4, you walked away from the lab sheet without trying hard enough.) Here's my solution (but remember there are millions of alternatives that are just as good): 15, "CS2501" => 5, "CS2502" => 5, "CS2503" => 5, "CS2504" => 5, "CS2505" => 5, "CS2506" => 5, "CS2507" => 5, "CS2508" => 5, "CS2509" => 5, "CS2510" => 5, "CS2511" => 5, "GE0002" => 10, "HS0128" => 10, "FR1105+FR1107" => 10, "FR2105+FR2107" => 10, "MA1057+MA1058" => 10); $core_cs = array("CS2500", "CS2501", "CS2502", "CS2503", "CS2504", "CS2505", "CS2506", "CS2507"); $core_web = array("CS2500", "CS2501", "CS2503", "CS2505", "CS2508", "CS2509", "CS2510", "CS2511"); $REQUIRED_NUM_CREDITS = 60; $stream = $_GET['stream']; if ( isset( $_GET['choices'] ) ) { $choices = $_GET['choices']; } else { $choices = array(); } $is_legal = true; $core = ($stream == 'cs') ? $core_cs : $core_web; foreach ($core as $c) { if ( ! in_array($c, $choices) ) { $is_legal = false; } } $total = 0; foreach ( $choices as $c ) { $total += $credits[$c]; } if ( $total != $REQUIRED_NUM_CREDITS ) { $is_legal = false; } echo $is_legal ? "Yes" : "No"; ?>