Question: Complete the code: int combinations (int n, int k) - This method takes two int parameters and returns the number of combinations when choosing k
Complete the code:



int combinations (int n, int k) - This method takes two int parameters and returns the number of combinations when choosing k elements out of a set of n objects, also known as n choose k. To receive any credit, you should NOT use multiplication, division, factorial, or recursion. Only a one-dimensional array is allowed. Hint: use dynamic programming. int numOrders (int n) - This recursive method takes an int n and returns the number of orders to multiply n chained matrices. For example, there is only one order to multiply two matrices: (A1A2), two orders to multiply three matrices: (A1A2)A3, A1(A2A3), and five orders to multiply four matrices (see lecture slides). You are not required to list all the combinations, just return the number of orders for a given n. int maxMulti(int n, int[] d, int[] [] M, int[][] P) - This method takes as parameters the number of matrices, the matrix dimensions, and two matrices m and p. It builds an order matrix (saved in the two-dimensional int array P) and returns the maximum number of element multiplications when multiplying matrices Aj through An. Note that you should assume that the indices of both matrices start at 1.4 public class Practice // This method takes two int parameters and returns the number of combinations when choosing // k elements out of a set of n objects, also known as n choose k. To receive any credit, you // should NOT use multiplication, division, factorial, or recursion. Only a single one-dimensional // array is allowed. public int combinations (int n, int k) { // TODO: implement this method return -1; // replace this statement with your own return } // This method takes an integer n and returns the number of ways to multiply n chained matrices public int numOrders (int n) { // TODO: implement this method return -1; // replace this statement with your own return } // This method finds and returns the maximum element multiplications and build the order matrix P public int maxMulti(int n, int[] d, int[][] M, int[][] P) { // TODO: implement this method return -1; // replace this statement with your own return } // This method takes as parameters an int array P, two ints, as well as a // String and recursively updates the string so that it represents the order // of multiplications. // Do not make any changes to this method! public String buildOrder(int[][] P, int i, int j, String order) { if (i == j) return order + "A" + i; else { int k = P[i][j]; String tmp = order + "("; tmp = buildorder(P, i, k, tmp); tmp = buildorder(P, k + 1, j, tmp); return tmp + ")"; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
