Lab 6

advertisement
Lab 6
CS2503
Branching
1. Given: v = [4, -2, -1, 5, 0, 1, -3, 8, 2] and w = [0, 2, 1, -1, 0, -2, 4, 3, 2]. Evaluate the
following expressions without Matlab, and then check your answers with Matlab. Record
your answers on this document:
a. v >= w ____________________________________________
b. w ~= v ____________________________________________
c. v < w
____________________________________________
d. What happens if v and w are not the same length?
2. Write a program in a script file that determines the real roots of a quadratic equation
a  x 2  b  x  c  0. Name the script quadroots. The script will prompt the user to
enter the coefficients a, b, and c then computes any real roots if they exist.
 Compute the discriminant D given by the equation: D  b 2  4  a  c
 If D > 0, print “The equation has two real roots,” and print the two roots.
 If D = 0, print “The equation has one real root,” and print the value of the root.
 If D < 0, print “The equation has no real roots.”
 Use a single if-block consisting of if, elseif, and else.
 Run the script file in the command window three times to obtain solutions to the
following three equations:
o 2  x 2  8  x  3  0. roots = __________________________________

o
15  x 2  10  x  5  0. roots = _________________________________
o
18  x 2  12  x  2  0. roots = _________________________________
Submit your completed script for this exercise to the lab instructor.
3. Write a function (not a script!) to compute worker pay. The function should have two
inputs: hours worked and hourly wage. The function will compute and return the total
pay based upon the following schedule:
a. A worker earns total pay based on hours worked times the hourly wage for the
first 40 hours worked.
b. A worker earns double the hourly wage for hours worked above 40 hours up to
60 hours.
c. A worker earns triple the hourly wage for hours worked above 60 hours.
Use one if-elseif-else block for your function and test it for the following inputs and pay
= $25.25 per hour.
38 hours worked, total pay = _______________________________________
47 hours worked, total pay = _______________________________________
63 hours worked, total pay = _______________________________________
Submit the completed script to the lab instructor.
4. The maximum daily temperature in Fahrenheit for New York City and Anchorage,
Alaska are given for the month of January in order from the 1st to the 31st:
NYC = [ 31, 26, 30, 33, 33, 39, 41, 41, 34, 33, 45, 42, 36, 39, 37, 45, 43, 36, 41,
37, 32, 32, 35, 42, 38, 33, 40, 37, 36, 51, 50];
ANC = [ 37, 24, 28, 25, 21, 28, 46, 37, 36, 20, 24, 31, 34, 40, 43, 36, 34, 41, 42,
35, 38, 36, 35, 33, 42, 42, 37, 26, 20, 25, 31];
Write a script file using Matlab functions and operators to answer the following:
a. Calculate the average (mean) temperature for the month in each city (Matlab has a
function for computing the mean, use help to find it).
b. How many days were below the average temperature in each city?
c. How many days, and which days of the month, was the temperature in Anchorage
higher than the temperature in New York?
d. How many days, and which days of the month, was the temperature in both cities the
same?
e. How many days, and which dates in the month, was the temperature in both cities
above freezing (32F).
The Matlab functions mean, sum and find will be helpful with this assignment
Download