CS100M Spring 2006
Project 3
Due Thursday 3/9 at 6pm

Submit your files on-line in CMS before the project deadline. See the CMS link on the course webpage for instructions on using CMS. Both correctness and good programming style contribute to your project score.

You must work either on your own or with one partner. You may discuss background issues and general solution strategies with others, but the project you submit must be the work of just you (and your partner). If you work with a partner, you and your partner must register as a group in CMS and submit your work as a group.

Objectives

Completing this project will help you learn about vectors (1D arrays), matrices (2D arrays), strings, and vectorized code. Your best strategy for this project is to decompose each problem into simpler pieces. These simpler pieces correspond to subfunctions (i.e., "helper" functions that reside in the same file as the primary function). 

Comparing Strings & Comparing Vectors

Both of the tasks below require you to compare vectors (recall that a string is a vector of characters). When you compare two vectors using, for instance, "==" the result is a vector of logical values: 0 for false and 1 for true. So what does Matlab do if we try to use a vector comparison in an if-statement or a while-loop?  There isn't a single boolean value; there is a whole vector of boolean values. 

To make sense for an if-statement or a while-loop, we must condense this vector of boolean values into a single value, either true or false. Matlab provides two functions for doing this: any and all. Each of these function takes a single vector (or matrix) as its argument.  Function any returns true if and only if there is some value in the vector that is true (nonzero).  Function all returns true if and only if all values in the vector are true (nonzero).

For example, to check if two strings are equal, we can use the following code.

    if length(strA) == length(strB) && all(strA == strB)
        % Code doing something with the strings
    end

1. Censorship

Write a censoring function: censor(inString, censorWord). The function should return a modified version of inString in which every occurrence of censorWord in inString is replaced with x's. For example, if inString is 'Matt used Matlab to format mathematics' and the censorWord is 'mat' then the resulting string should be 'Xxxt used Xxxlab to forxxx xxxthexxxics'. Note that the result string uses the same capitalization pattern as inString.

Details:

Hint: Recall the functions in Section and in Lab that involved DNA pattern matching. This kind of function can be used to accomplish a useful subtask for the censorship problem.

2. Interactive Graphing

Create a steerable worm. The idea is to create a Matlab figure containing a moving "worm"; the worm can be steered using mouse clicks.  We provide the code to create a figure that responds to mouse clicks. Your job is to provide the code that draws the worm and allows it to move.

The worm's current position is represented by a 21-by-2 matrix (called worm) indicating the positions of all 20 segments of the worm; worm(1,:) represents the position of the worm's "head". Each segment of the worm is one unit long and it is oriented either vertically or horizontally. For example, the pairs (1,1), (1,2), (1,3), (1,4), (1,5) correspond to a worm with 4 segments in a vertical position. Note that the worm need not be a straight line! However, in every iteration, the worm must have precisely 20 one-unit-long segments. 

The user steers the worm by clicking somewhere within the plot. A function that detects mouse clicks and the coordinates of the position of the mouse is provided to you (makeClickableFigure.m). Your job is to draw the worm repeatedly to make it move in the selected direction. 

Hint: Break the problem into easier pieces. For instance, start with a "helper function" that draws the worm once using plot, taking care that the figure's axes do not change (i.e., use the axis function as described above).