C Program to Rotate Matrix Elements
C Program to Rotate Matrix Elements
A ring of classroom table values should move one perimeter position clockwise while its center stays where it was.
A perimeter rotation moves values along an explicitly ordered boundary ring; it is different from automatically rotating every table by ninety degrees.
Before this exercise, review array indexes and bounds and nested loops. The worked program below applies those ideas to this specific problem.
Explain
Explain why rotate matrix perimeter uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Apply two one-position rotations and compare with the first result.
Rotate Matrix Perimeter: follow the data
Given data
A ring of classroom table values should move one perimeter position clockwise while its center stays where it was.
Rule
A perimeter rotation moves values along an explicitly ordered boundary ring; it is different from automatically rotating every table by ninety degrees.
Observed result
4 1 2 7 5 3 8 9 6
Understand the operation
The positions array lists eight boundary coordinates clockwise. Save the final ring value, shift from the end toward the start, then put the saved value at the first coordinate.
The center coordinate (1,1) is absent from the ring and is never assigned. A general large matrix needs a rule for each layer, including single-row and single-column remainders.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int table[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int positions[][2] = {{0,0},{0,1},{0,2},{1,2},{2,2},{2,1},{2,0},{1,0}};
int saved = table[1][0];
for (int i = 7; i > 0; i--) table[positions[i][0]][positions[i][1]] = table[positions[i-1][0]][positions[i-1][1]];
table[0][0] = saved;
for (int row = 0; row < 3; row++) for (int col = 0; col < 3; col++) printf("%d%s", table[row][col], col == 2 ? "\n" : " ");
return 0;
}
Expected output
4 1 2
7 5 3
8 9 6
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int table[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int positions[][2] = {{0,0},{0,1},{0,2},{1,2},{2,2},{2,1},{2,0},{1,0}};
int saved = table[1][0];
for (int i = 7; i > 0; i--) table[positions[i][0]][positions[i][1]] = table[positions[i-1][0]][positions[i-1][1]];
table[0][0] = saved;
for (int row = 0; row < 3; row++) for (int col = 0; col < 3; col++) printf("%d%s", table[row][col], col == 2 ? "\n" : " ");
return 0;
}
The saved value is four at coordinate (1,0).
Top-left becomes four, each boundary value advances one position, and the center remains five.
Follow the worked calculation
Start with the sample
The saved value is four at coordinate (1,0).
Apply the rule
Top-left becomes four, each boundary value advances one position, and the center remains five.
Check the result
4 1 2 7 5 3 8 9 6
4 1 2 7 5 3 8 9 6
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int table[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int positions[][2] = {{0,0},{0,1},{0,2},{1,2},{2,2},{2,1},{2,0},{1,0}};
int saved = table[1][0];
for (int i = 7; i > 0; i--) table[positions[i][0]][positions[i][1]] = table[positions[i-1][0]][positions[i-1][1]];
table[0][0] = saved;
for (int row = 0; row < 3; row++) for (int col = 0; col < 3; col++) printf("%d%s", table[row][col], col == 2 ? "\n" : " ");
return 0;
}
Apply two one-position rotations and compare with the first result.
Worked answer
The second rotation advances the same ring again. In this three-by-three example, two steps happen to correspond to a ninety-degree boundary move; that is not the definition of one step for every matrix size.
C17 · complete program
#include <stdio.h>
int main(void) {
int table[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int positions[][2] = {{0,0},{0,1},{0,2},{1,2},{2,2},{2,1},{2,0},{1,0}};
for (int step = 0; step < 2; step++) {
int saved = table[1][0];
for (int i = 7; i > 0; i--) table[positions[i][0]][positions[i][1]] = table[positions[i-1][0]][positions[i-1][1]];
table[0][0] = saved;
}
for (int row = 0; row < 3; row++) for (int col = 0; col < 3; col++) printf("%d%s", table[row][col], col == 2 ? "\n" : " ");
return 0;
}
Expected output
7 4 1
8 5 2
9 6 3
Run in C compiler →Recognize the important boundaries
Specify the direction, number of positions and treatment of inner layers. Different rotation problems can share a name while requiring different outputs.
Shifting forward in place without preserving unread source values would overwrite information.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
A perimeter rotation moves values along an explicitly ordered boundary ring; it is different from automatically rotating every table by ninety degrees.
Continue with one-dimensional rotation.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.