sort 2D array

This is a discussion on sort 2D array within the C Programming forums, part of the General Programming Boards category; I would like to sort a 2D array[3][5] of float in descending order based on the last 'cell' e.g. [0][4], ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    13

    sort 2D array

    I would like to sort a 2D array[3][5] of float in descending order based on the last 'cell' e.g. [0][4], [1][4], . . .

    Code:
       . . . 
    scorecard[i][j] = ttl_pts;    // load array
    
    for(u = 1; u < FLIGHT; ++u)
      for(v = FLIGHT; u < v; --v)
        if(scorecard[v-1][4] < scorecard[v][4])
           swap(scorecard[v-1], scorecard[v]);    // designate row? swap
    
    void swap (int * p, int * q)
    {
    	int temp;
    
    	temp = *p;
    	*p = *q;
    	*q = temp;
    }
    This method isn't working. Thanks in advance for any help.
    Keith
    Last edited by astro_not; 04-13-2003 at 01:15 AM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    13

    simplify

    I tried to simplify by avoiding passing to a function :

    Code:
    /*for(u = 1; u < FLIGHT; ++u)
    	for(v = FLIGHT; u < v; --v)
    		if(scorecard[v-1][4] < scorecard[v][4])
    			 
    
    	temp[5] = scorecard[v-1];
    	scorecard[v-1] = scorecard[v];
    	scorecard[v] = temp;*/
    the idea is wrong I suppose.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    13
    Thank you, Salem.
    I see. I have to write to each element separately, rather than attempt to refernce a 'row' or 'set' of elements. Thank you for your help. I think I can work that out.

    After your first comment, I recognized the error of using int *pointers in the swap() that were not matched to the array -- Pointers in multidimensional arrays are over my head at this point. I had mistakenly referred to this simpler swap function and overlooked that I was calling it, without noticing it would not work with an array.
    _________________
    I have been unable to understand the importance of void main() and have not found any mention of it outside of your icon animation (which is great). I notice that main() has changed over the iterations of C. Would you please say more about it if you haven't already elsewhere?

    Keith

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array of threads?
    By shorinhio in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 04:02 PM
  2. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 12:34 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Sorting an array of structures with sort()
    By rmullen3 in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 03:02 PM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 09:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21