Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- /*
- 1 2 3
- 4 5 6
- 7 8 9
- ==>
- 9 6 3
- 8 5 2
- 7 4 1
- n = 3
- i j x y
- 0 0 <-> 2 2
- 0 1 <-> 1 2
- 1 0 <-> 2 1
- x = n - j -1
- y = n - i -1
- */
- void mirror_flattened(int *t, int n)
- {
- int i, j;
- for(i = 0; i < n - 1; ++i)
- {
- for(j = 0; j < n - i; ++j)
- {
- int x = n - j - 1, y = n - i - 1, tmp;
- tmp = t[i * n + j];
- t[i * n + j] = t[x * n + y];
- t[x * n + y] = tmp;
- }
- }
- }
- void mirror_jagged(int **t, int n)
- {
- int i, j;
- for(i = 0; i < n - 1; ++i)
- {
- for(j = 0; j < n - i; ++j)
- {
- int x = n - j - 1, y = n - i - 1, tmp;
- tmp = t[i][j];
- t[i][j] = t[x][y];
- t[x][y] = tmp;
- }
- }
- }
- int main()
- {
- int M[3][3] = { {1,2,3},
- {4,5,6},
- {7,8,9}};
- int **t, i, j, x;
- t = (int**) malloc(3*sizeof(int*));
- x = 1;
- for(i = 0; i < 3; ++i)
- {
- t[i] = (int*) malloc(3*sizeof(int));
- for(j = 0; j < 3; ++j)
- t[i][j] = x++;
- }
- mirror_flattened((int*)M,3);
- for(i = 0; i < 3; ++i)
- {
- for(j = 0; j < 3; ++j)
- printf("%d ", M[i][j]);
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement