Quick question
I'm writing a RadixSort class for my Data Structures class, and I ran into a little roadblock:
How do you properly declare a pointer to a dynamic two dimensional array of queue?
I'm having a massive brainfart here :p
EDIT: I solved this by having the pointer point to an array of pointers. Here's my code:
This seems really awkward to me... I'll be using this method in my project, however, any cleaner methods would be appreciated.
EDIT:Okay, I added the code that I actually used. It's pretty sloppy coding that I'm not particularly proud of, however, it's finals week, I have other things to worry about at the moment. Don't worry about SortRoutine, it doesn't do/offer anything.
How do you properly declare a pointer to a dynamic two dimensional array of queue
I'm having a massive brainfart here :p
EDIT: I solved this by having the pointer point to an array of pointers. Here's my code:
Queue <int> ** Test;
Test = new Queue<int> * [5];
for ( int i = 0; i < 5; i++ )
Test[i] = new Queue<int> [5]; This seems really awkward to me... I'll be using this method in my project, however, any cleaner methods would be appreciated.
EDIT:Okay, I added the code that I actually used. It's pretty sloppy coding that I'm not particularly proud of, however, it's finals week, I have other things to worry about at the moment. Don't worry about SortRoutine, it doesn't do/offer anything.
RadixSort.hclass RadixSort : public SortRoutine { public: RadixSort ( int baser , int digitsx ); ~RadixSort (); virtual void sort( int array[], int size ); virtual string name() const; private: void radix( int array[], int size ); int myBase; int myDigits; Queue<int> ** buckets; };RadixSort.cppRadixSort::RadixSort( int baser , int digitsx ) : SortRoutine () { myBase = baser; myDigits = digitsx; buckets = new Queue<int> *[myDigits]; for ( int i = 0 ; i < myDigits ; i++ ) { buckets[i] = new Queue<int> [myBase]; } } RadixSort::~RadixSort( ){ for ( int i = 0 ; i < myDigits ; i++ ) { delete [] buckets[i]; } delete [] buckets; } void RadixSort::sort( int array[], int size ) { setSize( size ); radix( array, size ); } void RadixSort::radix( int array[], int size ) { int modulo = 0; int temptruncator = 0; int tempdivisor = 0; int arrayfiller = 0; for ( int i = 0 ; i < size ; i++ ) { modulo = array[i] % myBase; buckets[0][modulo].enqueue ( array[i] ); } for ( int j = 1 ; j < myDigits ; j++ ) { for ( int k = 0 ; k < myBase ; k++ ) { while ( !( buckets[j-1][k].isEmpty() ) ) { tempdivisor = pow ( myBase , j ); temptruncator = buckets[j-1][k].getFront() / tempdivisor; modulo = temptruncator % myBase; buckets[j][modulo].enqueue ( buckets[j-1][k].getFront() ); buckets[j-1][k].dequeue(); } } } for ( int l = 0 ; l < myBase ; l++ ) { while ( !buckets[myDigits - 1][l].isEmpty()) { array[arrayfiller] = buckets[myDigits - 1][l].getFront(); buckets[myDigits - 1][l].dequeue(); arrayfiller++; } } } string RadixSort::name() const { return( "RadixSort" ); }
