CS100M Spring 2006
Project 1
Due Thursday 2/2 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.


0. Objective

Completing all the tasks in this assignment will help you: First skim, and then carefully read the entire assignment before starting any tasks!

1. ASCII, course information and policies

Topics: what is ASCII?, creating an ASCII (text) file, course policies and procedures, accessing CS100 web pages

Read the following web pages to learn about ASCII (and also about writing your resume!). You don't have to read the entire documents--just the first page is sufficient. You may search the Internet for different articles on ASCII if you like.

Next, download the plain text file p1q1.txt from the course website. Answer the questions by editing the file with any text editor (e.g., Notepad, Wordpad, even MATLAB's editor) or word processor. Be careful to keep it as a plain text file, or save the file specifically as a plain text file if you use a word processor such as Microsoft Word.

Answer the questions by writing your responses after each question. Be neat! Keep line lengths to at most 70 characters. Again, be sure to save your work as a plain text file.


2. Areas for geometric shapes

Topics: Selection (the if construct), arithmetic operations

Write a program to compute the area of a geometric shape where the shape can be chosen by the user: either a circle, a rectangle, or a triangle.  Some sample output from such a program appears below.

>> P1Area
Select a shape (1=circle, 2=rectangle, 3=triangle): 1
Choose a radius: 4
The area is 50.265482
>> P1Area
Select a shape (1=circle, 2=rectangle, 3=triangle): 2
Choose a width: 4
Choose a height: 5
The area is 20.000000
>> P1Area
Select a shape (1=circle, 2=rectangle, 3=triangle): 3
Choose a side length: 3
Choose a side length: 4
Choose a side length: 5
The area is 6.000000

Your program should use an if-elseif-else construct to distinguish between the different shapes and to detect a bad shape choice (i.e, not 1, 2, or 3). You should, of course, be familiar with the formulas for the area of a circle and the area of rectangle.  For the area of a triangle given its sides, you can use Heron's formula (also called Hero's formula):

A = [s(s-a)(s-b)(s-c)]1/2

where a, b, c, are the lengths of the three sides of a triangle and s is half the perimeter.

Be sure to check for a bad shape choice (i.e., not 1, 2, or 3).  If a bad choice is made, your program should use the error function: error('This is an error message').  When the error function is executed, it will halt the program and print the error message on the Command Window. Your error message should be something meaningful that helps a user understand the causes of the error.

Another kind of error can occur: an impossible triangle error. A triangle is impossible if one of its sides is longer than the sum of the other two sides.  If this happens then Heron's formula will fail (it produces an imaginary area, but depending on how your program prints the area, only the real part may be printed; thus, it might appear in the output as 0.0). For this assignment, it is not necessary to detect or respond to this error, but if you have extra time, you can modify your program to produce a useful error message when an impossible triangle occurs.


3. Plotting a random rectangle

Topics: Built-in function rand, introduction to graphics

Download the program rectangle.m and run it. It should draw a rectangle.

Your job is to modify this program so that (1) it uses a random rectangle instead of the specific rectangle currently written into the program and (2) it draws the two corner points that define the rectangle.  The random coordinates that you use for your rectangle corners should all be in the range 1 to 9. The parts of the program where your changes should occur are highlighted in the program; you shouldn't make any changes outside these specified regions.

The statement v = rand(1) assigns to variable v a random number in the range of 0 to 1. Note that the 1 in the parentheses indicates that one number is to be generated --- it has nothing to do with the range. So how do you get a random number within a different range? First, the statement v = rand(1) gets you a real number in the range of 0 to 1. Next, scale (think multiply) and shift (think add) the value v to get the range you want.

To draw a point (x,y), use plot(x,y,'o'). This tells Matlab to draw the point (x,y) by using a small circle to indicate the point.  There are several other characters that can be used instead of the letter 'o'. You can type help plot in the Command Window to see some of the other plot options.  For instance, plot(x,y,'*r') will indicate the point (x,y) by drawing a small red star.