Advertisement
Qellex

5.2

Dec 24th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <locale.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <malloc.h>
  5. void PrintArray(int** a, int n) {
  6.     for (int i = 0; i < n; i++) {
  7.         for (int j = 0; j < n; j++)
  8.             printf("%3d", a[i][j]);
  9.         printf("\n");
  10.     }
  11. }
  12.  
  13.  
  14. void DDD(int** a, int n) {
  15.     int x = 1;
  16.     for (int s = 0; s < n * n; s++) {
  17.         if (s % 2 == 0) {
  18.             for (int i = 0; i < n; i++) {
  19.                 for (int j = 0; j < n; j++) {
  20.                     if ((i + j) == s) {
  21.                         a[j][i] = x;
  22.                         x++;
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.         if (s % 2 == 1) {
  28.             for (int j = 0; j < n; j++) {
  29.                 for (int i = 0; i < n; i++) {
  30.                     if ((i + j) == s) {
  31.                         a[j][i] = x;
  32.                         x++;
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
  39.  
  40. void main()
  41. {
  42.     setlocale(LC_ALL, "rus");
  43.     int n;
  44.  
  45.     printf("Введите размер n квадратной матрицы: ");
  46.     do {
  47.         scanf_s("%d", &n);
  48.         if (n <= 0)
  49.             printf("Повторите ввод, введите положительное число: ");
  50.     } while (n <= 0);
  51.  
  52.     int** a = (int**)malloc(sizeof(int*) * n);
  53.     for (int i = 0; i < n; i++) {
  54.         a[i] = (int*)malloc(sizeof(int) * n);
  55.     }
  56.  
  57.     DDD(a, n);
  58.  
  59.  
  60.     PrintArray(a, n);
  61.  
  62.     getchar(); getchar();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement