CS 104 - Lab 9
Due in Blackboard Assignment (one file named lastnameLab9.zip) Week 14
before Lab (8:35am)
put all the m-files and diary file in one zip file
Objectives:
Tasks:
1. (6 points) Write and test matlab functions or scripts requiring iteration.
Write a matlab funtion called hitTarget that will accept one scalar argument, the target sum of two random 6-sided dice rolls. The function generates and displays two random numbers between 1 and 6 (like dice rolls) until the sum of the two die equals the target argument. Keep track of how many rolls it takes. Here is a sample run.
>> hitTarget(8) 5 2 6 3 2 2 4 3 3 5 Target met, it took 5 rolls
2. (7 points) Write and test matlab functions or scripts requiring iteration.
You are burning some music CDs for a party. You would like to maximize
your use of space on the CD, which holds 80 minutes of music. To do so,
you want to keep track of how much time is left on the CD after you burn
each song to it. Design and write a MATLAB m-file script that prompts the
user for a song length. The time for a song is entered as seconds (assume
correct). For example, if a song takes 7 minutes and 42 seconds to play,
the data entered for that song would be 462 After each song length has
been read, the application should check if the song will fit on the CD,
if it does, print a message indicating the time remaining on the CD in
minutes and seconds and prompt the user again if there is any room left
on the CD. If a song does not fit, print a message "Song will not
fit" and prompt the user again. Also stop the prompting if the user
enters a 0 or negative number of seconds for a song. Here is a sample complete
run
Enter a song length in seconds: 462
Time Left:72 min. and 18 sec.
Enter a song length in seconds: 300
Time Left:67 min. and 18 sec.
Enter a song length in seconds: 3900
Time Left:2 min. and 18 sec.
Enter a song length in seconds: 150
Song will not fit
Enter a song length in seconds: 100
Time Left:0 min. and 38 sec.
Enter a song length in seconds: 0
Do an Input-Process-Output design, create a test table and write the
Matlab script to solve the problem.
3. (7 points) Write and test matlab functions or scripts requiring iteration.
Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice? Write a matlab program to help you decide. HINT: Make a function to simulate one instance. Argument to function is a Boolean, does the player switch or not. Function returns a Boolean true of player wins, false if player loses. Call the function many times for each player strategy, and save the results to analyze and to determine the best player strategy
Copyright CS, Illinois Institute of Technology