//Can you please debug this program. Thank you #include #include //This function takes n and k and computes n using Pascal’s Rule. long choose(int n, int k); //This functions below create a memoization table containing //long values of dimension (n+1)×(k+1) long chooseWithMemoization(int n, int k); //This is a new recursive function that also takes the table as a parameter. //When the function needs to compute (n , k )it checks the table first, and //if the value has already been comput(is not -1) then it returns that value. long chooseWithMemoizationRecursive(int n, int k, long **tableau); int main(int argc, char **argv) { //variable declaration int n; int k; scanf("%d",&n); scanf("%d",&k ); long choice = -1; //printing out the out put choice = chooseWithMemoization(n,k); printf("choose(%d,%d) = %ld\n", n, k, choice); return 0; } /** * This function takes n and k and computes n using Pascal’s Rule. */ long choose(int n, int k) { if ( k == 0 || k == 0) { printf("invalid inputs"); return 1; } if(k < 0 || n < 0) { exit(1); } return choose(n - 1, k) + choose(n - 1, k - 1); } //This functions below create a memoization table containing //long values of dimension (n+1)×(k+1) long chooseWithMemoization(int n, int k) { //test cases first if(k < 0 || n < 0 ) { printf("invalid inputs: choose(%d, %d), quitting on you...\n", n, k); exit(1); } int i, j; long** tableau = (long**)malloc(sizeof(long*) * (n + 1)); for(i=0; i<=n; i++) { tableau[i] = (long*)malloc(sizeof(long) * (k + 1)); for(j=0; j<=k; j++) { if (j == 0 || j == i) { tableau[i][j] = 1; } else{ tableau[i][j] = -1; } } } return chooseWithMemoizationRecursive(n, k, tableau); } //new recursive function that also takes the table as a parameter. //When the function needs to compute (n , k )it checks the table first, and //if the value has already been comput(is not -1) then it returns that value. long chooseWithMemoizationRecursive(int n, int k, long **tableau) { long value = -1; if(k < 0 || n < 0) { printf("invalid inputs: choose(%d, %d), quitting on you...\n", n, k); exit(1); } if (tableau[n][k] != -1) { value = tableau[n][k]; return value; } tableau[n][k] = chooseWithMemoizationRecursive(n - 1, k,tableau) + chooseWithMemoizationRecursive(n - 1, k - 1,tableau); value = tableau[n][k]; return value; }
//Can you please debug this program. Thank you
#include<stdio.h>
#include<stdlib.h>
//This function takes n and k and computes n using Pascal’s Rule.
long choose(int n, int k);
//This functions below create a memoization table containing
//long values of dimension (n+1)×(k+1)
long chooseWithMemoization(int n, int k);
//This is a new recursive function that also takes the table as a parameter.
//When the function needs to compute (n , k )it checks the table first, and
//if the value has already been comput(is not -1) then it returns that value.
long chooseWithMemoizationRecursive(int n, int k, long **tableau);
int main(int argc, char **argv) {
//variable declaration
int n;
int k;
scanf("%d",&n);
scanf("%d",&k );
long choice = -1;
//printing out the out put
choice = chooseWithMemoization(n,k);
printf("choose(%d,%d) = %ld\n", n, k, choice);
return 0;
}
/**
* This function takes n and k and computes n using Pascal’s Rule.
*/
long choose(int n, int k) {
if ( k == 0 || k == 0) {
printf("invalid inputs");
return 1;
}
if(k < 0 || n < 0) {
exit(1);
}
return choose(n - 1, k) + choose(n - 1, k - 1);
}
//This functions below create a memoization table containing
//long values of dimension (n+1)×(k+1)
long chooseWithMemoization(int n, int k) {
//test cases first
if(k < 0 || n < 0 ) {
printf("invalid inputs: choose(%d, %d), quitting on you...\n", n, k);
exit(1);
}
int i, j;
long** tableau = (long**)malloc(sizeof(long*) * (n + 1));
for(i=0; i<=n; i++) {
tableau[i] = (long*)malloc(sizeof(long) * (k + 1));
for(j=0; j<=k; j++) {
if (j == 0 || j == i) {
tableau[i][j] = 1;
}
else{
tableau[i][j] = -1;
}
}
}
return chooseWithMemoizationRecursive(n, k, tableau);
}
//new recursive function that also takes the table as a parameter.
//When the function needs to compute (n , k )it checks the table first, and
//if the value has already been comput(is not -1) then it returns that value.
long chooseWithMemoizationRecursive(int n, int k, long **tableau) {
long value = -1;
if(k < 0 || n < 0) {
printf("invalid inputs: choose(%d, %d), quitting on you...\n", n, k);
exit(1);
}
if (tableau[n][k] != -1) {
value = tableau[n][k];
return value;
}
tableau[n][k] = chooseWithMemoizationRecursive(n - 1, k,tableau) + chooseWithMemoizationRecursive(n - 1, k - 1,tableau);
value = tableau[n][k];
return value;
}
Step by step
Solved in 3 steps with 2 images