Advertisement
tampurus

3 Mean, Median & Mode Continuous Series

Oct 11th, 2023 (edited)
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. const int MAX_SIZE = 25;
  4.  
  5. void Mean(int ci[][2], int fre[], int n){
  6.     int mid[n];
  7.     for(int i=0 ; i<n ; i++){
  8.         mid[i] = (ci[i][0]+ci[i][1])/2;
  9.     }
  10.    
  11.     long long int sum =0 , N=0;
  12.     for(int i=0 ; i<n ; i++){
  13.         sum += (fre[i]*mid[i]);
  14.         N += fre[i];
  15.     }
  16.    
  17.     float mean = (float)sum / (float)N ;
  18.    
  19.     printf("\nMean for the given data is %.3f ",mean);
  20. }
  21.  
  22. void Median(int ci[MAX_SIZE][2], int fre[MAX_SIZE], int n ){
  23.     int cf[n];
  24.     int size = ci[0][1] - ci[0][0];
  25.     long int N =0;
  26.     for(int i=0 ; i<n ; i++){
  27.         N+=fre[i];
  28.         cf[i] = N;
  29.     }
  30.     float Nby2 = (float)N/2;
  31.     float L,F,CF;
  32.     for(int i=0 ; i<n ; i++){
  33.         if(Nby2<cf[i]){
  34.             L = ci[i][0];
  35.             F = fre[i];
  36.             CF = cf[i-1];
  37.             break;
  38.         }
  39.     }
  40.    
  41.     float K = (Nby2 - CF)/ F;
  42.     float median = L + (K*size);
  43.    
  44.     printf("\nMedian for this problem is %.3f",median);
  45.    
  46. }
  47. void Mode(int ci[MAX_SIZE][2], int fre[MAX_SIZE], int n){
  48.     float f1=fre[0];
  49.     int index =0;
  50.     for(int i=1 ; i<n ; i++){
  51.         if(f1<fre[i]){
  52.             f1 = fre[i];
  53.             index = i;
  54.         }
  55.     }
  56.    
  57.     float f0 = fre[index-1] , f2= fre[index+1];
  58.    
  59.     float k = (f1-f0)/( (2*f1) - f2 - f0);
  60.     float size = ci[0][1] - ci[0][0];
  61.     float mode = ci[index][0] + (k*size);
  62.    
  63.    
  64.     printf("\nMode of given data is %.3f \n",mode);
  65.    
  66. }
  67. int main()
  68. {
  69.     int n;
  70.     int ci[MAX_SIZE][2],fre[MAX_SIZE];
  71.     printf("Enter the number of elements \n");
  72.     scanf("%d",&n);
  73.    
  74.    
  75.     printf("\nEnter CI one by one \n");
  76.     for(int i=0 ; i<n ; i++){
  77.         for(int j=0 ; j<2 ; j++){
  78.             scanf("%d",&ci[i][j]);
  79.         }
  80.     }
  81.    
  82.     printf("\nNow enter the %d frequencies : \n",n);
  83.     for(int i=0 ; i<n ; i++){
  84.         scanf("%d",&fre[i]);
  85.     }
  86.    
  87.     // now calling function which will print the mean
  88.     Mean(ci, fre, n);
  89.    
  90.     // now callign function which will print the median of the given data
  91.     Median(ci, fre, n);
  92.    
  93.     // now calling function which will print the mode of given data
  94.     Mode(ci, fre, n);
  95.  
  96.     return 0;
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement