CS 1713 Section 2 Exam 3

Write all of your answers in the space provided. Write your name on top of this test. You may not use the computer or refer to any other materials while you are taking this test. Read each question carefully. Some ask you to write methods while other only require code segments.
  1. Write a method called contains that accepts as a parameter an array of integers array and a value key. The method should return true if array contains key, i.e., if at least one element of array is equal to key, false otherwise.
    solution:
    
    boolean contains (int array[], int key) {
    	for (int i=0; i<array.length; i++)
    		if (array[i] == key) return true;
    	return false;
    }
    
  2. Rewrite the method contains described in Problem 1, but assume that array is sorted in ascending order. Use the binary search technique to determine whether key is contained in array.
    solution:
    
    boolean contains (int array[], int key) {
    	int low = 0;
    	int high = list.length - 1;
    	while (low <= high) {
    		int mid = (low + high)/2;
    		if (list[mid] == value)
    			return true;
    		else if (list[mid] < value)
    			low = mid + 1;
    		else
    			high = mid - 1;
    	}
    	return false;
    }
    
  3. Rewrite the method contains described in Problem 1, but now array is a rectangular two-dimensional array. That is, contains should return true if and only if some element of the two-dimensional array array is equal to the value key. (Hint: don't think about using binary search for this, but do think about using nested loops.)
    solution:
    
    boolean contains (int array[][], int key) {
    	for (int i=0; i<array.length; i++)
    		for (int j=0; j<array[i].length; j++)
    			if (array[i][j] == key) return true;
    	return false;
    }
    
  4. Write a code segment that does the following:
    1. Allocate a two-dimensional array of integers with 100 columns and 50 rows.
    2. Set each element in the array to a random number between 1 and 100, inclusive.
    3. Print the average of all the elements in the array.
    solution:
    
    	int v[][] = new int[50][100];
    	Random rand = new Random ();
    	int sum = 0;
    	for (int i=0; i<50; i++) {
    		for (int j=0; j<100; j++) {
    			v[i][j] = rand.nextInt(100) + 1;
    			sum += v[i][j];
    		}
    	}
    	System.out.println (sum / 5000.0);
    
  5. Consider an array of size n that is sorted in ascending order, and a value k that is not in the array. Answer the following questions. You may express your answers in big-O notation if you wish. Note that you must express your answers in terms of n.
    1. Suppose we do a linear search for the value k in the array. How many comparisons must the linear search make?

      solution

      O(n)

    2. Suppose we do a binary search for the value k in the array. How many comparisons must the binary search make?

      solution

      O(log n)

    3. Suppose we perform bubble sort on the array. How many comparisons must the bubble sort make?

      solution

      O(n)

    4. Suppose we perform selection sort on the array. How many comparisons must the selection sort make?

      solution

      O(n2)

  6. Write a method that accepts an array of doubles and returns the minimum value in the array.
    solution:
    
    double findMin (double v[]) {
    	double min = v[0];
    
    	for (int i=1; i<v.length; i++) {
    		if (v[i] < min) min = v[i];
    	}
    	return min;
    }
    
  7. Consider the following code:
    class foo {
    
            static void m1 (String v[], int i, String s) {
                    v[i] = s;
            }
    
            static void m2 (String v[], int i, String s) {
                    String w[] = new String[v.length];
                    for (int j=0; j<w.length; j++) w[j] = v[j];
    		v = w;
                    v[i] = s;
            }
    
            static void m3 (String s, String t) {
                    s = t;
            }
    
            public static void main (String args[]) {
                    String x[] = new String[3]; 
                    x[0] = "a";
                    x[1] = "b";
                    x[2] = "c";
                    m1 (x, 2, "d");
                    System.out.println (x[2]);
                    m2 (x, 2, "e");
                    System.out.println (x[2]);
                    m3 (x[2],  "f");
                    System.out.println (x[2]);
            }
    };
    
    Answer the following questions:
    1. Does the call to m1 change x[2]? Why or why not?
      solution:
      
      Yes.  The parameter v in m1 is a reference to the array x, so the #2
      element of x is change from "c" to "d".
      
    2. Does the call to m2 change x[2]? Why or why not?
      solution:
      
      No.  v is a reference to x.  m2 makes a copy of the array x, assigns a
      reference to the copy to v, and then changes the #i element of v.  However,
      x remains unchanged because v is only the value of the reference to x;
      changing v does not change x.
      
      
    3. Does the call to m3 change x[2]? Why or why not?
      solution:
      
      No.  The reference s is change to the value of the reference t, but the
      original reference x[2] remains unchanged.