CS100 M Spring 2005
Review/Practice Questions for Prelim 2

Question 1
Given integers nBig and nSm where nBig > nSm, write a program segment that lists the integers in decreasing order and annotates the integers that are prime numbers. Assume nBig,nSm > 1. Example output for nBig=6 and nSm=3 is
  6
  5 is prime
  4
  3 is prime

Question 2
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 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
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.

Other review questions
Be sure to review the examples from lecture, the exercises in section (and lab), and the projects.