2. Save your array2d3.c program from the previous problem as array2d3-better.c.
ID: 3543268 • Letter: 2
Question
2. Save your array2d3.c program from the previous problem as array2d3-better.c. Edit array2d3-better.c so that the definitions of the four matrix-handling functions conform to the following prototypes:
ra_2d display2d( ra_2d a, size_t nrows, size_t ncols, char *caption );
ra_2d hflip( ra_2d a, size_t nrows, size_t ncols );
ra_2d vflip( ra_2d a, size_t nrows, size_t ncols );
ra_2d rotate( ra_2d a, size_t n );
You must define ra_2d to stand for the type "pointer to array of MAXCOLS ints". The return value for each of the above functions is simply its first parameter. Note that a fourth parameter, caption, of string type (that is, of char * type), has been added for display2d; your definition of this function should display this parameter value before displaying the matrix parameter a.
Returning the matrix parameter allows users of these functions to chain a series of matrix operations in a single nested function call expression. For example, the nested call:
display2d( rotate( t, MAXROWS ), MAXROWS, MAXCOLS, "After rotating 90 degrees clockwise: " )
will rotate the numbers contained in the square matrix a (remember, we're assuming MAXROWS = MAXCOLS) then display the caption "After rotating 90 degrees clockwise:" on a line by itself, followed by a display of the rotated matrix.
Here is array2d3.c
#include <stdio.h>
#include <stddef.h> /* for size_t type definition */
#define MAXROWS 3
#define MAXCOLS 3
/* function prototypes */
void display2d( int a[][MAXCOLS], size_t nrows, size_t ncols );
void hflip( int a[][MAXCOLS], size_t nrows, size_t ncols );
void vflip( int a[][MAXCOLS], size_t nrows, size_t ncols );
void rotate( int a[][MAXCOLS], size_t n );
int main( void )
{
int t[MAXROWS][MAXCOLS] = { {1, 2, 3}, {4, 5, 6}, {7,8,9} };
display2d( t, MAXROWS, MAXCOLS );
printf( " " );
vflip( t, MAXROWS, MAXCOLS );
display2d( t, MAXROWS, MAXCOLS );
printf( " " );
hflip( t, MAXROWS, MAXCOLS );
display2d( t, MAXROWS, MAXCOLS );
printf( " " );
vflip( t, MAXROWS, MAXCOLS );
display2d( t, MAXROWS, MAXCOLS );
printf( " " );
hflip( t, MAXROWS, MAXCOLS );
display2d( t, MAXROWS, MAXCOLS );
printf( " " );
rotate(t,MAXCOLS);
display2d( t, MAXROWS, MAXCOLS );
return 0;
}
void display2d( int a[][MAXCOLS], size_t nrows, size_t ncols )
{
size_t i, j; /* loop counters */
for ( i = 0; i < nrows; ++i )
{
for ( j = 0; j < ncols; ++j )
printf( "%12i", a[i][j] );
printf( " " );
}
return;
}
void hflip( int a[][MAXCOLS], size_t nrows, size_t ncols )
{
size_t i, j; /* loop counters */
size_t midcol = ncols / 2; /* "middle" column */
int t;
for ( i = 0; i < nrows; ++i )
for ( j = 0; j < midcol; ++j )
{
t = a[i][j];
a[i][j] = a[i][ncols-(j+1)];
a[i][ncols-(j+1)] = t;
}
return;
}
void vflip( int a[][MAXCOLS], size_t nrows, size_t ncols )
{
size_t i, j; /* loop counters */
size_t midrow = nrows / 2; /* "middle" row */
int t;
for ( i = 0; i < midrow; ++i )
for ( j = 0; j < ncols; ++j )
{
t = a[i][j];
a[i][j] = a[nrows-(i+1)][j];
a[nrows-(i+1)][j] = t;
}
return;
}
void rotate( int a[][MAXCOLS], size_t n )
{
size_t i,j; /* loop counters */
int t;
for(i=0;i<MAXCOLS;i++)
for(j=i;j<MAXCOLS;j++)
{
t=a[i][j];
a[i][j] = a[j][i];
a[j][i] = t;
}
ra_2dp( a, MAXCOLS, MAXCOLS );
return;
}
Explanation / Answer
include the stdlib library
#include<stdio.h
//the prototypes
int** display2d( int**, size_t nrows, size_t ncols, char *caption );
int** hflip( int**, size_t nrows, size_t ncols );
int** vflip( int**, size_t nrows, size_t ncols );
int** rotate( int**, size_t n );
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.