CS100M Spring 2006
Project 2
Due Thursday 2/15 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 developing algorithms and about loops and selection. Compared to Project 1, this project is more open-ended. Any assumptions you make should be stated using comments.

Do not use arrays (vectors) for this project.

1. Rolling Dice

For this problem, you need to write two programs (Matlab scripts): oneTurn.m and averageRolls.m.  

Craps is a popular casino game based on rolling a pair of dice.  Here, we're not interested in the gambling aspects of the game; instead, we want to estimate the expected number of rolls for a single turn.  Here are the rules for how many rolls take place.

Write a program (a Matlab script) called oneTurn.m that simulates a single turn.  As it runs, it should print the value of each roll. After all the rolls are done it should print the number of rolls that occurred.  Here's an example of one way to arrange the program's output.

>> oneTurn
8 4 4 5 7 -> 5 rolls

Write a separate program called averageRolls.m that uses the first program over several turns to determine the average number of rolls per turn.  Your program should ask the user for the number of turns, run oneTurn.m the specified number of turns, and then report the average number of rolls per turn.

2. Approximation via Taylor Series

The Taylor series for ex is given by the sum 1 + x/1 + x2/2 + x3/6 + ... + xk/k! + ... where k! is 1*2*...*k.  We can use this series to approximate ep for various values of p.

For this problem, write a program (a Matlab script) called approxE.m to see how many terms are necessary to get a good approximation.  Your program should request two inputs from the user: p (the exponent) and epsilon (the desired accuracy).  Your program should then determine and report the number of terms of the Taylor series that are necessary to get within epsilon of the correct value for ep.  We'll use Matlab's built-in function exp as our definition of accurate; in other words, we assume that exp(p) is the correct value for ep.  Note that two values a and b are within epsilon of each other if |a-b| < epsilon.

3. Twinkle, Twinkle Little Star

Create a program (a Matlab function) called star.m that draws a star. Your function should have two arguments: points (the number of points) and inc (the increment). For instance, for points=5 and inc=2, five points are equally spaced around a circle and then each point is connected to the point two forward from it; this creates a traditional five-pointed star. For points=5 and inc=1, your program should draw a pentagon. Example output for star(7, 2) appears below.