{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Tutorial_4_Sol-for-viewing-printing

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

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

View Full Document Right Arrow Icon
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;
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
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

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern