I need some examples about Dynamic Programming at Language C. I am study algorithm with C now but at this book, examples are too hard to understand. Does anyone give me some example for me?
Asked
Active
Viewed 315 times
1 Answers
0
A recursive function:- without dynamic programming it will call the same function multiple times.
int nCr(int n,int r){
if (r == 0 || r == n || n == 1 || n == 0){
return 1;
}
else{
return nCr(n-1,r) + nCr(n-1,r-1);
}
}
Now with the help of Dynamic Programming:
#include <stdio.h>
int main(void) {
int n,r;
scanf("%d%d",&n,&r);
int mem[n+1][r+1];
int i,j;
for(i=0;i<n+1;i++)
{
for(j=0;j<r+1;j++)
{
if (j == 0 || j == i || i == 1 || i == 0)
mem[i][j]=1;
else
mem[i][j]=mem[i-1][j]+mem[i-1][j-1];
}
}
printf("%d",mem[n][r]);
return 0;
}
I have used a 2D-array to save values so that i can use them instead of calling the function again and again. I have used Dynamic Programming concept.

anupam691997
- 310
- 2
- 8
-
you can upvote the answer if you understood it. – anupam691997 Nov 19 '17 at 12:23