Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. //левый верхний по часовой одномерный
  2. //#include "pch.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. int const strs = 7;
  7. int const clmns = 3;
  8.  
  9.  
  10. void init_m(int A[], int strs, int clmns)
  11. {
  12. for (int i = 0; i < strs; i++)
  13. {
  14. for (int j = 0; j < clmns; j++)
  15. {
  16. A[i*clmns + j] = rand() % 20;
  17. }
  18. }
  19. }
  20.  
  21. void print_m(int A[], int strs, int clmns)
  22. {
  23. for (int i = 0; i < strs; i++)
  24. {
  25. for (int j = 0; j < clmns; j++)
  26. {
  27. printf("%4d", A[i*clmns + j]);
  28. }
  29. printf("\n");
  30. }
  31. printf("\n");
  32. }
  33.  
  34. void left_down(int A[][clmns],int strs)
  35. {
  36. int i, j;
  37. int limit = strs * clmns;
  38. int left = 0;
  39. int right = clmns - 1;
  40. int up = 0;
  41. int down = strs - 1;
  42. int Strs = strs;
  43. int Clmns = clmns;
  44.  
  45. while ((limit > 0) && (left <= right) && (up <= down))
  46. {
  47. for (i = up; i < down; i++)
  48. {
  49. printf("%d ", A[i][left]);
  50. limit--;
  51. if (limit == 0)
  52. {
  53. break;
  54. }
  55. }
  56. for (j = left; j < right; j++)
  57. {
  58. printf("%d ", A[down][j]);
  59. limit--;
  60. if (limit == 0)
  61. {
  62. break;
  63. }
  64.  
  65. }
  66. for (i = down; i > up; i--)
  67. {
  68. printf("%d ", A[i][right]);
  69. limit--;
  70. if (limit == 0)
  71. {
  72. break;
  73. }
  74. }
  75. for (j = right; j > left; j--)
  76. {
  77. printf("%d ", A[up][j]);
  78. limit--;
  79. if (limit == 0)
  80. {
  81. break;
  82. }
  83. }
  84. left++;
  85. right--;
  86. up++;
  87. down--;
  88. }
  89. if(limit == 1)
  90. printf("%d ", A[Strs/2][Clmns/2]);
  91. }
  92. int main()
  93. {
  94. srand(12345);
  95.  
  96. int* A = (int*)malloc(clmns * strs * sizeof(int));
  97. init_m(A, strs, clmns);
  98. print_m(A, strs, clmns);
  99. left_down(A,strs);
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement