fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void print_layer_top_right(int a[][4], int col, int row, int collen, int rowlen);
  4. void print_layer_bottom_left(int a[][4], int col, int row, int collen, int rowlen);
  5.  
  6. int main(void)
  7. {
  8. int a[5][4] = {
  9. {1,2,3,4},
  10. {5,6,7,8},
  11. {9,10,11,12},
  12. {13,14,15,16},
  13. {17,18,19,20}
  14. };
  15.  
  16. print_layer_top_right(a,0,0,3,4);
  17. }
  18.  
  19. //
  20. // prints the top and right shells of the matrix
  21. //
  22. void print_layer_top_right(int a[][4], int col, int row, int collen, int rowlen)
  23. {
  24. int i = 0, j = 0;
  25.  
  26. // print the row
  27. for(i = col; i<=collen; i++)
  28. {
  29. printf("%d,", a[row][i]);
  30. }
  31.  
  32. //print the column
  33. for(j = row + 1; j <= rowlen; j++)
  34. {
  35. printf("%d,", a[j][collen]);
  36. }
  37.  
  38. // see if we have more cells left
  39. if(collen-col > 0)
  40. {
  41. // 'recursively' call the function to print the bottom-left layer
  42. print_layer_bottom_left(a, col, row + 1, collen-1, rowlen);
  43. }
  44. }
  45.  
  46. //
  47. // prints the bottom and left shells of the matrix
  48.  
  49. void print_layer_bottom_left(int a[][4], int col, int row, int collen, int rowlen)
  50. {
  51. int i = 0, j = 0;
  52.  
  53. //print the row of the matrix in reverse
  54. for(i = collen; i>=col; i--)
  55. {
  56. printf("%d,", a[rowlen][i]);
  57. }
  58.  
  59. // print the last column of the matrix in reverse
  60. for(j = rowlen - 1; j >= row; j--)
  61. {
  62. printf("%d,", a[j][col]);
  63. }
  64.  
  65. if(collen-col > 0)
  66. {
  67. // 'recursively' call the function to print the top-right layer
  68. print_layer_top_right(a, col+1, row, collen, rowlen-1);
  69. }
  70. }
  71.  
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
1,2,3,4,8,12,16,20,19,18,17,13,9,5,6,7,11,15,14,10,