Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //указатели
- //52) Выяснить, каких элементов в массиве A больше : отрицательных, положительных или нулей.
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- int** create_array(int w, int h)
- {
- int **a = new int*[w];
- for (unsigned int i = 0; i < w; i++)
- {
- a[i] = new int[h];
- }
- return a;
- }
- void delete_array(int **a, int w)
- {
- for (int j = 0; j < w; j++)
- delete[]a[j];
- }
- void print_array(int **a, int w, int h)
- {
- for (unsigned int k = 0; k < w; k++)
- {
- printf("\n");
- for (unsigned int j = 0; j < h; j++)
- {
- printf("%d\t", a[k][j]);
- }
- }
- }
- void fill_array(int **a, int w, int h)
- {
- for (unsigned int k = 0; k < w; k++)
- for (unsigned int j = 0; j < h; j++)
- {
- a[k][j] = (rand() % 40) - 21;
- }
- }
- /*
- Находим каких знаков в массиве больше:
- 1) Если больше положительных возвращает 1
- 2) Если больше отрицательных возвращает -1
- 3) Иначе ноль
- */
- int check_signs(int **a, int n, int m)
- {
- int pos = 0, nul = 0, neg = 0;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- {
- if (a[i][j] > 0)
- pos++;
- else if (!(a[i][j]))
- nul++;
- else neg++;
- }
- if (pos >= neg && pos > nul)
- return 1;
- else if (neg > pos && neg >= nul)
- return -1;
- else return 0;
- }
- int main()
- {
- int n,
- m;
- printf("Input arrat height and width:\n");
- scanf("%d \n%d", &n, &m);
- int **a = create_array(n, m);
- fill_array(a, n, m);
- print_array(a, n, m);
- if(check_signs(a, n, m) == 1)
- printf("\nThere is more positive numbers!\n");
- if (!(check_signs(a, n, m)))
- printf("\nThere is more nulls!\n");
- if (check_signs(a, n, m) == -1)
- printf("\nThere is more negative numbers!\n");
- delete_array(a, n);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement