How to Calculate Geometric Mean (GM) for Continuous series in C programming?

By | October 19, 2021

Calculate Geometric Mean (GM) for Raw Data in C programming:

// Here, ll = lowerLimit
// cl = classLimit
// nc = class length
// sf = sum of frequency
// f = frequency
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
            int ll,up,cl,nc,l[100],u[100],i,n,x[100],f[100],sf=0;
            float slogx=0.0;
            clrscr();
            printf("Enter the lower limit: ");
            scanf("%d",&ll);
            printf("Enter the class length: ");
            scanf("%d",&cl);
            printf("Enter the total number of class: ");
            scanf("%d",&nc);
            for(i=0;i<nc;i++)
            {
                        up=ll+cl;
                        l[i]=ll;
                        u[i]=up;
                        ll=up;
                        x[i]=((float)l[i]+(float)u[i])/2;
            }
            for(i=0;i<nc;i++)
            {
                        printf("Enter the frequency for class %d-%d: ",l[i],u[i]);
                        scanf("%d",&f[i]);
                        sf+=f[i];
            }
            printf(" Class Xi Fi FLogX ");
            //  for(i=0;i<25;i++);
            //    printf("-");
            for(i=0;i<nc;i++)
            {
                        printf("%d-%d %d %d %.4f ",l[i],u[i],x[i],f[i],(log10(x[i]))*f[i]);
                        slogx+=(log10(x[i]))*f[i];
            }
            for(i=0;i<25;i++)
            printf("-");
            printf(" Total %.4f ",slogx);
            printf("The Geometric Mean is: %.4f",pow(10,((slogx/sf))));
  getch();
}

Output:

Calculate Geometric Mean (GM) for Continuous series in C programming

Or

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
            int i,n;
			float x[100],f[100],sf=0,gm,al;
            float sflogx=0.0;
    
            printf("Enter the total number of observation: ");
            scanf("%d",&n);
            for(i=0;i<n;i++)
            {
                        printf("Enter the obs %d: ",i+1);
                        scanf("%f",&x[i]);
                        printf("Enter the freq: ");
                        scanf("%f",&f[i]);
                        sf=sf+f[i];
            }
            
            printf(" X LogX ");
            for(i=0;i<25;i++);
            
            printf("-");
            for(i=0;i<n;i++)
            {
                        printf(" %.4f ",f[i] * (log10(x[i])));
                        sflogx=sflogx+(log10(x[i])) * f[i];
                        
            }
            printf("sflogx=%f",sflogx);
            printf("sf=%f",sf);
            al=sflogx/sf;
            printf("al=%f",al);
            gm=pow(10,al);
            printf("geometric mean=%f",gm);
  getch();
}

One thought on “How to Calculate Geometric Mean (GM) for Continuous series in C programming?

  1. GET IT ON

    Thanks for the good writeup. It in fact used to
    be a enjoyment account it. Look complex to far delivered
    agreeable from you! However, how could we be in contact?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *