Matrix Sorting using Pointer

This is a discussion on Matrix Sorting using Pointer within the C Programming forums, part of the General Programming Boards category; I tried to use pointer arithmetic to sort a 2D matrix. The Code I've written was: Code: #include<stdio.h> #include<stdlib.h> main() ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Matrix Sorting using Pointer

    I tried to use pointer arithmetic to sort a 2D matrix. The Code I've written was:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    main()
    {
        int a[3][4], i, j, k, l, temp;
    
        for(i=0; i<3; i++)
        {
            for(j=0; j<4; j++)
            {
                scanf("%d", (a+i*4+j));
            }
        }
    
    
        for(i=0; i<3; i++)
        {
            for(j=0; j<4; j++)
            {
                for(k=0; k<3; k++)
                {
                    for(l=0; l<4; l++)
                    {
                        if(*(a+i*4+j)<*(a+k*4+l))
                        {
                            temp = *(a+i*4+j);
                            *(a+i*4+j) = *(a+k*4+l);
                            *(a+k*4+l) = temp;
                        }
                    }
                }
            }
        }
    
    
        for(i=0; i<3; i++)
        {
            for(j=0; j<4; j++)
            {
                printf("%d\t", *(a+i*4+j));
            }
            printf("\n");
        }
    
        return 0;
    }
    The compiler creates three message:
    Line 27: Warning: Assignment makes int to pointer without a cast.
    line 28: Incompatible types when assigning to type to type 'int[4]' from type 'int *'
    line 29: Incompatible types when assigning to type to type 'int[4]' from type 'int'

    I'm new to the whole programming thing. And I can not figure out what is wrong with my program as far my knowledge goes this should not be a problem. Help please.
    Thanks in advance. And explain in detail.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    I suggest getting the sort to work without using "pointer arithmetic"; then you can modify the code if needed back to using "pointer arithmetic".

    Tim S.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    I have already done it without pointer arithmetic. here is it:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    main()
    {
        int a[3][4], i, j, k, l, temp;
    
        for(i=0; i<3; i++)
        {
            for(j=0; j<4; j++)
            {
                scanf("%d", &a[i][j]);
            }
        }
    
    
        for(i=0; i<3; i++)
        {
            for(j=0; j<4; j++)
            {
                for(k=0; k<3; k++)
                {
                    for(l=0; l<4; l++)
                    {
                        if(a[i][j]<a[k][l])
                        {
                            temp = a[i][j];
                            a[i][j] = a[k][l];
                            a[k][l] = temp;
                        }
                    }
                }
            }
        }
    
    
        for(i=0; i<3; i++)
        {
            for(j=0; j<4; j++)
            {
                printf("%d\t", a[i][j]);
            }
            printf("\n");
        }
    
        return 0;
    }
    this works just fine.
    Now i want to do it using pointers.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    If you're going to use pointers like that - calculating your own row/column offset, then the array should be defined one dimensional such as a[12].

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    4
    As the array is stored in a continuous memory block you can say that. Actually for a 1D array, I have sorted it using the pointer arithmetic.
    However for a a[3][4] matrix to access the a[2][3] element the pointer notation is *(a+2*4+3); I wanted to use this to do the work. But not working.
    But i need this block to work:
    temp = *(a+i*4+j);
    *(a+i*4+j) = *(a+k*4+l);
    *(a+k*4+l) = temp;

    i wonder what is wrong with it.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    But you're not really using pointer arithmetic properly here.

    Your approach is akin to array flattening, but that only works if your initial pointer is a pointer to the first element. The array name (for a 2D array) is not a pointer to the first element, but a pointer to the first row. When you do the addition, the effective sizeof for each array element pushes you way past the end of the array.

    Besides, you should NOT have all those literal sizes in there.

    This is your equivalent, for the print statement.
    printf("%d\t", *(*(a+i)+j) );
    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.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Yes, you need to work on the sorting algorithm part.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    4
    Thanks Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 09:40 AM
  2. [C] sorting a matrix of struct
    By spaistik in forum C Programming
    Replies: 5
    Last Post: 05-24-2010, 09:18 AM
  3. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  4. sorting using pointer to pointer not working
    By eager2no in forum C Programming
    Replies: 17
    Last Post: 09-21-2008, 12:52 AM
  5. Sorting a 3-d matrix
    By Glauber in forum C++ Programming
    Replies: 15
    Last Post: 05-27-2008, 04:17 AM

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