Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <math.h>
- #include <locale.h>
- #include "conio.h"
- #include "stdio.h"
- #include <cmath>
- #include <time.h>
- #include <stdlib.h>
- #include <malloc.h>
- void destroy(int **m, int n)
- {
- if (!m) return;
- for (int i = 0; i < n; i++)
- free((void*)m[i]);
- free((void*)m);
- }
- int **caloc(int n)
- {
- int **m = NULL, i(0);
- m = (int**)calloc(n, sizeof(int*));
- if (!m) return m;
- for (; i < n; i++)
- {
- m[i] = (int*)calloc(n, sizeof(int));
- if (!m) break;
- }
- if (i < n)
- {
- destroy(m, n);
- m = NULL;
- }
- return m;
- }
- int **create(int n, int r)
- {
- int **m = caloc(n);
- if (!m) return m;
- srand((unsigned)time(NULL));
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- m[i][j] = rand() % r;
- return m;
- }
- void print(int **m, int n)
- {
- for (int i = 0; i < n; i++)
- {
- printf("\n\t");
- for (int j = 0; j < n; j++)
- printf("%4d", m[i][j]);
- }
- }
- int calc(int **m, int n) {
- int f(0);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (i + j < n - 1)
- if (m[i][j] % 2 == 0)
- f++;
- return f;
- }
- void izm(int **m, int n) {
- int c = calc(m, n);
- if (c == 0) {
- printf("Нет четных элементов выше побочной диагонали");
- }
- else {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (i == j)
- m[i][j] = c;
- printf("Количество четных элементов : ");
- printf("%d", c);
- printf("\n");
- print(m, n);
- }
- }
- void main()
- {
- setlocale(LC_ALL, "rus");
- int n(0);
- printf("укажите размер матрицы: ");
- scanf_s("%d", &n);
- int **m = create(n, 100);
- if (!m)
- {
- printf("\n\tнет памяти");
- _getch();
- }
- print(m, n);
- printf("\n\n");
- izm(m, n);
- destroy(m, n);
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement