{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Tutorial_4_Sol-for-viewing-printing

# Tutorial_4_Sol-for-viewing-printing - for(int j =-2 j<=...

This preview shows pages 1–2. Sign up to view the full content.

CC2203 Data Structures and Algorithms Sem 2, 2009-2010 Page 1 of 2 Tutorial 4 Solution Ex 1 answer: public class YourCombinatorial { public int factorial(int n, int bound) { if (n < bound) return -1; else if (n == bound) return 1; else return n * factorial(--n, bound); } public double combinatorial(int n, int k) { if (n < 0 || k < 0) return -2.0; else if (n < k) return -1.0; else return factorial(n, n - k) / factorial(k, 0); } /** test program */ public static void main(String [] args) { // test default constructor YourCombinatorial a = new YourCombinatorial(); String display; double result;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: for (int j = -2; j <= 5; j++) for (int p = -2; p <= 5; p++) { display = "c(" + j + ", " + p + ") "; result = a.combinatorial(j, p); if (result == -1.0) display = display + "-> n should not be smaller than k!"; else if (result == -2.0) display = display + " -> no negative input!"; else display = display + "= " + result; System.out.println(display); } } } CC2203 Data Structures and Algorithms Sem 2, 2009-2010 Page 2 of 2 The required java source codes are given in YourCombinatorial.java file. To compile and execute, at the DOS prompt: javac YourCombinatorial.java java YourCombinatorial...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern