Thread: swapping function 2 dimensional arrays

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    35

    swapping function 2 dimensional arrays

    Hello,

    I am working on a swap function with 2 dimensional arrays. I have it working if I write it as an If statement but cannot get it to work as a function.

    It should basically initialize the array with the square root of whatever you give d and then print the array to the screen so a 4*4 array should print out as

    15 14 13 12
    11 10 9 8
    7 6 5 4
    3 2 1 0

    The swap function should swap the 2 and the 1 so it should print out as
    15 14 13 12
    11 10 9 8
    7 6 5 4
    3 1 2 0


    I tried to use pointers because I believe that would be best but I removed them because I could not figure out how to pass a two dimensional array that I needed to perform math on as a pointer to the function.

    Attempting to use it as a function call.
    Code:
    void
    init(void)
    {
        int row,col,num=d*d,remain=d%2;
        // Initalize the board with a two dimensional array.
    
        for(row=0;row<d;row++) {
            for(col=0;col<d;col++) {
            num-=1; /* Start subtracting 1 from d*d Example 16-1=15 */
            board[row][col] = num;
            }
        }
        if(remain==0) {
            swap(board[d-1][d-2],board[d-1][d-3]);
            }
    }
    
    /* 
     * Prints the board in its current state.
     */
    
    void
    draw(void)
    {
        // Draw the board based upon the argument provided running the game.
        int row,col;
        for(row=0;row<d;row++) {
            printf("\n");
            for(col=0;col<d;col++) {
              printf("%3d",board[row][col]);
            }
        }
        printf("\n");
    }
    
    
    /* I know this function doesn't do anything.  I cannot figure out how to use pointers so that it changes the values of the address of the array element being passed to it. */
    void
    swap(int a, int b) {
        int temp;
        temp = a;
        a = b;
        b = temp;
        temp = 0;    
        draw();   
    }
    Any help would be great.

    Thanks,

    Wayne

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Did you try using pointers at all? It shouldn't matter that you want to swap two elements in a matrix, the actual swapping part doesn't change because of how you use it.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    35
    I have tried them however I only have a basic conception of pointers and was not able to get it to work. It kept telling me that I could send an integer argument without casting it as a pointer.


    Excerpts from above
    I tried to use pointers because I believe that would be best but I removed them because I could not figure out how to pass a two dimensional array that I needed to perform math on as a pointer to the function.

    /* I know this function doesn't do anything. I cannot figure out how to use pointers so that it changes the values of the address of the array element being passed to it. */

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well a proper swap needs pointers as arguments so put the pointers back. You cannot safely swap two things by value (although it is possible, it is also pretty dumb).
    Then you rewrite swap to swap two things using pointers.
    Then:
    Code:
    swap(&board[d-1][d-2], &board[d-1][d-3]);
    This, because the value of a pointer is a memory address, as you should recall from your studying. Before, you were only passing in the values of the elements you wanted to swap.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    35
    It gives me the errors when I just do that. I have tried the following:

    gcc -ggdb -std=c99 -Wall -Werror -o fifteen fifteen.c -lcs50 -lm
    fifteen.c: In function 'init':
    fifteen.c:152:9: error: passing argument 1 of 'swap' makes integer from pointer without a cast [-Werror]
    fifteen.c:45:6: note: expected 'int' but argument is of type 'int *'
    fifteen.c:152:9: error: passing argument 2 of 'swap' makes integer from pointer without a cast [-Werror]
    fifteen.c:45:6: note: expected 'int' but argument is of type 'int *'
    fifteen.c: At top level:
    fifteen.c:200:1: error: conflicting types for 'swap'
    fifteen.c:45:6: note: previous declaration of 'swap' was here
    cc1: all warnings being treated as errors

    Code:
    void
    init(void)
    {
        int row,col,num=d*d,remain=d%2;
        // Initalize the board with a two dimensional array.
    
        for(row=0;row<d;row++) {
            for(col=0;col<d;col++) {
            num-=1; /* Start subtracting 1 from d*d Example 16-1=15 */
            board[row][col] = num;
            }
        }
        if(remain==0) {
            swap(&board[d-1][d-2],&board[d-1][d-3]);
            }
    }
    
    void
    swap(int* pa, int* pb) {
        int* ptemp;
        ptemp = pa;
        pa = pb;
        pb = ptemp;
        //ptemp = 0;    
        draw();   
    }

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm more concerned with top-level errors because the code is largely correct.
    fifteen.c:200:1: error: conflicting types for 'swap'
    fifteen.c:45:6: note: previous declaration of 'swap' was here
    You only need one declaration for swap, that makes it a function that accepts two pointers. You probably forgot to update the swap function prototype when you changed how it works.

    Also I don't think your swap is correct. Normally you have to dereference both pointers repeatedly to make a swap.
    Last edited by whiteflags; 06-17-2012 at 07:14 PM.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    35
    You are exactly correct on two counts. I did forget to update the prototype. I had never seen that error before. My swap also did not work. I don't know what you mean by dereference. Any possibility of an example? Thank you for the help by the way.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by scrfix View Post
    My swap also did not work. I don't know what you mean by dereference. Any possibility of an example? Thank you for the help by the way.
    Code:
    void swap(int *pa, int *pb)
    {
       int hold = *pa; /* the operator * dereferences pointers */
       *pa = *pb;
       *pb = hold;
    }
    Like that, note the differences in my code. The object being pointed to is retrieved when you dereference a pointer.

    You are welcome.
    Last edited by whiteflags; 06-17-2012 at 09:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping elements in Arrays
    By november1992 in forum C Programming
    Replies: 11
    Last Post: 03-10-2012, 01:58 AM
  2. Swapping values in two dimensional arrays help needed
    By A34Chris in forum C Programming
    Replies: 6
    Last Post: 04-20-2011, 03:28 AM
  3. Help with two-dimensional arrays and function
    By edesign in forum C Programming
    Replies: 3
    Last Post: 12-21-2009, 10:29 AM
  4. Help swapping 2 arrays
    By Boboki in forum C Programming
    Replies: 8
    Last Post: 03-03-2005, 06:07 AM
  5. Swapping Pointers & Arrays
    By bartybasher in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2003, 02:17 PM