Review problems for the midterm

Note that these problems are intended as a review and NOT as sample problems.

What will be printed by this program?


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
What will be printed by this program? 
Author: Elena Machkasova
Last modified: 4/22/08
-->
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<?php
$names = array("Carol Andersen", "Joe Brown", "Mary Green", 
		"Andrew Peterson", "Fred Simpson", "Susan Smith");
?>
<title>
What will be printed by this program? 
</title>
</head>
<body>
<h3>What will be printed by this program? </h3>

<table border="1">
<?php
for ($i = 0; $i < count($names); $i = $i + 1) {
	if (($i % 2) == 0) print "<tr>\n";
	print "<td>$names[$i]</td>\n";
	if (($i % 2) != 0) print "</tr>\n";	
}
?>
</table>

<table border="1">

<?php
$tr = true;
foreach($names as $name) {
	if ($tr) print "<tr>\n";
	print "<td>$name</td>\n";
	if (!$tr) print "</tr>\n";
	$tr = !$tr;
}
?>
</table>
</body>

</html>

http://rynite.morris.umn.edu/~elenam/1101_spring08/review2/what.php

Write a function that removes all occurrences of an element from an array

Use the following program as a starting point:


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Write a function that removes all occurrences of an element 
from an array
Author: Elena Machkasova 
Last modified: 4/22/08
-->
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>

Write a function that removes all occurrences of an element 
from an array
</title>
<?php 
$stuff = array("apples", "oranges", "more apples", "oranges",
	"bananas", "kiwi", "apples", "strawberries");
function print_array($the_array) {
	print "<p>";
	foreach ($the_array as $element) {
		print "$element ";	
	}
	print "</p>\n";	
}

// write the function "remove" that takes an array and an element 
// and removes all copies of the element from the array
?>
</head>
<body>

<h3>Write a function that removes all occurrences of an element 
from an array </h3>

<?php
print_array($stuff);
// call the function to remove "apples" from the array, print it out

// call the function to remove "kiwi" from the array, print it out

// call the function that to remove "radishes" (or somehing else that's NOT in 
// the array, print out the array 
?>
</body>
</html>

http://rynite.morris.umn.edu/~elenam/1101_spring08/review2/array_remove.php


UMM CSci 1101