Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this code for it but it doesn't compile correctly: mport java.util.Scanner; class Main{ static int ncr( int n, int r){ int ans=1; for

image text in transcribed

I have this code for it but it doesn't compile correctly:

mport java.util.Scanner; class Main{ static int ncr(int n, int r){ int ans=1; for(int i=1;ireturn ans; } static int rec(int n, int r){ // System.out.println(n+" "+r);  if(nreturn 0; if(r==0){ return 1; } return rec(n-1,r-1)+rec(n-1,r); } static int dp[][],calls=0; static int recdp(int n, int r){ // calls++;  if(nreturn 0; // calls++;  if(r==0)return 1; calls++; if(dp[n][r]!=0)return dp[n][r]; dp[n][r] = recdp(n-1,r-1)+recdp(n-1,r); return dp[n][r]; } public static void main(String[] args) { System.out.println("Enter n and k"); Scanner obj = new Scanner(System.in); int n = obj.nextInt(); int k = obj.nextInt(); System.out.println("Using loop : "+ncr(n,k)); System.out.println("True recursion : "+rec(n,k)); dp = new int[n+1][k+1]; calls=0; System.out.println("Recursion with stored values "+recdp(n,k)+" The number of calls is "+calls); } }
Write one Java program that, in three different ways, calculates the binomial coefficients or C(n.k). Note that ,is definied for any n2kz0, otherwise Specific requirements: 1. Part (a): Use a loop to compute 2. In Part (b), use pure recursion to compute. Here is a recursive formula GH","HE:)with boundary values(,)-landd-i should count how many times recursive calls are made. 3. Part (c) is considered to be an improved version of Part (b). You may use an array (2-dimessional) to store some values that has already been computed using recursion so that when making recursive calls the program does not compute certain values over and over again 4. Prompt user to enter two integers as n and k. Report the values of together with the number of recursive calls in each way. Here is a sample output: Enter two integers as n and k to compute C(n,k): 10 5 (a) use a loop: C (10,5)-252 (b) use prue recursion: C(10,5)-252 The number of calls is 502 (c) use recursion with some stored values: C(10,5)-252 The number of calls is 50 You should name your program as al0.java. Please submit java

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions