CS100M Spring 2007
Project 1
Due Thursday, Feb 1 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. Hunt for the Line

Topics: Built-in function rand, selection (the if construct), introduction to graphics

Download the program lineHunt.m and run it. A graphics window will pop up and the message near the top (the title area) says to click in the window. Each time you click, a circle will be drawn showing where you clicked; also, the title of the graphics window will change to show whether you hit or missed the hidden horizontal line (hint: the line is "hidden" at y = 4.5).

Read the program to make sure you understand what it does. Don't worry about the early commands to set up the figure window, but here's how the plot statement works: plot([x1 x2] [y1 y2]) draws a line from the point (x1,y1) to (x2,y2) while plot(x,y) draws a single point. The statement title('hello there') displays the text 'hello there' as the title of a figure.

Your job is to modify this program:

  1. Your program should choose a random y-coordinate for the horizontal line instead of the specific y-coordinate currently written into the program. The line should be randomly placed within the range of the y-axis (between 1 and 9).
    Hint: 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. 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.
     
  2. Your program should give more information to the user by indicating whether the user's point is too high or too low.  In other words, you should change the message to indicate either "Too high!" or "Too low!" instead of just "Miss!". 
     
  3. You should also modify the way the program marks the location of the user's mouse click so that the way in which the point is drawn indicates too high or too low.  The statement plot(x,y,'r*') draws a red asterisk at point (x,y). Experiment with other markers!  'r' for red, 'g' for green, 'b' for blue, 'x' for an x, 'o' for a small circle, '+' for, well, you can guess... Use your favorite markers!

3. Grade Cutoffs

Topics: Selection, input, and printing (fprintf)

Find the course-grade cutoff values on the Syllabus page of the course website. Write a program to prompt for a score and then print out the corresponding course grade according to the cutoff value on the course website.  Note that some scores correspond to more than one possible grade (e.g., 90 is either an A or a B).  Some sample output for such a program appears below.

>> gradeSol
What is the score? 50
The grade is F
>> gradeSol
What is the score? 60
The grade is D
>> gradeSol
What is the score? 70
The grade is C
>> gradeSol
What is the score? 80
The grade is B
>> gradeSol
What is the score? 90
The grade is A or B
>> gradeSol
What is the score? 100
The grade is A
>> gradeSol
What is the score? 110
??? Error using ==> gradeSol
Score too large

You'll want to make use of built-in functions input, error and fprintf.  Recall that information about any function can be found by typing "help funcName" at the Command Window prompt.

Use the input function to get a score. For example, the instruction x = input('Give me input. ') will print "Give me input." on the Command Window and will then wait for user input, storing the result in variable x.

If a bad score is given (i.e., greater than 100 or less than zero), 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 the user understand the causes of the error.

You've seen examples of the use of fprintf in lecture.  fprintf('Hello! My name is %s.\n', name) will print the specified greeting, but with %s replaced by the string held in variable name.  Recall that \n represents an end-of-line marker.