Advertisement
Scratius

StatLevVerhPoChas

Dec 5th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include "stdio.h"
  3. #include <time.h>
  4. #define M 5
  5. #define N 5
  6.  
  7. void init_matr(int a[][M], int n) {
  8.     unsigned i, j;
  9.     for (i = 0; i < n; i++) {
  10.         for (j = 0; j < M; j++) {
  11.             a[i][j] = rand() % 100;
  12.         }
  13.     }
  14. }
  15. void print_matr(int a[][M], int n) {
  16.     unsigned i, j;
  17.     for (i = 0; i < n; i++) {
  18.         for (j = 0; j < M; j++) {
  19.             printf("%4d", a[i][j]);
  20.         }
  21.         printf("\n");
  22.     }
  23. }
  24. void the_power_of_spiral(int a[][M], int n) {
  25.     unsigned i, j;
  26.     int m = M;
  27.     int qu = M * n;
  28.     unsigned left = 0;
  29.     unsigned right = M - 1;
  30.     unsigned upper = 0;
  31.     unsigned bottom = n - 1;
  32.     while ((left <= right) && (upper <= bottom) && (qu > 0)) {
  33.  
  34.         for (j = left; j < right; j++) {
  35.             printf("%d ", a[upper][j]);
  36.             qu--;
  37.             if (qu == 0) { break; }
  38.         }
  39.         for (i = upper; i < bottom; i++) {
  40.             printf("%d ", a[i][right]);
  41.             qu--;
  42.             if (qu == 0) { break; }
  43.         }
  44.        
  45.         for (j = right; j > left; j--) {
  46.             printf("%d ", a[bottom][j]);
  47.             qu--;
  48.             if (qu == 0) { break; }
  49.         }
  50.            
  51.         for (i = bottom; i > upper; i--) {
  52.             printf("%d ", a[i][left]);
  53.             qu--;
  54.             if (qu == 0) { break; }
  55.         }
  56.            
  57.         left++;
  58.         right--;
  59.         upper++;
  60.         bottom--;
  61.     }
  62.     if(qu == 1){printf("%d ", a[n/2][m/2]);}
  63. }
  64. int main() {
  65.     int a[N][M] = { 0 };
  66.     init_matr(a, N);
  67.     print_matr(a, N);
  68.     printf("\n");
  69.     the_power_of_spiral(a, N);
  70.     getchar();
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement