Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. int main(int argc, char const *argv[]) {
  6.   int n=0,A = -100,B = -A;
  7.   int p_count,n_count;
  8.   int p_sum,n_sum;
  9.   float p_avr,p_avr
  10.  
  11.   srand(time (NULL));
  12.   /* giving the seed from our system
  13.    time to the rangom generator */
  14.  
  15.   printf("Input the size of array:\n>>");
  16.   scanf("%d",&n);
  17.   int arr[n];
  18.   /* dont forget to scan array size
  19.   before initializing the actual array */
  20.  
  21.   printf("\tarray arr[n] with n of %d:\t\n",n);
  22.   for(int i=0; i<n; i++){
  23. // filling array with random numbers
  24. // the range of numbers from generator is [A;B]
  25.     arr[i] = A +rand()%(B-A+1);
  26.     printf("arr[%d]=%d\n",i,arr[i]);
  27.   }
  28.  
  29. // going array to find positive or negative numbers
  30.   for(int i=0; i<n; i++){
  31.     if(arr[i]>=0){
  32.       p_count++;
  33.       p_sum+=arr[i];
  34.     }
  35.     else{
  36.       n_count++;
  37.       n_sum+=arr[i];
  38.     }
  39.   }
  40.  
  41. // calculating the average by definition
  42.   p_avr = p_sum/p_count;
  43.   n_avr = n_sum/n_count;
  44.  
  45.   printf("The average number is:\n for positive - %f\n for negative - %f\n",p_arv,n_avr);
  46.  
  47.   return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement