Thread: sorting a randomly generated dynamic array

  1. #1
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17

    sorting a randomly generated dynamic array

    First off thanks to Prelude for help with the array itself. It was much appreciated. My question is, when you have a randomly generated dynamic array do you have to go about sorting in a different way than has been described by the countless posts and and googles searches I have browesed, because every one I try to incorporate into my code doesnt seem to work, I just need a little direction. Thanks for your time.

    Code:
    
    //------------------------Preprocessor Directives-----------------------------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio.h>
    
    
    //-------------------------FunctioINDEX Prototypes--------------------------------------
     int getstevesort(int *wNum);
     int getdisplayResults(int largest, int smallest);
     int getsmallest(int *wNum);
     int getLargest( int*wNum);
     int getMiddle(int *wNum);
    //----------------------------Function Main----------------------------------------------
    int main(void)
    {
    
    //-----------------------------Variable Declaration----------------------------------
    
    
      int *wNum;
    
    
      int stevesort;
      int largest;
      int smallest;
    
      int i;
      //int middle;
    
      clrscr();
      printf("EC03SRS996\n");
      srand ( (unsigned)time ( NULL ) );
      if ( ( wNum = malloc ( 11 * sizeof *wNum ) ) != NULL )
      printf("Unsorted\n");
        {
        for ( i = 0; i < 11; i++ )
          {
          wNum[i] = ( rand() * 50 )/ RAND_MAX ;
    
    printf(" %d\n",wNum[i]);
         }
       smallest = getsmallest(wNum);
    
    
    
       largest = getLargest(wNum);
    
       getdisplayResults(smallest,largest);
    
       stevesort = getstevesort(wNum);
       printf(" %d", wNum);
    
    
       free ( wNum );
      }
    
    
    
      getch();
      return 0;
    }
    //----------------------------End of Function Main--------------------------------------------
    //-----------------------------userdefinition of getsort--------------------------------------
    int getstevesort(int *wNum)
    {
    int n = 11;
    int counter;
    int pWalk = wNum + 1;
    int pLook = wNum;
    int pLast = &wNum + 10;
    int temp;
    
    
    	for(counter = 0;counter < n; counter++);
       {
       	if(pWalk <= pLook)
             {
          	temp = pLook;
             pLook = pWalk;
             pWalk = temp;
             pWalk++;
             }
    
             	if(pWalk > pLook)
                	{
                    pWalk++;
                   }
                   if(pLast = &pWalk)
                      {
                       pLook++;
                       pWalk = pLook + 1;
                       counter = n - 1;
                      }
       }
    return wNum;
    }
    
    int getdisplayResults(int smallest, int largest)
    {
    printf("The smallest number is %d and the largest number is %d", smallest, largest);
    return getdisplayResults;
    }
    
    
    //-------------------------User definition of getsmallest-------------------------------------
    int getsmallest(int *wNum)
      {
    
      int *pLast;
      int *pSmallest;
      int *pWalk;
    
      pLast =  wNum + 10;
     for (pSmallest = wNum, pWalk = wNum +1; pWalk <= pLast;pWalk++)
     	if (*pWalk < *pSmallest)
          pSmallest = pWalk;
    
     return *pSmallest;
     }
    
    
    //------------------------User definition of getlargest----------------------------------------
    
    int getLargest(int *wNum)
    	{
    
       int *pLargest;
       int *pLast;
        int *pWalk;
         pLast =  wNum + 10;
     for (pLargest = wNum, pWalk = wNum +1; pWalk <= pLast;pWalk++)
     	if (*pWalk > *pLargest)
          {pLargest = pWalk;
           }
     return (*pLargest);
     }
    
    //---------------------User definition of getMiddle---------------------------------------------

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Dunno if this is a good way or not, but you can read about qsort here

    Might help ya a little. Or maybe someone will suggest a better way.....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I recommend brushing up on pointer notation a bit before encorporating it into a larger program. When working out a problem it helps a great deal to trim the fat and only work with the function that you want to fix. I ended up rewriting the sort function since it made no sense (sorry), and removing the unnecessaries as well as cleaning up the output so that it is easier to understand. This should work a bit better for you and you can plug in the other functions:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    void getstevesort(int *wNum, int size);
    
    int main(void)
    {
      int *wNum;
      int i;
    
      srand ( (unsigned)time ( NULL ) );
      printf("Unsorted\n");
      if ( ( wNum = malloc ( 11 * sizeof *wNum ) ) != NULL ) {
        for ( i = 0; i < 11; i++ ) {
          wNum[i] = ( rand() * 50 ) / RAND_MAX;
          printf(" %d",wNum[i]);
        }
        getstevesort(wNum, 11);
        printf ( "\nSorted\n" );
        for ( i = 0; i < 11; i++ )
          printf(" %d", wNum[i]);
        free ( wNum );
      }
      getchar();
      return 0;
    }
    
    void getstevesort(int *wNum, int size)
    {
      int x, y, temp;
      /* Simple insertion sort */
      for ( x = 1; x < size; x++ )
        for ( y = x; y > 0; y-- )
          if ( wNum[y-1] > wNum[y] ) {
            temp = wNum[y-1];
            wNum[y-1] = wNum[y];
            wNum[y] = temp;
          }
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17

    Thanks Prelude

    Thanks again for your help, after reading countless threads in the many searches I have conducted on this board since the end of August, I just have to say that you are truly an asset. There are too many people on this board who obviously know what they are talking about, but they would rather spend time insulting people or giving them bogus code. Anyway enough of the rant, I just wanted to say thanks.

    As far as that really sad sort function went, I just wrote down 11 random numbers and sorted them manually and tried to incorporate what I did into code, which I am obviously not that competent at yet, but I am trying. My instructor has stated numerous times that good programers usually go for a pad and pencil when they have to write code. I dont know if that is true in all cases but for me it seems to work, just need to work on the transfering from the pad to the screen.

    Last question, I promise, new subject actually. I need to find some info on parallel arrays. As far as I can tell they are just two single dimension arrays, where array 1 contains data relevent to the array 2. I literally only found 1 sentence in our book, "Parallel arrays are two or more arrays that contain related data." The book is titled "Computer Science A structured Programming Approach Using 'C'. Written by Forouzan and Gilberg. Is it best to use an array of pointers to manipulate the elements or is there some special functions for parallel arrays. If you could just point me in a direction that would be great.

    I feel a little like a dolt, because I post some scary code, but I do search all the obvious places before I post, (ie programming FAQ, google, other programming boards. etc...) I just dont always get what I am reading. Anyway I am coming up for air, thanks again for your time Prelude. It is greatly appreciated.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >My instructor has stated numerous times that good programers
    >usually go for a pad and pencil when they have to write code.
    This is true for me and those I work with. Once you break down the program into small functions, it's very easy to take a notepad and get the logic right before you start coding. It's nice to get a program to run properly on paper without having to worry about the syntax of the language. When you actually sit down to start typing you know that the program is solid and all you have to do is clean up the language bugs.

    >If you could just point me in a direction that would be great
    Well, you're description is accurate, but in implementation a two dimensional array or an array of structures with two members is more common since it stresses the relationship between the data more readily than two arrays.

    >Is it best to use an array of pointers to manipulate the elements
    It depends on what you're going to do and how much larger the elements are than pointers. If you plan on doing a lot of sorting for example, and all of the elements are quite large then an array of pointers would be a good idea to improve efficiency.

    >I just dont always get what I am reading.
    You'll get it with practice, no one starts out as an expert.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  2. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. dynamic array of vectors
    By axr0284 in forum C++ Programming
    Replies: 8
    Last Post: 02-26-2006, 12:01 AM
  5. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM