GRADE (TA use only) __________________

(1 point) USER NAME ___________________________

CS 102 Exam III, December 6, 2007

  1. (4 points) Examine the following declarations.
            int x, y;
            double gpa;
            char c;

    Rewrite the cin statements using scanf() statements.
    Rewrite the cout statements using printf() statements.
    Example:   cin >> gpa >> x;   would be   scanf("%lf%d", &gpa, &x);

        a.   cin >> x >> y;


        b.   cin >> gpa >> x;


        c.   cout << "OK   " << c << "  " << x << endl;


        d.   cout << gpa << "   is good.\n";


  2. (6 points) How many times does each loop execute?

        a.   for (i = 5; i < 19; i += 3)
                 cout << i << endl;

        b.   for (i = 5; i < 20; i += 3)
                 cout << i << endl;

        c.   for (i = 5; i < 21; i += 3)
                 cout << i << endl;

  3. (3 points) What is displayed by the code segment below if the input is 1234?   Be careful!

         int i, power, n, x;

         i = x = 0;
         power = 1;
         cin >> n;     // assume a non-negative value for n

         while (n > 0)   {
             x += n % 10 * power;
             power *= 10;
             n /= 10;
         }
         cout << x << endl;


  4. (3 points) In terms of a and b, what (not how) does this code do? Your answer should not mention x.

          x = a - b;
          a = a - x;
          b = a + x;

  5. (5 points) Complete function alike() that returns 1 if two one-dimensional arrays are alike, and 0 otherwise. The arrays hold n elements. Declare any variables that you need.
      int alike(int A[], int B[], int n)
      {
      
      
      
      
      
      
      
      
      
      
      
      
      }
        
  6. (4 points) Fill in the new array values after the code segment is executed. Leave no blank array elements.

      const int N = 8; int i, A[N] = {10, 11, 12, 13, 14, 15, 16, 17}; for (i = 1; i < N; i++) // notice that i starts at one (A[i-1] = A[i]; _________________________________________ | | | | | | | | | original A: | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | |---------------------------------------| 0 1 2 3 4 5 6 7 _________________________________________ | | | | | | | | | new A: | | | | | | | | | |---------------------------------------| 0 1 2 3 4 5 6 7
  7. (4 points) You have the following code fragment. What is printed with the indicated input values?
      cin >> x; switch(x) { case 20: x *= 4; break; case 40: x--; x--; break; case 60: x = -5; case 80: if(x) x = 100; break; default: x++; } cout << x << endl; a) input: 20 _________ b) input: 60 _________



  8. (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 = 88, y = 44, *p = &x, *q = &y; parm(x, q); cout << "In main: " << x << " " << y << " " << *p << " " << *q << endl; } void parm(int z, int *ptr) { int x, y; x = 11; y = 55; z = *ptr + *ptr; *ptr = *ptr - x; cout << "In parm: " << x << " " << y << " " << *ptr << " " << z << endl; }



  9. (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

        a. How many probes will it take to find   12?


        b. What is the main disadvantage of the array-based binary search?




  10. (4 points) Examine the function below. What (not how) does mystery() do?
      void mystery(char *one, char *two) // two c-style strings { char *p, *q; for (p = one, q = two; *q != '\0'; q++, p++) *p = *q; *p = '\0'; }


  11. (4 points) What is displayed by the code segments below?
      a. int a = 20, b = 12, c = 28; if(a <= b || a > c) { if(a != b) a++; else if(a < c) b++; } else c++; cout << a << " " << b << " " << c << endl; _____________________________ b. x = 30; y = 40; if(x != 33 || y == 33) { if(x < 33 && y > 33) y = x + 10; } else { x = y + 10; y++; } cout << x << " " << y << endl; _____________________________
  12. (2 points) Declare a two dimensional array A that can hold 40 integers and has four columns.


  13. (4 points)
    a. What are the two things that malloc() does?

        1.

        2.

    b. What are the two things that free() does?

        1.

        2.


  14. (6 points) Rewrite the following as a functionally equivalent while loop. You don't need the comments.
      for (count = sum = 0; // expression 1 scanf("%d", &x) != EOF && count < N; // expression 2 count++, sum += x); // expression 3



  15. (2 points) Command line arguments are used to pass information to a program when you run it.

        1. What value does the command line argument argc hold?




        2. The command line argument argv is an array of character pointers. What does argv[0] point to?



  16. (2 points) You have vectors v_one and v_two declared as
      vector <int> v_one (10); // create vector with 10 elements, all 0 vector <int> v_two (10, -1); // create vector with 10 elements, all -1
    Write code to copy v_two to v_one.   Hint: One line of code is all that is needed.






  17. (4 points) Examine the declarations and answer the questions. Assume initialization of all variables.
      typedef char STRING[31]; typedef struct { STRING name, major; char address[31]; int age, tickets, hours; char gender; double gpa, tuition; } STUDENT_TYPE; STUDENT_TYPE undergrad, grad;


        a. Write one statement to copy structure grad into structure undergrad.





        b. Write one executable statement to copy "CS" into the major field of undergrad.














  18. (5 points) Examine the arrays below that were filled using an array-based insertion sort without deletes as shown in class.

      data link first: 1 ----------|------- 0 | Tom | -1 | next: 4 ----------|------- 1 | Ann | 3 | ----------|------- 2 | Sue | 0 | ----------|------- 3 | Bob | 2 | ----------|------- 4 | | | ----------|------- a. In what order were the names entered into data? b. What constitutes an empty list? c. What constitutes a full list? d. Fill in the arrays below to show the data and link arrays if we add Nan. e. Fill in the values for first and next. data link first: ________ ----------|------- 0 | | | next: ________ ----------|------- 1 | | | ----------|------- 2 | | | ----------|------- 3 | | | ----------|------- 4 | | | ----------|-------

Key for CS 102 Exam III, December 6, 2007

  1. a. scanf ("%d%d", &x, &y); b. scanf ("%lf%d", &gpa, &x); c. printf("OK %c %d\n", c, x); d. printf("%lf is good\n", gpa);
  2. a. 5 b. 5 c. 6
  3. 1234
  4. It swaps the values of a and b.
  5. int alike(int A[], int B[], int n) { int i, same; same = 1; // assume arrays are alike for(i = 0; i < n && same; i++) if(A[i] != B[i]) same = 0; return same; }
  6. _________________________________________ | | | | | | | | | | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 17 | |---------------------------------------| 0 1 2 3 4 5 6 7
  7. a. 80 b. 100
  8. In parm: 11 55 33 88 In main: 88 33 88 33
  9. a. 2 b. The array must be ordered.
  10. This question was the bonus on Exam II. The function copies the second string into the first one. That is, it is functionally equivalent to strcpy().
  11. a. 20 12 29 b. 30 40
  12. int A[10][4];
  13. a. 1. malloc() allocates the requested amount of space from the heap.     2. malloc() returns the address of the allocated space. b. 1. free() returns to the heap the space pointed to by its argument.     2. The pointer is then undefined.
  14. count = sum = 0; while (scanf("%d", &x) != EOF && count < N) { count++; sum += x; }
  15. 1. argc holds the number of arguments on the command line. 2. argv[0] points to the name of the program.
  16. v_one = v_two;
  17. a. undergrad = grad; b. strcpy (undergrad.major, "CS");
  18. a. Tom Ann Sue Bob b. first is -1 c. next is -1 d, e. data link first: 1 ----------|------- 0 | Tom | -1 | next: -1 ----------|------- 1 | Ann | 3 | ----------|------- 2 | Sue | 0 | ----------|------- 3 | Bob | 4 | ----------|------- 4 | Nan | 2 | ----------|-------