Question

Write a program using "memoization" in order to calculate n choose k (permutation of subsets)? Here's...

Write a program using "memoization" in order to calculate n choose k (permutation of subsets)? Here's what I got so far but can't seem to get the last 3 functions to produce the correct value. Any suggestions on how to fix it?

#include <iostream>

using namespace std;

int values[1000][1000] = { (int)0 };

int n_choose_k(int n, int k) {
   if (k == 0)
       return (int)1;
   if (n == k)
       return (int)1;
   if (values[n][k] != 0)
       return values[n][k];
   return values[n][k] = (n_choose_k(n - 1, k - 1) + n_choose_k(n - 1, k)) % 100000000;
}

int main() {
   cout << n_choose_k(10, 5) << endl;
   cout << n_choose_k(20, 10) << endl;
   cout << n_choose_k(30, 15) << endl; // should be 155117520 but I get 55117520
   cout << n_choose_k(39, 15) << endl; // should be 25140840660
   cout << n_choose_k(39, 14) << endl; // should be 15084504396
   cout << n_choose_k(40, 15) << endl; //should be 40225345056
   system("pause");

   return 0;
}

Homework Answers

Answer #1
  • Change the datatype as unsigned long long so that it can hold the larger value.
  • No need of doing mod with 1000000 because you are not calculating ncr modulo k

Code

#include <iostream>

using namespace std;

unsigned long long values[1000][1000] = { (int)0 };

unsigned long long n_choose_k(int n, int k) {
if (k == 0)
return (int)1;
if (n == k)
return (int)1;
if (values[n][k] != 0)
return values[n][k];
return values[n][k] = n_choose_k(n - 1, k - 1) + n_choose_k(n - 1, k);
}

int main() {
cout << n_choose_k(10, 5) << endl;
cout << n_choose_k(20, 10) << endl;
cout << n_choose_k(30, 15) << endl; // should be 155117520 but I get 55117520
cout << n_choose_k(39, 15) << endl; // should be 25140840660
cout << n_choose_k(39, 14) << endl; // should be 15084504396
cout << n_choose_k(40, 15) << endl; //should be 40225345056
system("pause");

return 0;
}

Test Output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For different input n, i.e., n = 1, n = 10, n = 20, write down...
For different input n, i.e., n = 1, n = 10, n = 20, write down the final value of counter for function1, function2, and function3. Explain the counter result through math calculation. #include <iostream> using namespace std; void function1(int n){ int count = 0; for(int x = 0; x < 12; x++){ cout<<"counter:"<< count++<<endl; } } void function2(int n){ int count = 0; for(int x = 0; x < n; x++){ cout<<"--- x="<<x<<"------"<<endl; for(int i =0; i < n;...
C++ Converting from binary to decimal. Here's what i have. it doesn't work yet unfortunately. Fix?...
C++ Converting from binary to decimal. Here's what i have. it doesn't work yet unfortunately. Fix? #include <conio.h> // For function getch() #include <cstdlib> // For several general-purpose functions #include <fstream> // For file handling #include <iomanip> // For formatted output #include <iostream> // For cin, cout, and system #include <string> // For string data type using namespace std; // So "std::cout" may be abbreviated to "cout" int main() {       int n;    int a[10], i;    int...
Analyze the following program and write down the output. # include <iostream> using namespace std;    void...
Analyze the following program and write down the output. # include <iostream> using namespace std;    void modifyArray( int [ ], int );    void modifyElement ( int );      int main( ) {   const int arraySize = 8;   int a[arraySize] = { 2, -2, 10, -3, -1 ,0, 10, -5 };      modifyArray ( a, arraySize);      for ( int i =0; i < arraySize; i++)               cout << a[i] << ‘  ’;         modifyElement ( a[4] );          for ( int i =0; i <...
Im using visual studio to write a c++ program has a function that will output the...
Im using visual studio to write a c++ program has a function that will output the number if ways a person can climb stairs using only 1 or two steps and and function that when given a number outputs the corresponding fibonacci number. I want to call the functions in main. #include <iostream> using namespace std; int FiboNum(int fiboNum); int Stairs(int nStairs); int main() {    int fiboNum;    int nStairs;    cout << "Enter a number : " <<...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
Analyze the following programs and write down the output of the program. Please print every character...
Analyze the following programs and write down the output of the program. Please print every character (including whitespace character) clearly!       # include <iostream> using namespace std;   int fun( int a ) {       int b = a * 2;       return b;   }   int main()   {       int y = 5;       cout << fun(y) << endl;       cout << fun(-- y) << endl;       cout << fun(y--) << endl;       cout << y <<endl;           return 0;   }
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;     float f=2.22;     float h=-7.5;     char a='S';     char b='M';     cout<<i<<"\t"<<j<<"\t"<<k<<endl;     Cout<<f<<"\t"<<h<<endl;     cout<<a<<"\t"<<b<<endl;     return 0;     }
This is the program that I need to make. Write a program that prints three items,...
This is the program that I need to make. Write a program that prints three items, such as the names of your three best friends or favorite movies, on three separate lines. This is what I did. #include <iostream> using namespace std; int main() { cout << "Suresh" << endl; cout << "Sekhar" << endl; cout << "Anshu" << endl; return 0; } Then it tells me that the program fails to test the submission with three random names. Please...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...