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). Below is the starting code.


<!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
           // the function goes here
?>
</head>
<body>
<p>
<?php
  // function call goes here
?>
</p>
</body>
</html>

http://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/review2/recursion.php

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

Write the following queries (you may test them in your own database):

  1. Change the title of the post with an id=19 to "A testing post"
  2. Add a new user with a login name "guest", email "guest123456@gmail.com", and a password "blah98765" (just in plain text).

Regular expressions

Write regular expressions for the following patterns:

  1. A string consisting of all letters or all digits, but not a mix and not any other symbols.
  2. A string of alternating letters and digits.
  3. A string that has at least one occurrence of the digit 5.
  4. A string that has at least two occurrences of the digit 5.
  5. A string with exactly two occurrences of the digit 5.

You may test them here: http://www.spaweditor.com/scripts/regex/index.php. Important: please set type to POSIX. Note that the default is case-insensitive.

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://csci1101sp10.morris.umn.edu/~elenam/1101_spring10/review2/array_printing_as_lists.php

Write a function that removes all occurrences of an element from an array (advanced).

Use the following program as a starting point. Recall that unset removes an element. Also note that in order to change an array you must pass it by reference (using &)


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


UMM CSci 1101