GRADE (TA use only) __________________

(1 point) USER NAME: ___________________________

CS 102 Exam II, November 8, 2007

  1. (6 points)} Fill in the code described in the two *** comments.
      // file Circle.h const double PI = 3.14159; class Circle { public: double circumference(); double area( ); // prototypes for constructor and destructor Circle(double r); ~Circle(); private: double radius; // radius of circle in feet }; #include <iostream> #include "Circle.h" using namespace std; int main() { Circle o_shape(7.25); // print area and circumference cout << "Area: " << o_shape.area() << endl; cout << "Circumference: " << o_shape.circumference() << endl; } // *** Implement Constructor for Circle; look at prototype. // *** Implement Destructor for Circle; look at prototype. // Destructor should print "It's gone!" . double Circle::area() { return PI * radius * radius; } double Circle::circumference() { return PI * 2 * radius; }





  2. (4 points) Indicate what gets printed if the input is:       25   -3   cs102   44   UT   87
  3. (4 points) Examine this code from the web notes and answer the questions. Watch lines.

    1. What is the purpose of the statement cin >> dummy; in the code above?




    2. What is printed if the input is:           25   -3   cs102   44   UT   87







  4. (3 points) Why do we use   #ifndef...#endif  ("preprocessor wrapper") in header files?




  5. (6 points) In the space provided below, indicate what gets printed from the following program?
        #include <iostream> using namespace std; void one(int &x, int &y) { int z; z = x; x = y; y = z; } void two(int *x, int *y) { int z; z = *x; *x = *y; *y = z; } int main() { int a, b; int &c = a, &d = b; // reference variables a = 22; b = 88; one(c, d); cout << "After one() " << a << " " << b << endl; a = 22; b = 88; two(&a, &b); cout << "After two() " << c << " " << d << endl; }




  6. (6 points) For the following questions, tell 1) what the function does with the argument, and 2) how the original argument is affected by the action of the function.

    1. When an argument is passed by value to a function, what happens?




    2. When a pointer is passed to a function, what happens?




    3. When a reference variable is passed to a function, what happens?






  7. (4 points) Recall this question from Exam I. Write a complete C++ function maximum( ) that returns the maximum of three integers. Your code must have only one exit point to receive full credit. Don't forget the case where two or more integers are the same.




















  8. (4 points) Examine the code below and show what is printed.
      #include <iostream> using namespace std; int main() { double array[7] = {24, 83, 15, 78, 32, 91, 40}; double *p; for(p = array + 2; p < array + 7; p += 2) cout << *p << " "; cout << endl; }





  9. (4 points) With the given input, what is printed from the code segment below?
      int x, y; cin >> x >> y; cout << (x > 25 || y < 25 ? "Yes\n" : "No!\n"); Input: 10 20 Output: Input: 25 25 Output:




  10. (8 points) Trace the code segment below and fill in the ASCII diagram with the indicated characters.
           char two_d [5][7];
           int row, col;
    
           for(row = 0; row < 5; row++)  
             for(col = 0; col < 7; col++)  
               if(col % 2)
                 two_d[row][col] = 'X';
               else
                 two_d[row][col] = 'O';
    
             
    
                  0   1   2   3   4   5   6
                -----------------------------
             0  |   |   |   |   |   |   |   |
                -----------------------------
             1  |   |   |   |   |   |   |   |
                -----------------------------
             2  |   |   |   |   |   |   |   |
                -----------------------------
             3  |   |   |   |   |   |   |   |
                -----------------------------
             4  |   |   |   |   |   |   |   |
                ----------------------------- 


    .
  11. (8 points) In the space provided below, indicate what gets printed from the following program?
      #include <iostream> using namespace std; void parm (int*, int); int main() { int x = 43, y = 57, *p = &x, *q = &y; parm(p, y); cout << "In main: " << x << " " << y << " " << *p << " " << *q << endl; } void parm(int *ptr, int num) { int x, y; x = 33; y = 77; num = *ptr; *ptr = *ptr + x; cout << "In parm: " << x << " " << y << " " << *ptr << " " << num << endl; }







  12. (4 points) Examine the code below and show the output if   set_team()   returns TN. Watch linefeeds.
      #include <iostream> using namespace std; enum SEC_TYPE {KY, TN, FL, GA, SC, AK, AL}; // define a new global type int main() { SEC_TYPE team; // declare variable of type SEC_TYPE team = set_team( ); // assume a function to set the value of team cout << "The winner is "; switch(team) { case TN: cout << "Tennessee "; break; case FL: cout << "Florida "; break; case GA: cout << "Georgia "; break; case SC: cout << "South Carolina "; break; case AK: cout << "Arkansas "; break; case AL: cout << "Alabama "; break; }; cout << "in position " << team << ".\n"; }


  13. (4 points) You have    int a[7] = {44, 55, 77, 11, 66, 22, 33};
    Show the array elements after the first and second passes of selection sort as shown in class.
      ------------------------------------------- pass 1: | | | | | | | | | | | | | | | | ------------------------------------------- 0 1 2 3 4 5 6 ------------------------------------------- pass 2: | | | | | | | | | | | | | | | | ------------------------------------------- 0 1 2 3 4 5 6


  14. (4 points) This question is about the binary search as shown in class.
      ------------------------------------------------------------------ array: | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | ------------------------------------------------------------------ 0 1 2 3 4 5 6 7 8 9 10 11 12

    How many probes will it take to find   19?

    How many probes will it take to find   11?





  15. (10 points) Answer either part A or part B, not both.

    A. Write a function initialize_vector() to read values into vector vec.

    B. The string function strcmp() is used to compare two strings. Write a complete function my_compare() that is almost functionally equivalent to strcmp(). Your function should return -1, 0, or 1. You may not use any string library functions.
































    *** Bonus (2 points, all or none, no partial credit) Examine the function below. What (NOT HOW) does mystery() do?

      void mystery(char *one, char *two) { char *p, *q; for(p = one, q = two; *q != '\0'; q++, p++) *p = *q; *p = '\0'; }

    Key for CS 102 Exam II, November 8, 2007

    1. Circle::Circle(double r) { radius = r; } Circle::~Circle() { cout << "It's gone!\n"; }
    2. number: 25 number: -3
    3. 1. To read past an input value that cannot be converted to an integer. 2. Number: 25 Number: -3 Number: 44 Number: 87
    4. To prevent multiple definition errors.
    5. After one() 88 22 After two() 88 22
    6. 1. 1) A copy of the argument is made; 2) any action by the function is performed on the copy, not the original argument. 2. 1) A copy of the pointer (an address) is passed; 2) the value pointed to by the original argument can be modified. 3. 1) An alias is created for the variable; 2) any action by the function is performed on the original argument.
    7. int maximum(int x, int y, int z) { int max; // ultimately, holds max value max = x; // assume the largest is x if(y > max) max = y; if(z > max) max = z; return max; }
    8. 15 32 40
    9. Yes No!
    10.       0   1   2   3   4   5   6
          -----------------------------
       0  | O | X | O | X | O | X | O |
          -----------------------------
       1  | O | X | O | X | O | X | O |
          -----------------------------
       2  | O | X | O | X | O | X | O |
          -----------------------------
       3  | O | X | O | X | O | X | O |
          -----------------------------
       4  | O | X | O | X | O | X | O |
          ----------------------------- 
    11. In parm: 33 77 76 43 In main: 76 57 76 57
    12. The winner is Tennessee in position 1.
    13. ------------------------------------------- pass 1: | 11 | 55 | 77 | 44 | 66 | 22 | 33 | ------------------------------------------- 0 1 2 3 4 5 6 ------------------------------------------- pass 2: | 11 | 22 | 77 | 44 | 66 | 55 | 33 | ------------------------------------------- 0 1 2 3 4 5 6
    14. 2 4
    15. A. vector <int> initial_vector(vector <int> vec) { int i; for(i = 0; i < vec.size(); i++) cin >> vec[i]; return vec; } B. int compare(char *a, char *b) // or int compare(char a[], char b[]) { int i; /* Loop until two characters differ or until end-of-string */ for(i = 0; a[i] == b[i] && (a[i] !='\0' && b[i] != '\0'); i++); /* Last chars examined are different or the same, including '\0' */ if(a[i] < b[i]) return -1; /* string a < string b */ else if(a[i] == b[i]) return 0; /* string a = string b */ else if(a[i] > b[i]) return 1; /* string a > string b */ } *** Bonus
        It copies the second string into the first one. That is, it is functionally equivalent to strcpy().