Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,158 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,123 people online right now. Registration is fast and FREE... Join Now!




Sorting pointer based array

 
Reply to this topicStart new topic

Sorting pointer based array, I seem to have blanked on this

uberamd
6 Dec, 2007 - 05:49 PM
Post #1

New D.I.C Head
*

Joined: 6 Dec, 2007
Posts: 1


My Contributions
I will start by giving the code:
CODE
    double* tmpArray = new double[(INDEX_MAX / BLOCKS)];
    for(int i = 0; i < BLOCKS; i++)
    {
        for(int k = 0; k < (INDEX_MAX / BLOCKS); k++)
        {
            tmpArray[k] = anArray[count];
            count++;
        }
        int elements = sizeof(tmpArray) / sizeof(tmpArray[0]);
        sort(tmpArray, tmpArray + elements);
        int whatever = 1;
        for(int j = 0; j < (R+1); j++)
        {
            topArray[place] = tmpArray[(INDEX_MAX / BLOCKS) - whatever];
            place++;
            whatever++;
        }
        whatever = 1;
        for(int z = 0; z < (INDEX_MAX / BLOCKS); z++)
        {
            tmpArray[z] = 0; // just zeroing out tmpArray before deleting it
        }
    }
    delete[] tmpArray;

It's not clean, but its a very small project and gets the job done. Well, not yet but it will, using OS X and X-Code to compile the code (without tmpArray being pointer based) the sort() function works fine to sort the array. However, the values INDEX_MAX and BLOCKS are defined when the program starts by using a cin, and Windows does not allow for allocation of arrays after compile time unless it is a pointer based array, so I was forced to switch tmpArray to pointer based.

By doing that, the sort() function broke, and no longer works. I completely blanked on how to sort tmpArray from greatest to least (contains randomly generated numbers between 0.00001 and 1). Could anyone possibly help me out on sorting the pointer based array in descending order. Again, I seem to have blanked.

Thanks in advance!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:27PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month