Reverse The Order of The Elements in A 2D Array
Reverse The Order of The Elements in A 2D Array
#include <stdio.h>
#include <stdlib.h>
#define NUMCOLS 4
#define NUMROWS 8
/* Puzzle D11 -- reverse the order of the elements in a 2D array */
int reverse2D( int x[][NUMCOLS], int nrows )
{
int r, c;
int temp;
/* Swap elements in the full rows of the first half of */
/* the array with elements in full rows of the last half */
for ( r=0; r<nrows/2; r++ )
for ( c=0; c<NUMCOLS; c++ )
{
temp = x[r][c];
x[r][c] = x[nrows-1-r][NUMCOLS-1-c];
x[nrows-1-r][NUMCOLS-1-c] = temp;
}
/* If there are an odd number of rows, deal with the middle row */
if ( nrows%2 == 1 )
{
r = nrows/2;
for ( c=0; c<NUMCOLS/2; c++ )
{
temp = x[r][c];
x[r][c] = x[nrows-1-r][NUMCOLS-1-c];
x[nrows-1-r][NUMCOLS-1-c] = temp;
}
}
Transpose a matrix
#define NUMCOLS 5
int transpose( int x[NUMCOLS][NUMCOLS] )
{
int r, c, temp ;