Computer Science 2610
Fall 2004
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;
         }
    

  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.

  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;
         }
       }
    

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

  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.

      int sum(int A[], int n)
    

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

  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?

  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.

  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?