Bookwork 4.2 #1-9 Rewritten and Updated.

  1. Indicate which of the following are valid function declarations. Explain what is wrong with those that are invalid.
    1. round_tenth (double x);
    2. double make_changes (X, Y);
    3. int max (int x, int y, int z);
    4. char sign (double x);
    5. void output_string(apstring s);
  2. Find all the error in each of the following functions:
    1. Code Snippet:
      int average (int n1, int n2);
      return N1 + N2 / 2;
    2. Code Snippet:
      int total (int n1, int n2);
      int sum;
      return 0;
      sum = n1+n2;
  3. Write a function for each of the following:
    1. Round a real number to the nearest tenth.
    2. Round a real number to the nearest hundredth.
    3. Convert degrees Fahrenheit to degrees Celsius.
    4. Compute the charge for cars at a parking lot; the rate is 75 cents per hour or any fraction thereof.
  4. Write a program that uses the function you wrote for calculating parking lot charges to print a ticket for a customer who parks in the parking lot. Assume the input is in minutes.
  5. Write two functions (square and cube) to write a program which prints the square and cube of any user given integer.
  6. Write a program that contains a function that allows the user to enter a base (a) and an exponent (x) and then have the program print the value of a to the x power.
  7. What role do parameters play in a function?
  8. Explain the difference between formal parameters and actual parameters.
  9. Why is it a good idea to write functions that are as general as possible?


Leave a Reply