fork download
  1. #include <stdio.h>
  2.  
  3. void print(int *arr, int sz, char *title) {
  4. int i, j;
  5. printf("%s\n", title);
  6. for(i = 0; i < sz; ++i) {
  7. for(j = 0; j < sz; ++j) printf("%d\t", arr[i * sz + j]);
  8. printf("\n");
  9. }
  10. }
  11.  
  12. void rotate(int *arr, int sz) {
  13. int i;
  14. for(i = 0; i < sz / 2; ++i) {
  15. int j, temp;
  16. int index = i * sz + i;
  17. const int tpi = index + sz;
  18. temp = arr[index];
  19. for(j = 1; j < sz - 2 * i; ++j, ++index) arr[index] = arr[index + 1];
  20. for(j = 1; j < sz - 2 * i; ++j, index += sz) arr[index] = arr[index + sz];
  21. for(j = 1; j < sz - 2 * i; ++j, --index) arr[index] = arr[index - 1];
  22. for(j = 1; j < sz - 2 * i; ++j, index -= sz) arr[index] = arr[index - sz];
  23. arr[tpi] = temp;
  24. }
  25. }
  26.  
  27. int main(void) {
  28. int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  29. int arr2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  30. print(arr, 3, "Input");
  31. rotate(arr, 3);
  32. print(arr, 3, "Output");
  33. print(arr2, 4, "Input2");
  34. rotate(arr2, 4);
  35. print(arr2, 4, "Output2");
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
Input
1	2	3	
4	5	6	
7	8	9	
Output
2	3	6	
1	5	9	
4	7	8	
Input2
1	2	3	4	
5	6	7	8	
9	10	11	12	
13	14	15	16	
Output2
2	3	4	8	
1	7	11	12	
5	6	10	16	
9	13	14	15