Review problems for the midterm

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

Recursion

Write a recursive function that finds the sum of squares up to a given number n (passed as a parameter).

Solution:


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- 
Recursive sum squares
Author: Elena Machkasova elenam@morris.umn.edu 
Last modified: 4/21/09
--> 
<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>
CSci 1101: Recursive sum squares
</title>
<?php

function sum_squares($n) {
	if ($n == 1) {
		return $n*$n;	
	}
	else {
		return ($n * $n) + sum_squares($n-1);	
	}
}
?>
</head>
<body>
<p>
<?php
print sum_squares(5);
?>
</p>
</body>
</html>

SQL

Log in to PhpMyAdmin and write and test the following queries:

  1. Select posts (title, date, content, author, and number of comments) that don't have any comments
  2. Select posts written in February and later and order them alphabetically
  3. Select all posts that have a commnent with the word "project" in it. Display the content of the posts and the corresponding comments

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">
<!-- 
Recursive array printing - as nested lists
Author: Elena Machkasova elenam@morris.umn.edu 
Last modified: 4/21/09
--> 
<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>
CSci 1101: Recursive array printing -- as nested lists
</title>
<?php
// the function is given an array that may 
// contain nested subarrays. The function
// prints the array elements indenting each level
function print_nested_array($array) {
   print "<ol>";
   foreach($array as $item) {
     if (!is_array($item)) {
       print "<li>$item</li>";
     } else {
       print_nested_array($item);
     }
   }
   print "</ol>\n";
}
?>
</head>
<body>
<?php
$strings = array("Level one", array("Level two", "Another level two","A thread at level two", array("Level three", "More level three")), 
"Back to level one again");
//print "<pre>";
//print_r($strings);
//print "</pre>";
print_nested_array($strings);
?>
</body>
</html
http://csci1101sp09.morris.umn.edu/~elenam/1101_spring09/recursion/array_printing_lists.php

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://csci1101sp09.morris.umn.edu/~elenam/1101_spring09/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://csci1101sp09.morris.umn.edu/~elenam/1101_spring09/review2/array_remove.php


UMM CSci 1101