CS100 M Fall 2004
Review/Practice Questions for Prelim 2
- Question 1
- 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:
- Determine the mean of the data set.
- Subtract the mean from each data value. This results in a new set of numbers, each of which is called the deviation.
- Square each deviation.
- Add up the squared deviations and divide the sum by the number of deviations.
- Question 2
- 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.