Tutorial_4_Sol-for-viewing-printing

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

Info iconThis 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;
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background 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

This note was uploaded on 08/18/2011 for the course COMP 3868 taught by Professor Keithchan during the Summer '97 term at Hong Kong Polytechnic University.

Page1 / 2

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

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online