CS 104 - Lab 8
Design and test tables will be started in lecture/lab the week prior.
Due in Blackboard Assignment (one file named lastnameLab8.zip) Week 13
before Lab (8:35am)
put all the m-files and diary file in one zip file
Objectives:
Tasks:
1. (4 points) Understand and correct MATLAB conditions.
For these problems you do not need to write entire matlab functions or scripts, just the condition statements requested.
1a. Write a selection statement that will print 'warm' if the temperature is between 80 and 90, inclusive.
1b. Write a selection statement that will print 'hot' if the temperature >=80, 'pleasant' if the temperature >= 60, 'cool' if the temperature is >=45, 'cold' otherwise.
1c. If integer variable currentNumber is odd, change its value so that it is now 3 times currentNumber plus 1, otherwise change its value so that it is now half of currentNumber
1d. Find the largest of three scalar numbers x y and z and assign the value to the variable largest.
2. (12 points) Write and test a matlab function or script requiring conditions.
2a. Create a test plan with sample data for the following problem. You do not have to calculate expected results, but please explain why each test case was chosen as related to the problem. Also include a description of any error checking on the input values that you should do.
A cell phone company uses the following table to determine the monthly cost of mobile share plan for up to 10 devices (at least one smartphone). The inputs to monthlyCost function are Amount of Data, # of smartphones, # of other devices.
Amount of Data |
1 GB data |
6 GB data |
Cost for Data |
$40 |
$90 |
Additional costs for devices |
+$45 per smartphone + $10 per other device |
+$35 per smartphone + $10 per other device |
You may need more rows than are shown in the test table.
Test Case Reason | Sample Data | Expected Result (manually calculate) |
Verified? |
Error Checking on Input Values |
2b. Write a MATLAB function called monthlyCost as described above. If any of the input values are incorrect, return zero.
2c. Write a matlab function daysInMonth(month, year) which takes two arguments, the month number (1=January, . . .12=December) and a postive year, and returns the integer number of days in that month/year. You should call the below leapYear(year) function. If the month or year is invalid, return 0;
function value = leapYear( year ) %LEAPYEAR returns a boolean true if year is a leap year, else returns false if (~((mod(year,4)==0) && ((mod(year,100)~=0)||(mod(year,400)==0)))) value=false; else value=true; end end
Create a test table and write matlab function to solve the problem.
Test Case Reason | Sample Data | Expected Result (manually calculate) |
Verified? |
3. (4 points) Understand and correct MATLAB iteration.
3a. (1 points) Write a m-file script to prompt the user to input ten real number temperature readings one at a time and calculate the average.
3b. (1.5 points) Write a matlab script that will generate a random integer between 1 and 10 inclusive and will NOT display this value; this is the 'secret number'. The script will prompt the user to 'guess' this secret number until the user guesses the secret number. After each guess the script will tell the user if the guess is too high or too low. When the user guesses the secret number correctly, the script replies with a message including how many guesses it took. Here are two sample runs:
>> guess Guess the secret number, 1-10: 5 too high Guess the secret number, 1-10: 3 too high Guess the secret number, 1-10: 2 You guessed it in 3 guesses!
>> guess Guess the secret number, 1-10: 5 too low Guess the secret number, 1-10: 9 too high Guess the secret number, 1-10: 7 You guessed it in 3 guesses!
Copyright CS, Illinois Institute of Technology