Advertisement
InnaSibirova

M2

Oct 29th, 2022 (edited)
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main() {
  6.     srand(time(NULL));
  7.     double *a;
  8.     double n = 10, sr_plus = 0, sr_minus = 0, count_plus = 0, count_minus = 0;
  9.     a = (double*)malloc(n * sizeof(double)); // инициализируе массив а
  10.     for(int i = 0; i < n; i++) {
  11.         a[i] = (float)rand() / 100 - 100; // заполняем массив а и выводим его значения
  12.         printf("%g ", a[i]);
  13.     }
  14.     for(int i = 0; i < n; i++) { // считаем сумму и кол-во положительных и отрицательных чисел соответсвенно
  15.         if(a[i] > 0) {
  16.             sr_plus += a[i];
  17.             count_plus++;
  18.         }
  19.         else if(a[i] < 0) {
  20.             sr_minus += a[i];
  21.             count_minus++;
  22.         }
  23.     }
  24.     // считаем средннее арифметическое положительных и отрицательных чисел
  25.     // делением полученных сумм на кол-во соотвествующих чисел
  26.     sr_plus /= count_plus;
  27.     sr_minus /= count_minus;
  28.     printf("\n arithmetic mean of positive numbers = %g \n", sr_plus);// вывод значений
  29.     printf("\n arithmetic mean of negative numbers = %g \n", sr_minus);
  30.     free(a);
  31.     return 0;
  32.    
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement