Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi.
i have the code below:

C++
int dynamicbinomial(int n, int k)
{
	int* b = new int[n,k];
	for(int i=0; i<= n; i++)
	{
		for(int j=0; j<= min(i,k); j++)
		{
			if(j==0 || j==i)
				b[i,j] = 1;
			else if(j==1 || j==i-1)
				b[i,j] = i;
			else
			        b[i,j] = b[i-1,j-1] + b[i-1,j];			
		}
	}
	return b[n,k];
}


it works correctly but in the last line:
C++
b[i,j] = b[i-1,j-1] + b[i-1,j];


when it reaches that line it does like it:
b[i,j] = b[0,j-1] + b[1,j-1] + b[2,j-1] + b[3,j-1] + ... + b[i-1,j] ;

it shows the sum of all numbers in line (i-1) form 0 to j

if you enter 5 as n and 3 as k you must have a matrix as below:
1
1  1
1  2  1
1  3  3  
1  4  6  
1  5  10 


but it makes a matrix like it:

1
1 1
1 2 1
1 3 3
1 4 7
1 5 12


traced it this way:

1- i= 0 j= 0 k= 3 min( i,k )= 0 b[i,j]= b[0,0]= 1
2- i= 1 j= 0 k= 3 min( i,k )= 1 b[i,j]= b[1,0]= 1

3- i= 1 j= 1 k= 3 min( i,k )= 1 b[i,j]= b[1,1]= 1

4- i= 2 j= 0 k= 3 min( i,k )= 2 b[i,j]= b[2,0]= 1

5- i= 2 j= 1 k= 3 min( i,k )= 2 b[i,j]= b[2,1]= 2

6- i= 2 j= 2 k= 3 min( i,k )= 2 b[i,j]= b[2,2]= 1

7- i= 3 j= 0 k= 3 min( i,k )= 3 b[i,j]= b[3,0]= 1

8- i= 3 j= 1 k= 3 min( i,k )= 3 b[i,j]= b[3,1]= 3

9- i= 3 j= 2 k= 3 min( i,k )= 3 b[i,j]= b[3,2]= 3

10- i= 3 j= 3 k= 3 min( i,k )= 3 b[i,j]= b[3,3]= 1

11- i= 4 j= 0 k= 3 min( i,k )= 3 b[i,j]= b[4,0]= 1

12- i= 4 j= 1 k= 3 min( i,k )= 3 b[i,j]= b[4,1]= 4

13- i= 4 j= 2 k= 3 min( i,k )= 3 b[i,j]= b[4,2]= 7

14- i= 4 j= 3 k= 3 min( i,k )= 3 b[i,j]= b[4,3]= 4

15- i= 5 j= 0 k= 3 min( i,k )= 3 b[i,j]= b[5,0]= 1

16- i= 5 j= 1 k= 3 min( i,k )= 3 b[i,j]= b[5,1]= 5

17- i= 5 j= 2 k= 3 min( i,k )= 3 b[i,j]= b[5,2]= 12

see line 13 ?
Posted
Updated 2-Jun-13 7:09am
v4
Comments
Sergey Alexandrovich Kryukov 2-Jun-13 12:26pm    
Did you try to run it step by step using the debugger and check up each step?
—SA
m.r.m.40 2-Jun-13 12:44pm    
yes alexandrovix,
but couldn't get anything of it.
Sergey Alexandrovich Kryukov 2-Jun-13 14:19pm    
Sorry, this isn't my name. Anyway, your "couldn't get..." is not informative. The ability to debug code is more important than your particular problem...
—SA
m.r.m.40 2-Jun-13 18:23pm    
Please help.
it took 1 day for me to solve it. I'm still working on it, but yet no result.
Thank you,

I have modified your function. Hope it helps. Make sure to release the allocated memory.
C#
int dynamicbinomial(int n, int k)
{
    int **b = new int*[n];
    for(int i = 0; i < n; ++i) {
    b[i] = new int[k];
    }

    // Print the address of the allocated array
    for (int i=0; i<n; ++i)
    {
        for (int j=0; j<k; j++)
        {
            std::cout<< &b[i][j] << " ";
        }
        std::cout<<"\n";
    }

    for(int i=0; i< n; i++)
    {
        for(int j=0; j< std::min(i+1,k); j++)
        {
            if(j==0 || j==i)
                b[i][j] = 1;
            else if(j==1 || j==i-1)
                b[i][j] = i;
            else
            {
                b[i][j] = b[i-1][j-1] + b[i-1][j];
            }

            // Print the values and address
            std::cout<<b[i][j] << " " <<std::cout<< &b[i][j] << " ";
        }
        std::cout<<"\n";
    }
    return b[n][k];
}
 
Share this answer
 
v2
Comments
m.r.m.40 3-Jun-13 4:38am    
you almost saved my life.
thank you,

declaring 2d array was my problem.


int **b = new int*[n];
for(int i = 0; i <= n; ++i)
b[i] = new int[k];

thanks,
Have you looked closely at your code?
Let's just shrink it a bit...
C#
int* b = new int[n, k];
for (int i = 0; i <= n; i++)
    {
    for (int j = 0; j <= Math.Min(i, k); j++)
        {
        b[i, j] = 1;
        }
    }
how many elements are there in the array, and how many are you assigning "1" to?
 
Share this answer
 
Comments
m.r.m.40 2-Jun-13 13:10pm    
n * k
at maximum
OriginalGriff 2-Jun-13 13:33pm    
Are you sure about that?
How many times do you go round each loop if n and k are both one?
m.r.m.40 2-Jun-13 13:42pm    
look griff i cant get what you mean.
could you please be straight?
OriginalGriff 2-Jun-13 14:36pm    
int [4] declares an array of four integers.
for (i = 0; i <= 4; i ++)
Executes 5 times, when i equals 0,1,2,3, and 4
Your two loops run off the end, and start to corrupt data...
m.r.m.40 2-Jun-13 18:19pm    
this is what i want:
when input is 5 it means make an array from 0 to 5.
and also the for loop is needed to act 6 times if n is 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900