Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had a go. It's a bit naive as I didn't know the algorithm for it. It works for any matrix whose size is an odd number.</p> <p>[EDIT: now with comments]</p> <pre><code>#include &lt;iostream&gt; #include &lt;cstdlib&gt; #include &lt;cassert&gt; using namespace std; void printMatrix(int** mat, int s) { for (int i = 0; i &lt; s; ++i) { for (int j = 0; j &lt; s; ++j) { cout &lt;&lt; mat[i][j] &lt;&lt; " "; } cout &lt;&lt; "\n"; } } // For the matrix of size s located at mat1[src_x][src_y], shift the numbers on its // perimeter in a counter-clockwise direction, and store the result in mat2. void periShift(int** mat1, int src_x, int src_y, int s, int** mat2, int shift) { // Bottom edge for (int i = 0; i &lt; s; ++i) { int x = i; int y = 0; // Max x on this edge int X = s - 1; // The amount of space we have to shift x int a = X - x; // Clip the value to shift if (a &gt; shift) a = shift; // If a &lt; shift, the number is to be shifted round the corner (up the right side) by b places. int b = shift - a; mat2[src_x + x + a][src_y + y + b] = mat1[src_x + x][src_y + y]; } // Top edge for (int i = 0; i &lt; s; ++i) { int x = s - 1 - i; int y = s - 1; int a = x; if (a &gt; shift) a = shift; int b = shift - a; mat2[src_x + x - a][src_y + y - b] = mat1[src_x + x][src_y + y]; } // Right edge for (int i = 0; i &lt; s; ++i) { int y = i; int x = s - 1; int Y = s - 1; int a = Y - y; if (a &gt; shift) a = shift; int b = shift - a; mat2[src_x + x - b][src_y + y + a] = mat1[src_x + x][src_y + y]; } // Left edge for (int i = 0; i &lt; s; ++i) { int y = s - 1 - i; int x = 0; int a = y; if (a &gt; shift) a = shift; int b = shift - a; mat2[src_x + x + b][src_y + y - a] = mat1[src_x + x][src_y + y]; } } void rotateMatrix(int** mat1, int** mat2, int s) { // The size of the matrix's perimeter int peri = 4 * (s - 1); assert(peri % 8 == 0); // As 45 degrees is an eighth of a full circle, the numbers on the matrix's perimeter // should end up (peri / 8) places counter-clockwise from where they started. int shift = peri / 8; // Work our way out from the centre of the matrix to the edge, performing the perimeter shift // on each submatrix. I.e. the 3x3 matrix in the middle has its perimeter shifted 1 position // counter-clockwise, the 4x4 matrix has its position shifted 2 places, etc. for (int i = 0; i &lt; s / 2; ++i) { int x = i; int y = i; // Shift the perimeter of the submatrix periShift(mat1, x, y, s - 2 * i, mat2, shift - i); } // Copy the centre value over mat2[s / 2][s / 2] = mat1[s / 2][s / 2]; } int main() { int s = 0; while (1) { cout &lt;&lt; "Enter matrix size:\n"; cin &gt;&gt; s; if (s % 2 == 1) break; cout &lt;&lt; "Invalid matrix size. Must be an odd number.\n"; } // Construct 2 matrices int** mat1 = new int*[s]; for (int i = 0; i &lt; s; ++i) mat1[i] = new int[s]; int** mat2 = new int*[s]; for (int i = 0; i &lt; s; ++i) mat2[i] = new int[s]; // Fill first matrix with random numbers (0 .. 9) srand(time(NULL)); for (int i = 0; i &lt; s; ++i) { for (int j = 0; j &lt; s; ++j) { mat1[i][j] = rand() % 10; } } cout &lt;&lt; "Matrix =\n"; printMatrix(mat1, s); rotateMatrix(mat1, mat2, s); cout &lt;&lt; "\nRotated matrix =\n"; printMatrix(mat2, s); // Delete matrices for (int i = 0; i &lt; s; ++i) delete[] mat1[i]; delete mat1; for (int i = 0; i &lt; s; ++i) delete[] mat2[i]; delete mat2; return EXIT_SUCCESS; } </code></pre> <p>EDIT: In theory, the whole perimeter shift thing is unnecessary if you can figure out the mathematical function that maps the position of values in matrix A to their new position in matrix B.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload