Skip to content

Why use the c.incremen(); in the Ex_1_1_27a.java? #3

Description

In the Ex_1_1_27a.java binomial() function, I test the code,but there is a error :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method incremen() is undefined for the type Counter

In your program, the incremen() is undefined, so, how can the this program run successfully?

I know the Counter c is used to cumulative the number of recursive calls, but can't understand why use the function c.incremen().

I think now that c is used to cumulative the number of recursive calls, I use this program:

public static double binomial( int n, int k, double p, int c) {
        if ( n == 0 && k == 0) {
            return 1.0;
        }
        if ( n < 0 || k < 0) {
            return 0.0;
        }

        c++;  

        return (1.0 - p) * binomial( n - 1, k, p, c) + p * binomial( n - 1, k - 1, p, c);
    }
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]),
            k = Integer.parseInt(args[1]);
        double p = Double.parseDouble(args[2]);

        int c = 0;

        double b = binomial(n, k, p, c);

        StdOut.println(b);
        StdOut.println(c);
    }

I test 10 5 0.5, but I get the result: 0.24609375 0.

I hope to help me Solve this doubt,thanks a lot !

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions