Thread: Passing an array of Struct to a sorting function

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    74

    Passing an array of Struct to a sorting function

    how is this done not really sure .

    Code:
     
    //prototype
    
    void improvedbubblesort (struct team Table[]);
    
    //this is defined out side of main 
    struct team 
    {
        char name[20] ; 
        int points ; 
        int goalsscored ; 
        int goalsconceeded ; 
    };
    
    int main(void)
    {
            //how array is defined and initilize in main 
            struct team Table[20]={{{{0}}}} ;
    
            //how function is called
            improvedbubblesort(Table) ; 
    
    
         return 0 ; 
    }
    
    //implementation of function 
    
    void improvedbubblesort(struct  team Table[])
    {
        int i ; 
        int No_Els = 10 ; 
        int pointsTemp ; 
        int goalsScoredtemp ; 
        int goalsConcTemp ;
        char name[25];
        bool No_swaps ; 
        
        for( i = 0 ; i < No_Els - 1 ; i ++ )
        {
            //set bool to true  assume No swaps to be made 
            
            for( int j = 0 ; j < No_Els - i - 1  ; j ++ )
            {
               
                
                No_swaps = true ; 
                
                if(Table[j].points > Table[j+1].points )
                {
                    //swap name
                    strcpy(name,Table[j].name) ;
                    strcpy(Table[j].name,Table[j+1].name) ;
                    strcpy(Table[j+1].name,name) ;
                    //swap points
                    pointsTemp = Table[j].points ; 
                    Table[j].points = Table[j+1].points ;
                    Table[j+1].points = pointsTemp ; 
                    //swap goalsscored
                    goalsScoredtemp = Table[j].goalsscored ; 
                    Table[j].goalsscored= Table[j+1].goalsscored ;
                    Table[j+1].goalsscored = goalsScoredtemp ;
                    //swap goalssconceeded
                    goalsConcTemp = Table[j].goalsconceeded ; 
                    Table[j].goalsconceeded = Table[j+1].goalsconceeded ;
                    Table[j+1].goalsconceeded = goalsConcTemp ;
                    //****swaps made change bool****//
                    No_swaps = false ; 
                }//****end if****// 
                    
            }//end for
                
            if(No_swaps == true )
            {
                printf("noswaps made \n " ) ; 
                getchar() ; 
                break ; 
            }
            
        }//end for
    }
    the whole code is very long but can post if necessary .
    Basically i get no errors in this but its not sorting them .
    Think its in the way i pass the array of struct .
    any help would be good.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your test input, expected output and actual output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    Quote Originally Posted by laserlight View Post
    What is your test input, expected output and actual output?
    got it had a sign the wrong way in the sorting so it was sorting them the wrong way

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    btw you can swap the structures referenced as array elements (using a temporary struct) - instead of swapping the structure fields like this.

    Should result in much simpler code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-05-2012, 10:16 PM
  2. Replies: 8
    Last Post: 05-10-2012, 12:07 PM
  3. Replies: 1
    Last Post: 05-30-2009, 11:04 PM
  4. Passing array of struct as a function parameter
    By desmond5 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 11:32 AM
  5. Replies: 2
    Last Post: 03-07-2002, 10:14 AM