CSE131 Module 7: Solutions to Practice Problems

Be sure to ask if you are surprised by any of these answers.

  1. Write a method that takes as its parameter an array of integers and returns the sum of the values in the array.
    int sum(int[] a) {
       int total = 0;
       for (int i : a)
          total += i;
       return total;
    }
    
  2. Write a method that takes as its parameter an array of integers and returns the minimum of the values in the array.
    int min(int[] a) {
       int least = a[0];
       for (int i : a)
          if (i < least)
             least = i;
       return least;
    }
    
  3. Write a method that takes as its parameters two arrays of integers and returns the dot product of the two (i.e., the sum of the products of the corresponding elements).
    int dotProduct(int[] a, int[] b) {
       int result = 0;
       for (int i=0; i < a.length; i++)
          result += a[i]*b[i];
       return result;
    }
    
  4. Write a method that takes as its parameter an array a of integers and returns a new array of the same length, where each entry is the corresponding value in a multiplied by its index in the array.
    int[] multiplyPosition(int[] a) {
       int[] result = new int[a.length];
       for (int i=0; i < a.length; i++)
          result[i] = a[i]*i;
       return result;
    }
    
  5. Write a method that takes as its parameters two arrays of integers and returns a new array where the value at each index is the sum of the corresponding two elements of the given arrays at the same index. Assume the arrays are of equal length.
    int[] sumEqualLengthArrays(int[] a, int[] b) {
       int[] result = new int[a.length];
       for (int i = 0; i < a.length; i++)
          result[i] = a[i]+b[i];
       return result;
    }
    
  6. Write a method that takes as its parameters two arrays of integers and returns a new array where the value at each index is the sum of the corresponding two elements of the given arrays at the same index. Do not assume the arrays are of equal length. Pretend that the shorter array is padded with zeros at the end. (For example, if the arrays are of length 3 and 5, your result should be as if the shorter array had zeros in the "missing" slots.)
    int[] sumArrays(int[] a, int[] b) {
       int[] result = new int[Math.max(a.length, b.length)];
       for (int i = 0; i < result.length; i++) {
          if (i < a.length)
             result[i] += a[i];
          if (i < b.length)
             result[i] += b[i];
       }
       return result;
    }
    
  7. Write a method that takes as its parameter an array a of integers and modifies the given array so that it contains a running sum of its original values. For example, if the array originally had the values [3 2 4 7], after running your method that array would instead contain [3 5 9 16], and if you ran it another time on the same array, you'd have [3 8 17 33].
    void runningSum(int[] a) {
       int sum = 0;
       for (int i = 0; i < a.length; i++) {
          a[i] += sum;
          sum = a[i];
       }
    }
    
    Or, using assignment of the result of an assignment...
    void runningSum(int[] a) {
       int sum = 0;
       for (int i = 0; i < a.length; i++)
          sum = (a[i] += sum);
    }
    
  8. Write a method that takes as its parameter an "two dimensional?" array a of integers and fills the main diagonal with the value 1. (The main diagonal consists of those entries whose row and column index are equal.) Assume the two dimentional array is square.
    void fillDiagonal(int[][] m) {
       for (int i = 0; i < m.length; i++)
          m[i][i] = 1;
    }
    
  9. Write a method that takes an integer parameter n. It should create and return an array of n arrays of integers, where the array for row 0 has length 0, the array for row 1 has length 1, the array for row 2 has length 2, etc. All values in the arrays can remain uninitialized (0).
    int[][] tieredArray(int n) {
       int[][] result = new int[n][];
       for (int i = 0; i < n; i++)
          result[i] = new int[i];
       return result;
    }
    
  10. Write a method that takes an array of String objects and returns a single String that results from concatenating all the strings together, separated by spaces. Do not add a space after the last element, nor before the first one.
    String joinUp(String[] a) {
       String s = "";
       for (int i = 0; i < a.length; i++) {
          s = s + a[i];
          if (i < a.length-1)
             s = s + " ";
       }
       return s;
    }