With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Solved

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.

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.

3 Comments

The problem is that you've exceeded the memory that's been allocated at compile time when MAX = 2000000. To fix this problem you should dynamically allocate the memory for DataArray instead i.e.

unsigned long *DataArray = new unsigned long[2000000];

and then deallocate the memory at the end of your function with

delete [] DataArray;

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Course of the Month10 days, 7 hours left to enroll

Join the community of 500,000 technology professionals and ask your questions.