Advertisement
Miha_Ch

C_lec03

Mar 4th, 2025 (edited)
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define BOOL int
  6. #define FALSE 0
  7. #define TRUE !FALSE
  8.  
  9. void inputArray(int data[], unsigned len)
  10. {
  11. for (int i = 0; i < len; i++) {
  12. printf("A(%d) = ", i+1);
  13. scanf("%d", data + i); //data+i = &data[i]
  14. }
  15. }
  16.  
  17. void inputRandomArray(int data[], unsigned len, BOOL isCustomSeed)
  18. {
  19. const int MIN = -10, MAX = 10;
  20.  
  21. if (isCustomSeed)
  22. srand(time(NULL));
  23.  
  24. for (int i = 0; i < len; i++) {
  25. data[i] = rand() %(MAX-MIN+1) + MIN;
  26. }
  27. }
  28.  
  29. void outputArray(int *data, unsigned len)
  30. {
  31. for (int i = 0; i < len; i++) {
  32. printf("%d\t", data[i]); //data[i] = *(data + i)
  33. }
  34. printf("\n");
  35. }
  36.  
  37. long sumarray(int data[], unsigned len)
  38. {
  39. long sum = 0;
  40.  
  41. for (int i = 0; i < len; i++)
  42. {
  43. sum += data[i];
  44. }
  45. return sum;
  46. }
  47. double averateNegatives(int data[], unsigned len) {
  48. long sum = 0;
  49. unsigned count = 0;
  50.  
  51. for (int i = 0; i < len; i++) {
  52. if (data[i] < 0) {
  53. count++;
  54. sum += data[i];
  55. }
  56. }
  57. return (count > 0) ? (double)sum/count : 0;
  58. }
  59.  
  60. int minEvenElement(int data[], unsigned len)
  61. {
  62. int index = -1;
  63.  
  64. for (int i = 0; i < len; i++) {
  65. if (data[i] % 2 == 0) {
  66. if (index == -1) {
  67. index = i;
  68. }
  69. else if (data[index] > data[i]) {
  70. index = i;
  71. }
  72. }
  73. }
  74. return index;
  75. }
  76.  
  77. int maxArray(int data[], unsigned len) {
  78. int max =data[0];
  79.  
  80. for (int i = 0; i < len; i++) {
  81. if (data[i] > max) {
  82. max = data[i];
  83. }
  84. }
  85. return max;
  86. }
  87.  
  88. unsigned countElement(int data[], unsigned len, int element) {
  89. unsigned count = 0;
  90.  
  91. for (int i = 0; i < len; i++) {
  92. if (data[i] == element) {
  93. count++;
  94. }
  95. }
  96. return count; // Трябва да се върне след приключване на цикъла
  97. }
  98.  
  99. int main(void)
  100. {
  101. const unsigned SIZE = 10;
  102. int array[SIZE];
  103. double result;
  104. int index, max;
  105.  
  106. //inputArray(array, 3);
  107. //inputRandomArray(array, SIZE, TRUE);
  108. inputRandomArray(array, SIZE, FALSE);
  109. outputArray(array, SIZE);
  110. printf("The sum is %d\n", sumarray(array, SIZE));
  111. result = averateNegatives(array, SIZE);
  112. if (result == 0) {
  113. printf("No such data...\n");
  114. }
  115. else {
  116. printf("The average of Negatives: %g\n", result);
  117. }
  118. index = minEvenElement(array, SIZE);
  119. if (index==-1) {
  120. printf("No such data...\n");
  121. }
  122. else {
  123. printf("Min Even Element: %d\n", array[index]);
  124. }
  125. printf("Average of Negatives: %f\n", averateNegatives(array, SIZE));
  126. printf("Minimum Even Element: %d\n", minEvenElement(array, SIZE));
  127.  
  128. max = maxArray(array, SIZE);
  129. printf("Max Element is %d.It\'s repeated %u times.\n", max, countElement(array, SIZE, max));
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement