Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include<time.h>
  3. const int M = 4;
  4. const int cols = 4;// stolbu
  5. void init(int* B, int n);
  6. void print(int* B, int n);
  7. void lab(int* B, int n);
  8.  
  9. int main()
  10. {
  11. const int N = 4;
  12. srand(time(NULL));
  13. int* B = (int*)malloc(sizeof(int*) * N * M);
  14. init(B, N);
  15. print(B, N);
  16. lab(B, N);
  17. free(B);
  18. return 0;
  19. }
  20. void init(int* B, int n){
  21. for (int i = 0; i < n; i++) {
  22. for (int j = 0; j < M; j++) {
  23. B[i * M + j] = rand() % 100;
  24. }
  25. }
  26. }
  27. void print(int* B, int n) {
  28. for (int i = 0; i < n; i++) {
  29. for (int j = 0; j < M; j++) {
  30. printf("%3d", B[i * M + j]);
  31. }
  32. printf("\n");
  33. }
  34. printf("\n");
  35. }
  36.  
  37. void lab(int* B, int rows) {
  38. int krug = 0; int sum = 0;
  39. int goright, goleft, goup, godown;
  40. int flag = 0;
  41.  
  42. while(sum!=cols*rows) {
  43. for (goleft = cols - 1 - krug; goleft > krug; goleft--) {
  44. printf("%3d", B[cols * (rows - 1 - krug) + goleft]);
  45. sum++;
  46. }
  47. if (sum >= cols * rows) break;
  48. for (goup = rows - 1 - krug; goup > krug; goup--) {
  49. printf("%3d", B[cols * goup+krug]);
  50. sum++;
  51. }
  52. if (sum >= cols * rows) break;
  53. for (goright = krug ; goright < cols - krug - 1; goright++) {
  54. printf("%3d", B[cols * krug + goright]);
  55. sum++;
  56. }
  57. if (sum >= cols * rows) break;
  58. for (godown = krug; godown < rows - 1 - krug; godown++) {
  59. printf("%3d", B[cols-1 +(cols)*godown-krug]);
  60. sum++;
  61. }
  62. if (sum >= cols * rows) break;
  63. krug++;
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement