Thread: Confused...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Question Confused...

    Trying to write a code so that I'm not changing the original array. Just sorting in ascending order and then in descending order. I have lost my train of thought and am now stuck.

    Could some body help walk me throught this. Need help please.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 5
    
    //Function Declarations
    int* getData (int* pAry, int arySize);
    void bubbleSortAscend (int* pAry, int* last);
    void bubbleSortDescend (int* pAry, int* last);
    void printData (int* pAry, int* last);
    int* smallest (int* pAry, int* pLast);
    void exchange (int* current, int* smallest);
    
    int main (void)
    {
    	// Local Declarations
    	   int ary[SIZE];
    	   int* pLast;
    
       // Statements
    	   pLast = getData (ary, SIZE);
    	   bubbleSortAscend (ary, pLast);
    	   bubbleSortDescend (ary, pLast);
    	   printData (ary, pLast);
    	   
    	   system("PAUSE");
    
    	   return 0;
    }//main
    /*============================getData=======================================
      Reads data from keyboard into array for sorting.
    	Pre  pAry is pointer to array to be filled 
    		 arySize is integer with maximum array size
        Post array filled. Returns address of last element
    */
    int* getData (int* pAry, int arySize)
    {
    	//Local Declarations
    	  int ioResult;
    	  int readCnt = 0;
    	  int* pFill = pAry;
    
    	// Statements
    	  do
    		{
    			printf ("Please enter number of <EOF>: ");
    			ioResult = scanf ("%d", pFill);
    			if (ioResult == 1)
    				{
    					pFill++;
    					readCnt++;
    				}//if
    		}while (ioResult == 1 && readCnt < arySize);
    
    	 printf ("\n\n%d numbers read.", readCnt);
    	 return (--pFill);
    } // getData
    /*=========================bubbleSortAscend===============================
    	Sorts a list using bubble sort. Adjacent elements are compared and
    	exchanged until list is ordered.
    		Pre the list must contain contain at least one item
    		    last contains index to last element to list
    		Post list rearranged in sequence low to high
    */
    void bubbleSort(int* pAry, int* last)
    {
    	//Local Declarations
    	  int temp;
    
       //Statements
    	  //Outer loop
    	  for (int current = 0; current < *last; current++)
    		{
    			//Inner Loop: Bubble up one element each pass
    			for (int walker = *last;
    				walker > current;
    				walker--)
    			  if (list[walker] < list[walker-1])
    				{
    					temp = list[walker];
    					list[walker] = list[walker - 1];
    					list[walker - 1] = temp;
    			    }//if
    	    }// for current
    	  
    	  return;
    } // bubbleSortAscend
    /*=============================bubbleSortDescend===============================
    
    
    */
    void bubbleSortDescend(int* pAry, int* last)
    {
    	//Local Declarations
    	  int temp2;
    
       //Statements
    	  //Outer loop
    	  for (int current = 0; current < *last; current++)
    		{
    			//Inner Loop: Bubble up one element each pass
    			for (int walker = *last;
    				walker > current;
    				walker--)
    			  if (list[walker] < list[walker-1])
    				{
    					temp2 = list[walker];
    					list[walker] = list[walker - 1];
    					list[walker - 1] = temp2;
    			    }//if
    	    }// for current
    	  
    	  return;
    } // bubbleSortDescend
    I appreciate any help. Thank you!!!!

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    An easy way of doing bubble sort.

    Code:
        for ( a = 0; a < size - 1; a++ )
        {
            for ( b = a + 1; b < size; b++ )
    	{
    	    if ( array[a] > array[b] )
                {
    		temp = array[a];
    		array[a] = array[b];
    		array[b] = temp;
    	    }
    	}
        }
    All you need to change to make it from ascending to descending is the > inside the if; to <
    Last edited by Aia; 12-06-2007 at 09:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  2. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  3. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 05:41 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM