ibrahim_065

Write a program to find elements from an array that are larger than the average.

Oct 10th, 2020 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. /*
  2. 2. Write a program to find elements from an array that are larger than the average.
  3. */
  4. #include<stdio.h>
  5. int main()
  6. {
  7.     int n, k = 0, i;
  8.     float avg = 0;
  9.     // array size
  10.     scanf("%d", &n);
  11.     int ar[n], lrg_el[n];
  12.     // input array
  13.     for (i = 0; i < n; ++i)
  14.     {
  15.         scanf("%d", &ar[i]);
  16.         avg += ar[i];
  17.     }
  18.     avg /= (float)n;
  19.     // comparing array elements with average
  20.     for (i = 0; i < n; ++i)
  21.     {
  22.         if (ar[i] > avg)
  23.             lrg_el[k++] = ar[i];
  24.     }
  25.     // print array
  26.     for (i = 0; i < k; ++i)
  27.         printf("%d ", lrg_el[i]);
  28.     return 0;
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment