//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; }

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.4: A Case Study: Rectangular To Polar Coordinate Conversion
Problem 9E: (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by...
icon
Related questions
Question

//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;
}

 

 

Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning