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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education