CS100 M Fall 2004
Review/Practice Questions for Prelim 1

Question 1
The income tax code in Timbuktu works as follows: the first $30,000 of income is taxed at 25%, the next $30,000 at 30%, and any amount above $60,000 at 40%. Write a program that accepts as input an income amount and computes and prints the correct tax amount to be paid in Timbuktu.

Question 2
Write a program that accepts three numbers as input and prints the maximum of the three numbers. Do not use arrays. The only MATLAB built-in function allowed is input.

Question 3
Write a program to calculate some statistics of a sequence of non-negative numbers entered one by one and terminated by a negative number. Do not use arrays or MATLAB predefined functions (other than input). The statistics to calculate are sum, mean, maximum, and minimum. Store the statistics in variables and print the values to the screen.

Question 4
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 5
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 6
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.

Other review questions from lecture
Complete the "mode question" from the 9/21 lecture. First think about the algorithm and then write the code.