Rotate the Grid – Solution

The challenge for this month’s Exercise is to fill a grid with random letters, a to z, then rotate that grid on its side, counterclockwise, and display the results. Such fun.

Well, I think it’s fun because this is the exact type of programming puzzle I’ve enjoyed since toying with the BASIC language centuries ago. The delightful part is figuring out the code that plucks a value from a position in the original matrix and sets that value in the proper, rotated position in the second. Once you get the equation down, the program runs and data flows to work out the solution. It’s like magic!

Here’s the main() function for my solution, which breaks down the four steps as presented in the Exercise’s original post:

int main()
{
    char matrix[SIZE*SIZE];
    char matrix_r[SIZE*SIZE];

    /* seed randomizer */
    srand( (unsigned)time(NULL) );
    
    fill_matrix(matrix);
    puts("Original:");
    show_matrix(matrix);
    rotate_matrix(matrix,matrix_r);
    puts("Rotated:");
    show_matrix(matrix_r);

    return(0);
}

The original grid is matrix[], the rotated grid is matrix_r[].

The fill_matrix() function initializes the original matrix with random characters, a to z.

The show_matrix() function outputs the original matrix.

The rotate_matrix() function is the workhorse, transferring the characters from one grid to the other, rotating the results.

Finally, the show_matrix() function is called again, but with matrix_r as its argument.

Because both arrays are expressed in single dimensions, they can be passed to the functions name-only.

Here’s the rotate_matrix() function:

void rotate_matrix(char a[], char b[])
{
    int row_a,row_b,col_a,col_b;
    
    for(row_a=0,col_b=SIZE-1; row_a<SIZE; row_a++,col_b--)
    {
        for(col_a=0,row_b=0; col_a<SIZE; col_a++,row_b++)
        {
            b[(row_b*SIZE)+col_b] = a[(row_a*SIZE)+col_a];
        }
    }
}

The original matrix is processed rows, then columns. The rotated matrix is processed columns, then rows. You see this effect with the multiple arguments in each for loop.

The outer loop is initialized row_a=0,col_b=SIZE-1 and each turn alters the variables by incrementing row_a and decrementing col_b. This is the rotation method, which effectively copies a row from one matrix into a column for the next.

The inner loop doesn’t need to rotate anything; it processes columns and rows in sequence, initializing both variables col_a and row_b to zero and incrementing both each turn. This loop’s statement copies the values:

b[(row_b*SIZE)+col_b] = a[(row_a*SIZE)+col_a];

The *SIZE math is necessary to calculate the proper offset in the grid.

Here’s sample output:

Original:
 u k b x v
 a s t y n
 m i c w q
 k b l u x
 v r j m l
Rotated:
 v k m a u
 r b i s k
 j l c t b
 m u w y x
 l x q n v

Click here to view my solution on github. Alas, I can’t upload C code to the blog any longer, which disappoints me, but encourages me to post more stuff on github.

I hope you devised a clever solution and were able to solve the problem. For further skills, consider writing code that rotates the grid 180° or 270°. Think of the fun you’ll have!

Leave a Reply