General Statement:

Given the matrix:

17 3 9 6

5 11 2 10

6 4 8 16

Read a command to determine whether a row or a column is to be rotated the indicated direction and number of steps. Rotate a column of values down if the direction is negative or up if it is positive. Rotate a row of values to the left if the direction is negative or to the right if it is positive, Be sure to circle around to the other end when the values “fall off” the matrix.

Input: The first line in the data set is an integer that represents the number of commands that follow. Each command consists of a letter (R for row or C for column) followed by an integer indicating the amount of the rotation.

Output: Output the matrix so that the numbers are right-justified within each column. Output some white space between columns. The output is to be formatted exactly like that for the sample outputgiven below.

Assumptions: If the amount of rotation exceeds the dimension of the matrix, circle around to the other end to complete the rotation. Each new command is to be used to rotate values in the most recent version of the matrix. Do not start over with the original matrix.

Sample Input:

3

R 1 2

R 2 -1

C 3 -5

Sample Output:

9 6 17 3

5 11 2 10

6 4 8 16

9 6 17 3

11 2 10 5

6 4 8 16

9 6 10 3

11 2 8 5

6 4 17 16

Solutions :

#include

#include

int a [3] [4] = {

{17, 3, 9, 6},

{5, 11, 2, 10},

{6, 4, 8, 16}};

void rotation_function (char ch, int number, int rotation)

{

int save;

if ( ch == ‘R’ ) {

if ( rotation < 0 ) {
for ( int i = 0; i < abs(rotation); i++ ) {
save = a [number - 1] [0];
a [number - 1] [0] = a [number - 1] [1];
a [number - 1] [1] = a [number - 1] [2];
a [number - 1] [2] = a [number - 1] [3];
a [number - 1] [3] = save;
}
}
else {
for ( int i = 0; i < abs(rotation); i++ ) {
save = a [number - 1] [3];
a [number - 1] [3] = a [number - 1] [2];
a [number - 1] [2] = a [number - 1] [1];
a [number - 1] [1] = a [number - 1] [0];
a [number - 1] [0] = save;
}
}
}
else {
if ( rotation < 0 ) {
for ( int i = 0; i < abs(rotation); i++ ) {
save = a [0] [number - 1];
a [0] [number - 1] = a [2] [number - 1];
a [2] [number - 1] = a [1] [number - 1];
a [1] [number - 1] = save;
}
}
else {
for ( int i = 0; i < abs(rotation); i++ ) {
save = a [0] [number - 1];
a [0] [number - 1] = a [1] [number - 1];
a [1] [number - 1] = a [2] [number - 1];
a [2] [number - 1] = save;
}
}
}
}
int main ()
{
int dataset;
scanf ("%d", &dataset);
while ( dataset-- ) {
char ch;
int number;
int rotation;
getchar ();
scanf ("%c %d %d", &ch, &number, &rotation);
rotation_function (ch, number, rotation);
for ( int i = 0; i < 3; i++ ) {
for ( int j = 0; j < 4; j++ ) {
if ( j )
printf (" ");
printf ("%2d", a [i] [j]);
}
printf ("\n\n");
}
}
return 0;
}
[/sourcecode]