Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- const int MAX_SIZE = 25;
- void Mean(int ci[][2], int fre[], int n){
- int mid[n];
- for(int i=0 ; i<n ; i++){
- mid[i] = (ci[i][0]+ci[i][1])/2;
- }
- long long int sum =0 , N=0;
- for(int i=0 ; i<n ; i++){
- sum += (fre[i]*mid[i]);
- N += fre[i];
- }
- float mean = (float)sum / (float)N ;
- printf("\nMean for the given data is %.3f ",mean);
- }
- void Median(int ci[MAX_SIZE][2], int fre[MAX_SIZE], int n ){
- int cf[n];
- int size = ci[0][1] - ci[0][0];
- long int N =0;
- for(int i=0 ; i<n ; i++){
- N+=fre[i];
- cf[i] = N;
- }
- float Nby2 = (float)N/2;
- float L,F,CF;
- for(int i=0 ; i<n ; i++){
- if(Nby2<cf[i]){
- L = ci[i][0];
- F = fre[i];
- CF = cf[i-1];
- break;
- }
- }
- float K = (Nby2 - CF)/ F;
- float median = L + (K*size);
- printf("\nMedian for this problem is %.3f",median);
- }
- void Mode(int ci[MAX_SIZE][2], int fre[MAX_SIZE], int n){
- float f1=fre[0];
- int index =0;
- for(int i=1 ; i<n ; i++){
- if(f1<fre[i]){
- f1 = fre[i];
- index = i;
- }
- }
- float f0 = fre[index-1] , f2= fre[index+1];
- float k = (f1-f0)/( (2*f1) - f2 - f0);
- float size = ci[0][1] - ci[0][0];
- float mode = ci[index][0] + (k*size);
- printf("\nMode of given data is %.3f \n",mode);
- }
- int main()
- {
- int n;
- int ci[MAX_SIZE][2],fre[MAX_SIZE];
- printf("Enter the number of elements \n");
- scanf("%d",&n);
- printf("\nEnter CI one by one \n");
- for(int i=0 ; i<n ; i++){
- for(int j=0 ; j<2 ; j++){
- scanf("%d",&ci[i][j]);
- }
- }
- printf("\nNow enter the %d frequencies : \n",n);
- for(int i=0 ; i<n ; i++){
- scanf("%d",&fre[i]);
- }
- // now calling function which will print the mean
- Mean(ci, fre, n);
- // now callign function which will print the median of the given data
- Median(ci, fre, n);
- // now calling function which will print the mode of given data
- Mode(ci, fre, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement