d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Me With My Sort > Also Get An Error
Add Reply New Topic New Poll
Member
Posts: 22,502
Joined: Aug 5 2011
Gold: 0.00
Jan 21 2013 03:40pm
So this is the question's I've recieved.

a) Place each value of the one-dimensional array into a row of the bucket array based on the value's ones digit. For example, 97 is placed in row 7, 3 is placed in row 3, and 100 is placed in row 0. This is called a "distribution pass."

b) Loop through the bucket array row by row, and copy the values back to the original array. This is called a "gathering pass." The new order of the preceding values in the one-dimensional array is 100, 3, and 97.

c) Repeat this process for each subsequent digit position.

I'm no sure if I understand what I'm doing too well, but my code doesn't quite do what I'm trying to do.
Also the error I receive is 0xC0000005 after the program terminates.


#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int sort(int arrNum[], int arrSize)
{
const int max = arrSize;

int bucket[10][max+1];

for(int x = 0;x < 10; x++) bucket[x][max] = 0;

for(int digit = 1; digit <= 10000000; digit *= 10)
{

for(int i = 0; i < max; i++)
{

int digit2 = (arrNum[i] / digit) % 10;

bucket[digit2][bucket[digit2][max]++] = arrNum[i];
}

int j = 0;
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < bucket[x][max]; y++)
{
arrNum[j++] = bucket[x][y];

bucket[x][max] = 0;
}
}
}
return 0;
}

int main()
{
int i;
cout << "Input array size: ";
int size;
cin >> size;
cout << endl;

int* array = new int [size];
delete[] array;

srand((unsigned)time(0));

for(i = 0; i < size; i++)
{
array[i] = (rand()%100)+1;

cout << array[i] << " ";
}

cout << endl << "Sorting Array: " << endl;

for (i = 0; i <= size; i++)
{
sort(array,size);
cout << array[i] << " ";
}

}

This post was edited by sylz1014 on Jan 21 2013 03:42pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 21 2013 07:03pm
i dont see any log statements in there. are you using a debugger? use one or the other.
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Jan 21 2013 07:45pm
You deleted array just after initializing it and then you try and use it? I'm pretty sure that causes 0xc0000005

int* array = new int [size];
delete[] array;

srand((unsigned)time(0));

for(i = 0; i < size; i++)
{
array[i]= (rand()%100)+1;

cout << array[i] << " ";
}

This post was edited by ShadowFiend on Jan 21 2013 07:47pm
Member
Posts: 22,502
Joined: Aug 5 2011
Gold: 0.00
Jan 21 2013 07:49pm
Quote (ShadowFiend @ 21 Jan 2013 21:45)
You deleted array just after initializing it and then you try and use it? I'm pretty sure that causes 0xc0000005

int* array = new int [size];
delete[] array;

srand((unsigned)time(0));

for(i = 0; i < size; i++)
{
array[i]= (rand()%100)+1;


thanks ^_^ that fixed my error.
Although I still need help with my prior issue of, sorting it the way the question says to.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll