Computer Science 2610
Fall 2004
Solutions to practice questions for quiz 2

This will be a closed book quiz. You may bring one 8.5x11 page of prepared notes, written on both sides.

All topics from the first quiz are also potential topics for this one. The following questions illustrate additional topics that might be included.

  1. If function t is called, what is printed? Be very careful with this question.

    	void r(int x, int y, int& z)
    	{
    	  cout << "r: x = " << x << " y = " << y 
                   << " z = " << z << endl;
    	  x = x + 1;
    	  z = y + x;
    	}
    	 
    	void t()
    	{
    	  int a, b, x;
    	  a = 20;
    	  b = 25;
    	  x = 250;
    	  r(a,b,x);
    	  r(x,a,b);
    	  cout << "t: a = " << a << " b = " << b 
                   << " x = " << x << endl;
         }
    

    Answer:

             r: x = 20 y = 25 z = 250
             r: x = 46 y = 20 z = 25
             t: a = 20 b = 67 x = 46
    

    The key to getting this right is to do the hand simulation carefully. Do it on paper, not in your head. Be sure to remember how to handle call-by-reference and call-by-value. You start running t. Before the first call to r, the picture looks like this, inside function t.

             t
         ----------
           a = 20
           b = 25
           x = 250
    

    Now r(a,b,x) is called. The picture looks like this.

             t                    r
         ----------           ---------
           a = 20              x = 20
           b = 25              y = 25
           x = 250 <-          z .
                     |___________|
    
    Remember that parameter z is call-by-reference, so it refers to the actual parameter, x. When function r runs, it prints line
             r: x = 20 y = 25 z = 250
    
    and then performs the two assignments. The assignment to x only changes r's local variable x, not anything else, since x is a call-by-value parameter. But the assignment to z changes the variable to which z refers. The picture looks like this.
             t                    r
         ----------           ---------
           a = 20              x = 21
           b = 25              y = 25
           x = 46  <-          z .
                     |___________|
    
    Now r returns, and t is about to call r again. The picture is like this.
             t
         ----------
           a = 20
           b = 25
           x = 46
    
    Now t calls r(x,a,b). This time, r's parameter z refers to t's variable b. The picture looks like this just before r runs.
             t                    r
         ----------           ---------
           a = 20              x = 46
           b = 25  <-          y = 20
           x = 46    |         z .
                     |___________|
    
    Now r runs. It prints line
             r: x = 46 y = 20 z = 25
    
    Then r does the two assignments. The picture is like this.
             t                    r
         ----------           ---------
           a = 20              x = 47
           b = 67  <-          y = 20
           x = 46    |         z .
                     |___________|
    
    Now r returns, leaving t ready to print its line. It prints
             t: a = 20 b = 67 x = 46
    

  2. Write a recursive function anySevens(n) that returns 1 if there are any 7's in the decimal representation of n, and 0 otherwise. For example, anySevens(974) = 1, anySevens(7) = 1 and anySevens(32) = 0. Function anySevens should require its parameter n to be nonnegative. For this problem, do not use any form of loop, and do not alter the value of any variable that already has a meaningful value.

    Hint. You can get the rightmost digit of n by computing n % 10. You can get all but the rightmost digit by computing n/10. For example, if n is 1974 then n % 10 is 4 and n/10 is 197.

    Answer:

        bool anySevens(int n)
        {
          if(n == 0) return 0;
          else if(n % 10 == 7) return 1;
          else return anySevens(n/10);
        }
    

    Clearly, 0 has no 7's in it, so the function returns 0 in this case. If the rightmost digit of n (n%10) is 7, then n does have a 7 in it, so the function returns 1. If n is not 0 and the rightmost digit of n is not 7 then n has a 7 just when all but the rightmost digit of n (n/10) has a 7. For example, to test n = 2743, you would notice that the rightmost digit is not 7, so you would give the same answer as you would give on input 274. If you can't stand the idea that a boolean value is really a value, then another way to write this function is as follows.

        bool anySevens(int n)
        {
          if(n == 0) return 0;
          else if(n % 10 == 7) return 1;
          else if(anySevens(n/10) == 1) return 1;
          else return 0;
        }
    

  3. Suppose that array A contains the following.

        i      A[i]
        0       3
        1       7
        2       8
        3       5
    
    Function mystery is shown below. Starting with A holding what is shown above, what is in array A after running mystery(A,4)?
       int mystery(int B[], int n)
       {
         int k;
         for(k = 0; k < n; k++) {
           B[k] = B[k] + k;
         }
       }
    

    Answer:

        i      A[i]
        0       3
        1       8
        2      10
        3       8
    

  4. What is the difference between the logical and physical size of an array?

    Answer: The physical size of the array tells how much space is available to be used. The logical size tells how much is actually in use.

  5. Write a function that computes the sum of all of the numbers in an array of integers. There should be two parameters, the array A and the logical size n of the array. A function heading is provided. Use a loop for this function.

    Answer:

      int sum(int A[], int n)
      {
        int sm = 0;
        int k;
        for(k = 0; k < n; k++) {
          sm = sm + A[k];
        }
        return sm;
      }
    

  6. Write a definition of function sum that has the same meaning as before, but use recursion for this definition instead of a loop.

    Answer:

      int sum(int A[], int n)
      {
        if(n == 0) return 0;
        else return A[n-1] + sum(A, n-1);
      }
    

  7. You would like to set variable s to the sum of all of the integers in array Fish, which has 12 members. What statement would you write to use function sum to do the computation?

    Answer:

        s = sum(Fish,12);
    

  8. Write a function that has a single null-terminated string parameter. It should return 1 if the string contains the letter 'b', and 0 if it has no occurrences of the letter 'b'. Call it anyBs. For example, anyBs("bat") = 1, anyBs("rabbit") = 1, but anyBs("dog") = 0.

    Answer:

      bool anyBs(char s[]) 
      {
        for(int k = 0; s[k] != '\0'; k++) {
          if(s[k] == 'b') return 1;
        }
        return 0;
      }
    

  9. When passing an array as a parameter to a function, you usually also pass the logical size of an array. That is not necessary when the array is a null-terminated string? Why not?

    Answer: You can compute the length of a null-terminated string using strlen. So it is not necessary to be told the length.