Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
void CountSort(int A[],int B[], int k, int size)
{
    const int mx=k; 
    int C[mx];//error:expression must have a constant value!!!

...

}

mx is constant! What is wrong here??

[From asker's "solution"]
C++
void CountSort(int[],int[], int , int);

int _tmain(int argc, _TCHAR* argv[])
{


const int size=12;

int A[size]={0,2,3,1,7,9,6,45,12,10,20,15};
int B[size];

int max=45;

CountSort(A,B,max,size);

//for(int i = 0 ; i<size;> // cout<<A[i]<<" ";
for(int i = 1 ; i<size;> cout<<B[i]<<" ";


return 0;
}


void CountSort(int A[],int B[], int k, int size)
{

int *C = new int[k];

for(int i =0;i<=k;i++)
C[i]=0;

for(int j =1; j C[A[j]] = C[A[j]] + 1 ;

for (int i = 1; i<=k ;i++)
C[i]= C[i] + C[i-1] ;

for (int j = size-1 ;j>0;j--)
{
B[C[A[j]]] = A[j] ;
C[A[j]] = C[A[j]] - 1 ;
}

//delete[] C;

}
Posted
Updated 3-Dec-12 4:54am
v2
Comments
CPallini 3-Dec-12 12:23pm    
Please post the actual code (with proper formatting - for instance for loop line is incomplete in the above code).

No, it doesn't, not really anyways. I'm a little surprised the error isn't the line before that honestly.

But let's break it down: the value of mx is k, but the compiler does not know what k will be until runtime. This might be allowable, but the compiler has no idea how big to make C, which means it can't continue. If you need a size that's unknown at compile time, you need a dynamic array.

Try this instead:
C++
void CountSort(int A[],int B[], int k, int size)
{
    int* C = new int[k];

    ...

    delete[] C; // clean up the memory you allocated when you're done with it
}
 
Share this answer
 
v2
Comments
missak boyajian 3-Dec-12 10:26am    
Thanks it worked. But one more problem. When i use delete[] C ==> its giving error when running the program. When I do not use it, it works fine.
lewax00 3-Dec-12 10:30am    
If it's a small program it probably won't matter (i.e. if this function is only run a few times) because the memory will be reclaimed when the program exists, but what is the error?
Albert Holguin 3-Dec-12 15:02pm    
I'd fix the problem though... it's better to spend the time to figure out what's wrong than to learn to live with memory leaks.
missak boyajian 3-Dec-12 10:34am    
Debug Error!
Program:...

HEAP CORRUPTION DETECED: after Normal block...
CRT detected that the application wrote to memory after end of heap buffer.
lewax00 3-Dec-12 10:49am    
That sounds like you're writing something after the end of C, it's hard to say without seeing the rest of the code.
Arrays can only be initialized by using compile time constants like
C++
static const int mx = 9;
int C[mx];


But you cannot assigned a non-static variable to a static constant.
static const int mx = k; // Invalid if k is a function parameter


The most simple solution is to use the Standard Template Library which provides the std::vector template:
C++
#include <vector>

void CountSort(int A[],int B[], int k, int size)
{
    std::vector<int> C(k);

...
 
}
 
Share this answer
 
v2
C#
void CountSort(int A[],int B[], int k, int size)
{
    const int mx=k;
    int C[mx];//error:expression must have a constant value!!!

...

}



try using new for this

void CountSort(int A[],int B[], int k, int size)
{
const int mx=k;
//int C[mx];//error:expression must have a constant value!!!

int* C = new int[k];//or new int[mx] anyway is fine
//You can use it as a normal arrray
C[somevalue]=someoperation;

...

}

and while you're at it do have a look at dynamic memory allocation.
 
Share this answer
 

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