help- swap the first and last column of a two dimensions array

This is a discussion on help- swap the first and last column of a two dimensions array within the C Programming forums, part of the General Programming Boards category; Hello!!! I am new in programming and i think i might need you help. I am trying to swap the ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    7

    Exclamation help- swap the first and last column of a two dimensions array

    Hello!!! I am new in programming and i think i might need you help. I am trying to swap the first column with the last. Can someone help? Here is the code:
    Code:
    for (i=0; i<N; i++)
    	{for(j=0; j<M; j++)
    		{ temp = A[i][1];
    		  A[i][1] = A[i][M];
    		  A[i][M] = temp;}
    	}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The first column is not #1 and the last column is not #M. Also you only need to switch once per row, not for every single element in the array.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,438
    Try A[i][M-1].

    Indexes in C go from 0 to size-1. If your array has M columns in it, valid indexes are 0..M-1.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    Quote Originally Posted by anduril462 View Post
    Try A[i][M-1].

    Indexes in C go from 0 to size-1. If your array has M columns in it, valid indexes are 0..M-1.
    ok i did this
    Code:
    for (i=0; i<N; i++)
    	{for(j=0; j<M; j++)
    		{ temp = A[i][1];
    		  A[i][1] = A[i][M-1];
    		  A[i][M-1] = temp;}
    	}
    but now in the results it swaps the second with the third column(i have a 2x3 array). I need to swap the first with the third...

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by markip View Post
    ok i did this
    Code:
    for (i=0; i<N; i++)
    	{for(j=0; j<M; j++)
    		{ temp = A[i][1];
    		  A[i][1] = A[i][M-1];
    		  A[i][M-1] = temp;}
    	}
    but now in the results it swaps the second with the third column(i have a 2x3 array). I need to swap the first with the third...
    Quote Originally Posted by tabstop View Post
    The first column is not #1 and the last column is not #M. Also you only need to switch once per row, not for every single element in the array.
    I am obligated to type something "new" here. So: "new".

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,438
    I didn't show you, but I stated that the indexes go from 0 to M-1. So your use of 1 for the first column is incorrect. It's index 0.

    tabstop also brought up a good point. Ditch your inner for loop. It's totally useless since you never do anything with j in your swap code.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    ok!!! I DID IT!!! you were right !!! THANK YOU THANK YOU THANK YOU all of you!!

Popular pages Recent additions subscribe to a feed

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