Problem initializing array size to variable

int counter = 0;
int minSol = INT_MIN;
int numbers[] = {1,2,3,4,5,6,7,8,9,0};
string s = "", s1 = "";
ofstream fout("F:\\C\\MyProject1\\Output.txt");


template<class T, int size>
int GetArrLength(T(&)[size]){
return size;
}

void invert(bool b[], int size) {
for (int i=0; i<size; i++) {
b[i] = !b[i];
}
}

int sum(bool a[], int size) {
int sum = 0;
for(int i=0; i< size; i++) {
if (a[i])
sum += numbers[i];
}
return sum;

}

void generate(bool auxA[], int pos, int cur, int k, int N) {
if (pos == k) {
s = ""; s1 = "";
int difference = 0;
for (int i=0; i<N; i++) {
if (auxA[i])
s += IntToStr(numbers[i]);
else
s1 += IntToStr(numbers[i]);
}
bool auxB[N];
memcpy(auxB,auxA,N);
for (int j=0; j<N; j++){
auxB[j] = auxA[j];
}
invert(auxB,N);
fout<<++counter<<". "<<s<<" | "<<s1<<endl;
fout<<"Sum ==> "<<sum(auxA,N)<<" | "<<sum(auxB,N)<<endl;
difference = abs(sum(auxA,N) - sum(auxB,N));
fout<<"Difference = "<<difference<<endl;
minSol = (difference > minSol) ? difference : minSol;
return;
}
for (int i=cur; i<N; i++) {
auxA[i] = !auxA[i];
generate(auxA,pos + 1,i + 1,k,N);
auxA[i] = !auxA[i];
}
}

int _tmain(int argc, _TCHAR* argv[])
{
bool auxA[] = {false,false,false,false,false,false,false,false,false,false};
int n = GetArrLength(numbers);
string astr;
generate(auxA,0,0,5,n);
fout<<"Largest difference is: "<<minSol;
cin >> astr;
return 0;

}

The problem I have is with the line bool b[N]; inside the generate() function. The compiler prompts me that error C2466: cannot allocate an array of constant size 0.
I am new to C, C++ programming despite having prior experience in Java and vb.net. I am programming using my knowledge of the languages I know.
Would appreciate some enlightenment on this area. Thank you all.
You would have to use new to allocate that array (unless you are using C99, but you are using C++ so...)

http://www.cplusplus.com/doc/tutorial/dynamic/
Topic archived. No new replies allowed.