D
Debaser
Okay, I'm one error away from making this work. Here's my main():
int main(void)
{
size_t x;
long y;
long samples[] = { 10, 50, 100, 500, 1000, 5000, 10000, 50000,
100000, 500000, 1000000 };
/* Seed the random number generator */
srand(time(0));
/* Run test */
for(x = 0; x < sizeof(samples)/sizeof(long); x++) {
/* Allocate memory */
long *nums = new long[samples[x]];
/* Fill array to be sorted with random numbers */
for (y = 0; y < samples[x]; y++) {
nums[y] = rand() % samples[x];
}
cout << "Insertion Sort Time with '" << samples[x] << "'
elements: "
<< insertion_sort(nums, samples[x]) << endl;
.....
many other sort algorithms follow with the exact same call...
Yes, I realize it's costly to fill the array each time, but the speed
of main() is not an issue...it's just a HW assignment. Just for the
record: I know better. Then at the end of the for loop we have:
.....
/* Fill array to be sorted with random numbers */
for (y = 0; y < samples[x]; y++) {
nums[y] = rand() % samples[x];
}
cout << "Bucket Sort Time with '" << samples[x] << "'
elements: "
<< bucket_sort(nums, samples[x]) << endl;
/* Debug */
for (int z = 0; z < samples[x]; z++ )
cout << nums[z] << "\t";
cout << endl;
/////////// The numbers print out sorted nums fine but the delete call
////////// crashes the program....WHY????
delete [] nums;
}
return EXIT_SUCCESS;
}
Please overlook code efficiency or cryptic variable names. This was
orginally written in C and the same thing happened with free() using
calloc() instead of new.
I apologize in advance for my ignorance.
-Mike-
int main(void)
{
size_t x;
long y;
long samples[] = { 10, 50, 100, 500, 1000, 5000, 10000, 50000,
100000, 500000, 1000000 };
/* Seed the random number generator */
srand(time(0));
/* Run test */
for(x = 0; x < sizeof(samples)/sizeof(long); x++) {
/* Allocate memory */
long *nums = new long[samples[x]];
/* Fill array to be sorted with random numbers */
for (y = 0; y < samples[x]; y++) {
nums[y] = rand() % samples[x];
}
cout << "Insertion Sort Time with '" << samples[x] << "'
elements: "
<< insertion_sort(nums, samples[x]) << endl;
.....
many other sort algorithms follow with the exact same call...
Yes, I realize it's costly to fill the array each time, but the speed
of main() is not an issue...it's just a HW assignment. Just for the
record: I know better. Then at the end of the for loop we have:
.....
/* Fill array to be sorted with random numbers */
for (y = 0; y < samples[x]; y++) {
nums[y] = rand() % samples[x];
}
cout << "Bucket Sort Time with '" << samples[x] << "'
elements: "
<< bucket_sort(nums, samples[x]) << endl;
/* Debug */
for (int z = 0; z < samples[x]; z++ )
cout << nums[z] << "\t";
cout << endl;
/////////// The numbers print out sorted nums fine but the delete call
////////// crashes the program....WHY????
delete [] nums;
}
return EXIT_SUCCESS;
}
Please overlook code efficiency or cryptic variable names. This was
orginally written in C and the same thing happened with free() using
calloc() instead of new.
I apologize in advance for my ignorance.
-Mike-