How to fix these error for radio sort algorithm?

There are two errors on this header file:

Line 22: expression did not evaluate to a constant.
Line 33: array type int [n] is not assignable.

radixSort.h

#ifndef RADIXSORT
#define RADIXSORT

// Function to get maximum value in array a[].
template <typename dataType>
int getmax(dataType a[], int n)
{
int max = a[0];
for (int x = 1; x < n; x++)
if (a[x] > max)
max = a[x];
return max;
}

// Function to do counting sort according to significant digits repesented by
// exp (where exp is 10^i).
template <typename dataType>
void CountSort(dataType a[], int n, int exp)
{
int result[n], i, count[10] = { 0 };

// Counting occurence of digits
for (i = 0; i < n; i++)
count[(a[i] / exp) % 10]++;

// Changing the position of count so that it appears at actual position in result.
for (i = 1; i < 10; i++)
count[i] += count[i - 1];

// Resultant output array
for (i = n - 1; i >= 0; i--)
{
result[count[(a[i] / exp) % 10] - 1] = a[i];
count[(a[i] / exp) % 10]--;
}
for (i = 0; i < n; i++)
a[i] = result[i];
}

// Radix Sort to sort a[] of given size.
template <typename dataType>
void radixSort(dataType a[], int n)
{
int exp, i;
i = getmax(a, n);
for (exp = 1; i / exp > 0; exp *= 10)
CountSort(a, n, exp);
}
#endif // RADIXSORT
you cannot have a variable as an array:

int n;
cin >> n;
int x[n]; //illegal: n is not a constant.

you can fix it 3 main ways:
1) use vector, not array. this is preferred in c++. vector<int>x(n);
2) use a pointer. int*x = new int[n]; //remember to delete it!
3) use oversized arrays: int x[1000000]; //how big you can make this depends on your compiler/OS/hardware/etc, and even how big 'int' is on your machine. at some point, it won't fit on the stack anymore and you are forced to use option 2. This approach also forces you to keep the 'virtual size' in a variable, in your case n, so you only use n elements of all that space.

Last edited on
Oh, that would be helpful. Thanks.
However, how should I format for those 3 ways?
Last edited on
keep braces on a line by themselves and indent between them 3 spaces, recursively for nested braces.
(I don't think you asked the question you meant to ask here?).
Last edited on
I'm confused about what you're saying.
Can you ask your question again?
How can I accomplish for each way?
mlanuri10 wrote:
Line 22: expression did not evaluate to a constant.
Line 33: array type int [n] is not assignable.

If you had used code tags those errors would have made more sense. Your source doesn't show line numbers here.

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier!

http://www.cplusplus.com/articles/jEywvCM9/

HINT: you can edit your post and add them.
I am not going to rewrite your whole program for you 3 times over.
you change the array for the things I gave you.
eg
int result[n]
becomes
vector<int> result(n);
etc.
result[index] still works exactly like an array here. that also works for a pointer and it also works for an oversized array. so you don't have to change too much, just any variable length arrays need to be changed and if you choose to do pointers, you need to release the memory in the correct places.
Topic archived. No new replies allowed.