fork(1) download
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6. int arraysize;
  7. cout<<"Size: ";
  8. cin>>arraysize;
  9. int a[arraysize][arraysize];
  10. int couter = 1, stepTop = 0, stepLeft = 1, stepBottom = 0, stepRight = 1;
  11. for(int iMain = 0; iMain < ((arraysize / 2) + 1); iMain++)
  12. {
  13. for(int i = stepTop; i < stepTop + 1; i++)
  14. {
  15. for(int j = stepTop; j < (arraysize - stepTop); j++)
  16. {
  17.  
  18. a[i][j] = couter;
  19. couter++;
  20. }
  21.  
  22. }
  23. stepTop++;
  24.  
  25. if(iMain < (arraysize / 2))
  26. {
  27. for(int i = stepRight; i <= (arraysize - stepRight - 1); i++)
  28. {
  29. for(int j = (arraysize - stepRight); j < (arraysize - stepRight + 1); j++)
  30. {
  31. a[i][j] = couter;
  32. couter++;
  33. }
  34. }
  35. stepRight++;
  36.  
  37. for(int i = (arraysize - 1 - stepBottom); i >= (arraysize - stepBottom - 1); i--)
  38. {
  39. for(int j = (arraysize - 1 - stepBottom); j >= stepBottom; j--)
  40. {
  41. a[i][j] = couter;
  42. couter++;
  43. }
  44. }
  45. stepBottom++;
  46.  
  47. for(int i = (arraysize - stepLeft - 1); i >= stepLeft; i--)
  48. {
  49. for(int j = stepLeft-1; j < stepLeft; j++)
  50. {
  51. a[i][j] = couter;
  52. couter++;
  53. }
  54. }
  55. stepLeft++;
  56. }
  57. }
  58.  
  59.  
  60. for(int i = 0; i < arraysize; i++)
  61. {
  62. for(int j = 0; j < arraysize; j++)
  63. {
  64. cout<<setw(arraysize)<<a[i][j];
  65. cout<<" ";
  66. }
  67. cout<<endl;
  68. }
  69.  
  70. return 0;
  71. }
Success #stdin #stdout 0s 3144KB
stdin
6
stdout
Size:      1      2      3      4      5      6 
    20     21     22     23     24      7 
    19     32     33     34     25      8 
    18     31     36     35     26      9 
    17     30     29     28     27     10 
    16     15     14     13     12     11