FULL NAME: ____________________________________   102 or 206 (circle one)

CS 102/ECE 206 Test, April 27, 2010

Choose either 1a or 1b, not both.

1a. (10 points) You have a two-dimensional array of characters that has NROWS (number of rows) and NCOLS (number of columns). NROWS and NCOLS are odd global constants.

1b. (10 points) You have an integer array of size N where N is a global constant. Write function max_min() that finds (and returns through the parameter list) 1) the largest value in the array, 2) the smallest value in the array, 3) the number of times the smallest appears, and 4) the number of times the largest appears. Use a single loop.


2. (8 points) You have the following class definition.

    class Test2  {

      public:
        Test2();                    // default constructor
        Test2(int, int, char*);     // parametrized constructor

      private:
        int x,         // range 0 through 99
            y;         // range 0 through 10
        char a[21];    // C-style string, requires srtcpy() to copy
    } 


3. (6 points) a. Study the code below and indicate the output.

    #include<iostream> #include<vector> using namespace std; void func(int a[], vector<int> v) { int i; v[2] = a[0] + v[1]; a[2] = v[2] - a[1]; v[0] = v[2] + a[1]; cout << "In func(): \n"; for(i = 0; i < 3; i++) cout << a[i] << " "; cout << endl; for(i = 0; i < 3; i++) cout << v[i] << " "; cout << endl; } int main(void) { int a[3] = {10, 11, 12}; vector<int> v(3); int i; cout << "Size of vector: " << v.size() << endl; v[0] = 20; v[1] = 21; v[2] = 22; func(a, v); cout << "In main(): \n"; for(i = 0; i < 3; i++) cout << a[i] << " "; cout << endl; for(i = 0; i < 3; i++) cout << v[i] << " "; cout << endl; }


4. (4 points) a. With vectors, precisely what does member size() do? Be complete but brief.


    b. With vectors, precisely what does member at() do? Be complete but brief.


5. (7 points) Mark each item as valid or invalid. If valid, give the value of the left side operand. If invalid, explain why. Assume that the initializations below are reset for each statement. int a[6] = {0, 1, 2, 3, 4, 5}, *p = &a[0]; (a) p = a; (b) a = p; (c) *p = *(a + 4); (d) a[3] = *p; (e) *p = a[2]; (f) p = (a + 4); (g) p = &a + 4;


6. (6 points) You have the following definitions and declarations:

        struct course  {
          char title[21];
          char day[3], grade;
          int hours;
        };

        struct course schedule[10], extra;     // assume initialization