CS 302 Quiz 4

CS 302 - Algebraic Language Programming Name: ____________________________________
Section 17 CS Login: ________________________@cs.wisc.edu
Instructor: Colby O'Donnell Score: __________________________ / 18 Points
Date: Tuesday, February 23, 1998 Percentage of Class Grade: 2%

  1. (____ / 3 points) Short Answer

    a) True / False - A Function with reference parameters is considered a true black box.

    b) What kind of function returns no value? void

    c) Circle the things that are part of a function signature:

    (return type), (function name), (formal parameters)
  2. (____ / 3 points) Below is a definition of the pow function, which I have written many times in class. What are the two Preconditions of the input parameters, and what is the Postcondition of the funtion?

    int pow (int b, int e) {
      int p = 1;
      while (e > 0) {
        p *= b;
        e--;
      }
      return p;
    }
    
    PRE:
    • b is any integer
    • e is a non-negative integer
    POST:
    • The function return an integer equal to be.

  3. (____ / 2 points) What is the major problem with this C++ code?

    #include <iostream.h>
    
    int main () {
      double area (double radius) {
        return radius * radius * 3.14159;
      }
    
      double r;
      cout << "Input the radius: ";
      cin >> r;
    
      cout << "The radius is ";
      cout << area(radius) << endl;
    }
    
    This problem was thrown out, due to all my mistakes. However, I was looking for an answer like: A function is defined within another function.

    
    
    
  4. Call by Reference

    a) (____ / 4 points) What are the two primary motivations for using Call By Reference that we discussed in class?

    1. Ability for a function to modify actual parameters
    2. Ability for a function to return more than one value

    b) (____ / 2 points) What were two major examples of functions discussed in class dealing with Call By Reference parameters?

    1. swap function. void swap (int & a, int & b);
    2. input function. void get_vals (int & x, int & y, int & z);

    c) (____ / 6 points) What is the output of the following C++ program?

    #include <iostream.h>
    
    void funfun (int & a, int b, int & c) {
      int temp = b + c;
      b = 1 - a;
      c = a * b;
      a = temp;
    }
    
    int main () {
      int b = 1, c = 2, d = 3;
      int i = 1;
    
      while (i < 4) {
        funfun (b,c,d);
        cout << b << ',' << c << ',' << d << endl;
        i++;
      }
    }
    
    Output:

    5,2,0
    2,2,-20
    -18,2,-2