Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. nt rows; // строка
  2. int cols; // столбец
  3.  
  4. cout << "Enter rows count : ";
  5. cin >> rows;
  6. cout << "Enter cols count : ";
  7. cin >> cols;
  8.  
  9. int** arr = new int* [rows];
  10.  
  11.  
  12. for (int i = 0; i < rows; i++) //выделение памяти для матрицы
  13. {
  14. arr[i] = new int[cols];
  15.  
  16. }
  17.  
  18. // заполнение
  19. int a = 1;
  20. /*while (j<rows*cols)
  21. {
  22. for (int j = 0; j < rows-1; j++)
  23. {
  24. arr[0][j] = a;
  25. a++;
  26. }
  27.  
  28. a = a - 1;
  29.  
  30. for (int i = 0; i < cols-1; i++)
  31. {
  32. arr[i][rows - 1] = a;
  33. a++;
  34. }
  35.  
  36. a = a - 1;
  37.  
  38. for (int j = rows - 1; j >= 0; j--)
  39. {
  40. arr[cols - 1][j] = a;
  41. a++;
  42. }
  43.  
  44. a = a - 1;
  45.  
  46. for (int i = cols - 1; i > 0; i--)
  47. {
  48. arr[i][rows - 1] = a++;
  49. }
  50. }*/
  51. for (int j = 0; j < rows - 1; j++) // заполняем первую строку
  52. {
  53. arr[0][j] = a;
  54. a++;
  55. }
  56.  
  57.  
  58.  
  59. for (int i = 0; i < cols - 1; i++) // заполняем последний столбец
  60. {
  61. arr[i][rows - 1] = a;
  62. a++;
  63. }
  64.  
  65.  
  66.  
  67. for (int j = rows - 1; j >= 0; j--) // заполняем последнюю строку
  68. {
  69. arr[cols - 1][j] = a;
  70. a++;
  71. }
  72.  
  73.  
  74.  
  75. for (int i = cols - 1; i > 0; i--)
  76. {
  77. arr[i][rows - 1] = a++;
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. for (int i = 0; i < rows; i++) // вывод матрицы
  91. {
  92. for (int j = 0; j < cols; j++)
  93. {
  94. cout << arr[i][j] << '\t';
  95. }
  96. cout << endl;
  97. }
  98.  
  99.  
  100.  
  101. ////////////////////////////// освобождение памяти
  102. for (int i = 0; i < rows; i++)
  103. {
  104. delete[] arr[i];
  105. }
  106. delete []arr;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement