qsort with multi-dimensional arrays

This is a discussion on qsort with multi-dimensional arrays within the C Programming forums, part of the General Programming Boards category; Hi, I need some help with the syntax on this. If I have an array d[1000][4] - how do I ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    3

    qsort with multi-dimensional arrays

    Hi,

    I need some help with the syntax on this. If I have an array
    d[1000][4] - how do I specify that I want the qsort funtion to sort on the 4th column?

    I am currently running this code with a bubbe sort, but I thought it would run faster with qsort.

    Thanks.

    Diane

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Do you want to sort the 4th column only or sort the rows based off the value of the 4th column?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    The real trick is mastering how to write the compare function
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 10
    void print ( int a[][4] ) {
        int r,c;
        for ( r = 0 ; r < MAX ; r++ ) {
            for ( c = 0 ; c < 4 ; c++ ) {
                printf( "%d ", a[r][c] );
            }
            printf( "\n" );
        }
    }
    
    int cmp ( const void *pa, const void *pb ) {
        const int (*a)[4] = pa;
        const int (*b)[4] = pb;
        if ( (*a)[3] < (*b)[3] ) return -1;
        if ( (*a)[3] > (*b)[3] ) return +1;
        return 0;
    }
    
    int main ( ) {
        int a[MAX][4];
        int r,c;
        for ( r = 0 ; r < MAX ; r++ ) {
            for ( c = 0 ; c < 4 ; c++ ) {
                a[r][c] = rand() % 100;
            }
        }
        qsort( a, MAX, sizeof a[0], cmp );
        print( a );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    Quote Originally Posted by Thantos
    Do you want to sort the 4th column only or sort the rows based off the value of the 4th column?
    I want to sort the all of the array - based on the 4th column.

    Thanks.

    Diane

  5. #5
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    use the follwing program
    #include"stdio.h"
    #define c 1000
    #define l 4
    void main()

    {
    for(i=0;i<c;i++)
    {for(j=0;j<l;j++)
    {
    printf("t[%d][%d]="i,j);
    scanf("%d",&t[i][j]);
    }
    for(i=0;i<c;i++)
    {for(j=0;j<l;j++)
    {....
    you want to sort the row or the colums

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I want to sort the all of the array - based on the 4th column.
    Then Salem's already given you the answer

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    Thanks Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  2. fscanf for multi dimensional arrays
    By rambos in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 03:26 AM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. Pointers to multi dimensional arrays.
    By bartybasher in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2003, 02:41 PM

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