COSC 311 HW 2/2/2016 Due: 2/8/2016 Write iterative solution and write recursive solution to the following problems. All arrays are packed and are no bigger than necessary to contain the data. An example problem and solution are given below. 1. Sum all elements in a 1D array. 2. Sum all elements with values greater than k in a 1D array. 3. Sum all elements in a 2D array. 4. Sum all elements with values greater than k in a 2D array. 5. Given: arr: a 1D array containing MAX elements, arr0: an empty 1D array size ceiling(MAX/2), arr1: an empty 1D array size ceiling(MAX/2). Put the elements at even indexes (0, 2, 4, ...) of arr into arr0 and the elements at odd indexes (1, 3, 5, ...) of arr into arr1. For example arr: [11, 21, 23, 31, 14] --> arr0: [ 11, 23, 14 ] arr1: [ 21, 31 ] ---------------------------------------------------------------------------- Example problem: Compute the sum of the squares of the elements of a 1D array. E.g. For [2, 4, 2, 4] return 40 (2^2 + 4^2 + 2^2 + 4^2) Note: Using array slicing will simplify visualizing the recursive solution because you can cut down the size of the data set for each recursive call. E.g., arr[2 .. 4] is the size 3 array containing only [arr[2], arr[3], arr[4]] E.g., for arr = [2, 4, 5, 7, 9, 1] arr[1 .. 3] is [4, 5, 7]. - - - - - // Iterative solution answer = 0; for (i = 0; i < sizeof(arr); i++) answer = answer + (arr[i] * arr[i]); output (answer); - - - - - // Recursive solution sumSquares(arr) { if (sizeOf(arr) == 0) return 0; return (arr[0] * arr[0]) + sumSquares(arr[1 .. n]); // reduce size of arr // for recursive call } main() { output ( sumSquares([2, 4, 2, 4]) );