D
Debaser
Could someone point out the error in the following code. It fails on
the last line.
double counting_sort(long* input, long inSize)
{
time_t start, end;
long x, k = 0, count = 0;
long *b, *c;
/* Get size of c array */
for (x = 0; x < inSize; x++) {
if (k < input[x]) {
k = input[x];
}
}
/* Allocate sorted array */
b = new long[inSize];
if (!b) {
cerr << "Out of memory.\n";
exit(EXIT_FAILURE);
}
/* Allocate storage array */
c = new long[k+1];
if (!c) {
cerr << "Out of memory.\n";
exit(EXIT_FAILURE);
}
time(&start);
/* init c */
for (x = 0; x <= k; x++) {
c[x] = 0;
}
/* count elements equal to x */
for (x = 0; x < inSize; x++) {
c[input[x]]++;
}
.....
It crashes on the first interation of the last for loop. I'm sure it's
just some stupid oversight between pointers and values, but I can't
figure it out.
Thanks in advance.
-Mike-
the last line.
double counting_sort(long* input, long inSize)
{
time_t start, end;
long x, k = 0, count = 0;
long *b, *c;
/* Get size of c array */
for (x = 0; x < inSize; x++) {
if (k < input[x]) {
k = input[x];
}
}
/* Allocate sorted array */
b = new long[inSize];
if (!b) {
cerr << "Out of memory.\n";
exit(EXIT_FAILURE);
}
/* Allocate storage array */
c = new long[k+1];
if (!c) {
cerr << "Out of memory.\n";
exit(EXIT_FAILURE);
}
time(&start);
/* init c */
for (x = 0; x <= k; x++) {
c[x] = 0;
}
/* count elements equal to x */
for (x = 0; x < inSize; x++) {
c[input[x]]++;
}
.....
It crashes on the first interation of the last for loop. I'm sure it's
just some stupid oversight between pointers and values, but I can't
figure it out.
Thanks in advance.
-Mike-