You are on page 1of 2

Vanderbilt University

CS1103 SPRING 2017


HOMEWORK #2

Homework #2 requires you to download the grader program from Blackboard. It is called hw2.p and you can find it
under Assignments and then Homework 2. Create a new blank folder where you intend to work on this homework and
copy the hw2.p file there. Solve each problem in MATLAB. Make sure to name your solutions (that is, your m-files) as
instructed below and that they are located in the same folder as the grader. When you start MATLAB, set your current
folder to this very same folder. Run the grader in MATLAB by typing the command: hw2. The program will display a
menu where you have the option to test individual problems one by one. To get the score for your homework, select to
grade "All Problems." The grader will ask for your vunetid. Makes sure you type it in correctly, all lower case and no
extra spaces. The grader will display your score and an alphanumeric code. This code contains your id and score and this
is what you will submit on Blackboard. IMPORTANT: make sure that you type the code exactly! Or even better,
copy/paste it directly from MATLAB!
Grading: your score will be exactly what the grader prints on your screen. It is the percentage of the number of
problems you solved correctly. For example, if you solved 4 out of 5, your score will be 80. In order to be counted as a
correct solution, your function must pass all the test cases used by the grader.
Submission: Via Blackboard on the same page you found the grader and this document. Submit the alphanumeric code
the grader gave you. Nothing else is needed. The deadline is 10pm Monday, February 20. Deduction rules for late
submissions: 10 percent per each day for the first two days. No credit after 10pm Wednesday, February 22. You can only
use a maximum of two free late days on any one submission.
Honor Code: The usual Honor Code rules apply. By submitting this assignment, you pledge your honor that you have
neither given nor received any unauthorized aid on this assignment.
Problems:

1. Write a function called hw2_problem1 that takes a positive integer called score as its input argument and
returns a letter grade according to the following scale: A: 91 and above; B: 81-90; C: 71-80; D: 61-70; F: below 61.
You do not need to check the input. Remember that to assign a letter to a variable, you need to put it in single
quotes, as in: grade = 'A'.

2. Write a function called hw2_problem2 that takes as its input arguments six positive scalar integers: y1, m1, d1,
y2, m2, d2, in that order, representing the birthdates of two persons. The variables that start with y stand for the
year, m for the month and d for the day. The variables that end in 1 correspond to the first person, while those that
end in 2 correspond to the second person. The function returns 1 if the first person is older, 0 if they have the same
age, and -1 if the first person is younger. You do not need to check whether the inputs have appropriate values. For
example, you may assume that both m1 and m2 are positive integers that are less than 13 and that the day numbers
fit with their months.

3. Write a function called hw2_problem3 that takes the starting times of two movies, hr1, hr2, min1, min2, and
their durations, durmin1, durmin2, as its input arguments and decides whether we can binge and watch both.
The criteria are that they must not overlap and that we are not going to wait more than 30 minutes between the
end of one and the beginning of the next. It returns true if the criteria are both met and returns false otherwise. You
may assume that movie start times are always after 1 pm and before midnight. You may also assume that the first
one starts earlier. The order of the input arguments is: hr1, min1, durmin1, hr2, min2, durmin2.
4. Write a function called hw2_problem4 that is called like this p = hw2_problem4(c0,c,x), where c0 and x
are scalars, c is a vector, and p is a scalar. If c is an empty matrix, then p = c0. If c is a scalar, then p = c0 + c*x.
Otherwise, p equals the polynomial,
0 + (1) 1 + (2) 2 + + () ,

where N is the length of the vector c. Here are a few sample runs:

>> format long


>> p = hw2_problem4(-17,[],5000)
p =
-17
>> p = hw2_problem4(3.2,[3,-4,10],2.2)
p =
96.920000000000030
>> p = hw2_problem4(1,[1,1,1,1],10)
p =
11111
5. Write a function called hw2_problem5 that computes the bus fare one must pay in a given city based on the
distance travelled. Here is how the fare is calculated: the first mile is $2. Each additional mile up to a total trip
distance of 10 miles is 25 cents. Each additional mile over 10 miles is 10 cents. Miles are rounded to the nearest
integer other than the first mile which must be paid in full once a journey begins. Children 18 or younger and seniors
60 or older get a 20% discount. The inputs to the function are the distance of the journey and the age of the
passenger in this order. Return the fare in dollars, e.g., 2.75 would be the result returned for a 4-mile trip with no
discount.

You might also like