Read the suggested @FredOverflow link: How to use arrays in C ++? .
To rotate the desired NxN array clockwise 90 Β°, you can divide the task into two small steps:
- flip the matrix up / down
- transpose him
void rot90cw(char A[][N]) { // flip in up/down direction (swap rows) for (int i = 0; i < N/2; i++) std::swap_ranges(&A[i][0], &A[i][0] + N, &A[Ni-1][0]); // transpose (swap top-right and bottom-left triangles) for (int i = 0; i < N-1; i++) for (int j = i+1; j < N; j++) std::swap(A[i][j], A[j][i]); }
I used swap() and swap_ranges() to perform operations in place.
Example
// -*- coding: utf-8 -*- #include <algorithm> #include <iostream> namespace { const int N = 3; // rotate 90Β° clock-wise void rot90cw(char A[][N]) { // ... defined above } void print(char a[][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) std::cout << a[i][j]; std::cout << '\n'; } std::cout << std::endl; } } int main() { char a[][N] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; print(a); rot90cw(a); std::cout << "Rotated 90Β° clock-wise:" << std::endl; //note: utf-8 print(a); }
Output
abc def ghi Rotated 90Β° clock-wise: gda heb ifc
source share