Advertisement
meta1211

Untitled

Dec 20th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. //указатели
  2. //52) Выяснить, каких элементов в массиве A больше : отрицательных, положительных или нулей.
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9.  
  10. int** create_array(int w, int h)
  11. {
  12. int **a = new int*[w];
  13. for (unsigned int i = 0; i < w; i++)
  14. {
  15. a[i] = new int[h];
  16. }
  17. return a;
  18. }
  19.  
  20. void delete_array(int **a, int w)
  21. {
  22. for (int j = 0; j < w; j++)
  23. delete[]a[j];
  24. }
  25.  
  26. void print_array(int **a, int w, int h)
  27. {
  28. for (unsigned int k = 0; k < w; k++)
  29. {
  30. printf("\n");
  31. for (unsigned int j = 0; j < h; j++)
  32. {
  33. printf("%d\t", a[k][j]);
  34. }
  35. }
  36. }
  37.  
  38. void fill_array(int **a, int w, int h)
  39. {
  40. for (unsigned int k = 0; k < w; k++)
  41. for (unsigned int j = 0; j < h; j++)
  42. {
  43. a[k][j] = (rand() % 40) - 21;
  44. }
  45. }
  46. /*
  47. Находим каких знаков в массиве больше:
  48. 1) Если больше положительных возвращает 1
  49. 2) Если больше отрицательных возвращает -1
  50. 3) Иначе ноль
  51. */
  52. int check_signs(int **a, int n, int m)
  53. {
  54. int pos = 0, nul = 0, neg = 0;
  55. for (int i = 0; i < n; i++)
  56. for (int j = 0; j < m; j++)
  57. {
  58. if (a[i][j] > 0)
  59. pos++;
  60. else if (!(a[i][j]))
  61. nul++;
  62. else neg++;
  63. }
  64. if (pos >= neg && pos > nul)
  65. return 1;
  66. else if (neg > pos && neg >= nul)
  67. return -1;
  68. else return 0;
  69. }
  70.  
  71. int main()
  72. {
  73. int n,
  74. m;
  75.  
  76. printf("Input arrat height and width:\n");
  77. scanf("%d \n%d", &n, &m);
  78. int **a = create_array(n, m);
  79. fill_array(a, n, m);
  80. print_array(a, n, m);
  81.  
  82. if(check_signs(a, n, m) == 1)
  83. printf("\nThere is more positive numbers!\n");
  84. if (!(check_signs(a, n, m)))
  85. printf("\nThere is more nulls!\n");
  86. if (check_signs(a, n, m) == -1)
  87. printf("\nThere is more negative numbers!\n");
  88.  
  89. delete_array(a, n);
  90. system("pause");
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement