1. Concepts: I/O, conditionals, mod Read in the year and print whether it is a leap year using (something equivalent to) the rules below: Rule 1. A non-multiple of 4 is not a leap year. Rule 2. A multiple of 4 is a leap year. Rule 3. Rule 2 does not apply to multiples of 100. Rule 4. Rule 3 does not apply to multiples of 400. Hint: REM and MOD are MATLAB functions for finding the remainder. Use HELP to check them out. E.g., rem(x,y) returns the remainder when x is divided by y. ------------------------------------------------------------- % Get input from user year = input('Enter a year: '); % Evaluate and give output if (rem(year,4)==0 & (rem(year,100)~=0 | rem(year,400)==0)) disp('A leap year!'); else disp('Not a leap year!'); end ===================================================================== 2. Concepts: I/O, simple matrix manipulation, array arithmetic, MATLAB functions Annual US fatality rates (per 100 million vehicle miles) on all roads are stored in a file called $rates.dat$ as an n-by-3 matrix: - column 1 contains the years - column 2 contains the actual rates - column 3 contains the model predicted rates For example, a row in the file $rates.dat$ looks like this: 1978 3.39 3.31 Write a program that performs the following tasks: - read the data in the file $rates.dat$ - store the actual fatality rates as the vector variable $actual$ - store the model predicted rates as the vector variable $model$ - for each year, calculate the percent error of the modeled rate. Percent error is (|actual-model|/actual)*100 Store the percent errors in a vector variable $errors$ - Calculate the average percent error (for all years) Potentially useful MATLAB functions: INPUT, LOAD, SIZE, ABS, SUM, MEAN ------------------------------------------------------------- % Load data file and calculate error in fatality rates % Load data and set variables load rates.dat actual=rates(:,2); model=rates(:,3); % Calculate errors errors = abs(actual-model)./actual*100; ave_error = mean(errors); ===================================================================== 3 and 4: Check your answers using MATLAB ===================================================================== % 5. Conditionals, nested conditions % Write a program that asks the user for a name with 3 letters. Your % program should store the name in a variable called $name$. The % program should output whether the name that the user typed in is: % in upper case (e.g. TOM), or % in lower case (e.g. tom), or % in title case (e.g. Tom), or % in messy case, which is non of the above (e.g. ToM, TOm, tOm, ...). % Some help on how to deal with strings: % Imagine you have a string assigned to a variable, e.g. % var='Jen' % There is a Matlab function called $double$ that converts a string % into an array of numbers where each number represents the position % of the according letter in the table of ASCII values. Here is a small % portion of the table of ASCII values: % % letter position letter position % A 65 a 97 % B 66 b 98 % C 67 c 99 % D 68 d 100 % E 69 e 101 % F 70 f 102 % ... ... % ... ... % U 85 u % V 86 v 118 % W 87 w 119 % X 88 x 120 % Y 89 y 121 % Z 90 z 122 % If you typed name=double('Jen') in the Command Window then an array % containing 3 numbers: 74 101 110 would be stored in w. % For simplicity, you can assume that any letter that is not uppercase % (position 65 through 90) is lower case. name=input('User, please enter a name with 3 letters: ','s') name=double(name); % Evaluate all cases given 1st character is in upper case if name(1)>=65 & name(1)<=90 if name(2)>=65 & name(2)<=90 if name(3)>=65 & name(2)<=90 disp('Entered name was in upper case!') elseif name(3)>=97 & name(3)<=122 disp('Entered name was in messy case!') end elseif name(2)>=97 & name(2)<=122 if name(3)>=65 & name(2)<=90 disp('Entered name was in messy case!') elseif name(3)>=97 & name(3)<=122 disp('Entered name was in title case!') end end % Evaluate all cases given 1st character is in lower case elseif name(1)>=97 & name(2)<=122 if name(2)>=65 & name(2)<=90 disp('Entered name was in messy case!') elseif name(2)>=97 & name(2)<=122 if name(3)>=65 & name(3)<=90 disp('Entered name was in messy case!') elseif name(3)>=97 & name(3)<=122 disp('Entered name was in lower case!') end end end