CS100 M Fall 2003
Review/Practice Questions for Prelim 2

Question 1
Part A: Write a function hms2s that converts a time expressed in hours, minutes, seconds to a time expressed in seconds. Function hms2s returns one variable seconds and takes three input arguments: hours (h), minutes (m), and seconds (s).
Part B: Write a function s2hms that performs the opposite operation. Function s2hms takes one input argument seconds and returns three variables: h, m, and s.
Part C: Write two function calls to the above functions. Use input values of your choice.

Question 2
Part A: Write a function average that calculates the mean of numbers contained in a matrix. Function average takes as input argument a matrix data and returns one variable ave that stores the mean. Do not use vectorized code or the MATLAB predefined functions sum and mean.
Part B: Write a function stdDev that calculates the standard deviation of numbers contained in a matrix. Function stdDev takes as input argument a matrix data and returns one variable sd that stores the value of the standard deviation. You must use your function average. You may use vectorized code and MATLAB predefined functions except mean and std. The standard deviation is calculated as follows:
  1. Determine the mean of the data set.
  2. Subtract the mean from each data value. This results in a new set of numbers, each of which is called the deviation.
  3. Square each deviation.
  4. Add up the squared deviations and divide the sum by the number of deviations.

Question 3
Write a function myCylinder that calculates the volume and surface area of a "randomly generated" cylinder. Function myCylinder takes as input argument a positive number ratio and returns two variables: volume and surfactArea. Function myCylinder randomly generates a real number in the range of (0,1) as the diameter d of the circular end of the cylinder. The height of the cylinder is d*ratio. Write a function call to myCylinder and save the results in variables vol and surfArea.

Question 4
Given a positive integer num, write a program segment that randomly generates num upper case letters from the alphabet set. The program displays these generated letters as a string and determines the relative frequency of each letter in the alphabet set. The relative frequency of a letter is the number of times that letter appears divided by the total number of letters generated. The relative frequencies should be stored in a vector relFreq.