Solved

# error message

Posted on 2003-02-27

Hi:

I'm testing a sort algorithm using an array, but as I increase the array size and run the program I get an error message.

This is the program:

#include <ctime>

#include <iostream>

#include <stdlib.h>

#include <limits.h>

using namespace std;

typedef unsigned long Item;

//Shellsort function

void shellsort(Item a[], int l, int r)

{

int h;

for(h=1; h <= (r-l)/9; h = 3*h+1);

for(; h>0; h/= 3)

for(int i= l+h; i<=r; i++)

{

int j = i; Item v = a[i];

while (j >= l+h && v < a[j-h])

{

a[j] = a[j-h]; j -= h;

}

a[j] = v;

}

}

int main()

{

//For this line I get 2147483647

cout<<"Max. long = "<<LONG_MAX<<endl;

//for this line I get -2147483647

cout<<"Min. long = "<<LONG_MIN<<endl;

//Clock initialization

clock_t start, finish;

start = clock();

finish = clock();

double delay = (double)(finish - start);

double elapsed = (double)(finish - start);

elapsed -= delay;

//Array initialization

const unsigned long MAX = 2000000;

unsigned long DataArray[MAX];

//Input data

long a, b, temp;

a = 0;

b = MAX - 1;

temp = b;

cout<<"Loading array ..."<<endl;

for(int i=0; i < MAX; i++)

{

temp = temp - 1;

DataArray[i] = temp;

}

//Start time

start = clock();

//Sort data

shellsort(DataArray, a, b);

//Finish time

finish = clock();

//calculate time

double clock_ticks = finish - start - delay;

double sortTime = clock_ticks / CLOCKS_PER_SEC;

//output data

cout<<"It took "<<sortTime<<" seconds to sort the array."<<endl;

return 0;

}

I would really appreciate if someone could tell me why this is happening if I add another zero to MAX.

Thanks.