Rotate the Grid

I’m not immersed in higher mathematics, therefore most of my programming puzzles tend to involve strings or other items I can easily comprehend. Beyond manipulating characters and strings, for my programming kung fu training I keep returning to the matrix or grid.

A grid is a two-dimensional array in C, a set of rows and columns defined as:

int grid[8][8];

The two dimensions are specified in brackets after the data type and variable name. They need not be equal, though in my mind equal dimensions define the difference between a grid/matrix and what I’d call a table.

The two-dimensional, double-bracket notation is merely for your reference. Internally, the grid’s data is stored in a linear fashion. So the same array can be declared as:

int grid[64];

To establish grid[] as a matrix and not a single file parade of 64 integers, you code the overhead, ensuring that data is manipulated and output in tidy rows and columns.

For this month’s Exercise, your task is to create a two character grids of the same size, 5-by-5. The first grid is to be initialized randomly, filled with characters ranging from 'a' to 'z'. The second grid contains the same characters as the first, but it’s rotated 90 degrees clockwise. To wit:

 j x y i r
 e n y f h
 i o o i h
 i d k l u
 g x c q b
 g i i e j
 x d o n x
 c k o y y
 q l i f i
 b u h h r

In order, your tasks are:

  1. Fill the grid/matrix with random letters, a to z
  2. Output the original matrix
  3. Fill a second grid/matrix with values from the first, but rotated 90° clockwise
  4. Output the second, rotated matrix

Please try this Exercise on your own before you peek at my solution.

2 thoughts on “Rotate the Grid

Leave a Reply