Jun 13, 2013 at 8:49am Jun 13, 2013 at 8:49am UTC
hi
i get this message in .net :
Error 1 error C2540: non-constant expression as array bound \documents\visual studio 2010\projects\test3\test3\test3.cpp 18 1 Test3
Error 2 error C2440: 'initializing' : cannot convert from 'long (*)[1]' to 'long *' \documents\visual studio 2010\projects\test3\test3\test3.cpp 18 1 Test3
Error 3 error C2109: subscript requires array or pointer type \documents\visual studio 2010\projects\test3\test3\test3.cpp 24 1 Test3
http://up.clip2ni.com/i/images/gw01igmnagww1ojhmfl8.jpg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// Test3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
long int bin(int n,int k);
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << " example : \n" << bin(10,5) << "\n" ;
return 0;
}
long int bin(int n , int k)
{
// long int b[n][k];
long int *b = new long int [n][k];
int i,j;
for (i=0;i<n;i++)
for (j=0;j<i;j++) // For (j=0;j< min(I,k);j++)
{
if (j==0 || j==i)
b[i][j]=1;
else
b[i][j]= b[i-1][j]+b[i-1][j-1];
}
return b[n][k];
}
why ?!
tnx
Last edited on Jun 13, 2013 at 9:12am Jun 13, 2013 at 9:12am UTC
Jun 13, 2013 at 9:34am Jun 13, 2013 at 9:34am UTC
Your code appears to have nothing to do with the subject of the thread or the image you linked, other than having variables named n and k .
You don't want a two dimensional array here if the subject is accurate. What are you trying to do exactly? Describe the algorithm you're attempting to implement in bin .
Jun 13, 2013 at 9:35am Jun 13, 2013 at 9:35am UTC
That's not how you dynamically allocate 2-dimensional arrays.
I'm sure a suitable Google search will find you some examples of how to do it. My advice would be to stop using C-style arrays, and start using STL vectors instead.
Last edited on Jun 13, 2013 at 9:36am Jun 13, 2013 at 9:36am UTC
Jun 13, 2013 at 2:22pm Jun 13, 2013 at 2:22pm UTC
it's generally a bad code habit for a c++ beginner declare a 2 dimensional array with pointer. It's so much old style :)
However if you want to do it no matter what, you should declare a long int double pointer
long int **b
otherwise you are declaring a one dimensional array.