My function prototype is
int** rotate(int **arr, int row, int col, int fl);
where arr
is the two dimensional array, row<
If your compiler supports variable length arrays then the function declaration can look the following way
void rotate( size_t row, size_t col, int arr[][col], int fl);
or
void rotate( size_t row, size_t col, int arr[][col], _Bool fl);
In this case you can use arrays with different sizes.
Here is a demonstrative program
#include
void rotate( size_t row, size_t col, int a[][col], _Bool fl )
{
for ( size_t i = 0; i < ( fl ? row : col ); i++ )
{
for ( size_t j = 0; j < ( fl ? col : row ); j++ )
{
printf( "%d ", a[fl ? i : j][fl ? j : i] );
}
putchar( '\n' );
}
}
#define N1 3
int main(void)
{
int a[][3] =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
rotate( sizeof( a ) / sizeof( *a ), N1, a, 0 );
putchar( '\n' );
rotate( sizeof( a ) / sizeof( *a ), N1, a, 1 );
putchar( '\n' );
return 0;
}
Its output is
1 4
2 5
3 6
1 2 3
4 5 6
Otherwise if within the function you are going to create new arrays then the function can look as it is shown in the following demonstrative program.
#include
#include
int ** rotate( size_t, size_t, int a[][*], _Bool fl );
int ** rotate( size_t row, size_t col, int a[][col], _Bool fl )
{
int **p = malloc( col * sizeof( int * ) );
for ( size_t i = 0; i < col; i++ )
{
p[i] = ( int * )malloc( row * sizeof( int ) );
}
if ( fl )
{
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
p[col - j - 1][i] = a[i][j];
}
}
}
else
{
for ( size_t i = 0; i < row; i++ )
{
for ( size_t j = 0; j < col; j++ )
{
p[j][i] = a[row - i - 1][j];
}
}
}
return p;
}
#define M 2
#define N 3
int main(void)
{
int a[M][N] =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
int **p = rotate( M, N, a, 0 );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ )
{
printf( "%d ", p[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
for ( size_t i = 0; i < N; i++ )
{
free( p[i] );
}
free( p );
p = rotate( M, N, a, 1 );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ )
{
printf( "%d ", p[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
for ( size_t i = 0; i < N; i++ )
{
free( p[i] );
}
free( p );
return 0;
}
Its output is
4 1
5 2
6 3
3 6
2 5
1 4