Program for average, geometric mean, harmonic mean

This is a discussion on Program for average, geometric mean, harmonic mean within the C Programming forums, part of the General Programming Boards category; I want to write a program that reads a series of numbers and calculates the average, geometric mean, and harmonic ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    34

    Program for average, geometric mean, harmonic mean

    I want to write a program that reads a series of numbers and calculates the average, geometric mean, and harmonic mean. I know the jist of what to do, but I don't know how to write the program.

    Here is what I have come up with for the program:
    The average is: sum/n
    The harmonic mean is: n/inverse sum (ex: 4/((1/5)+(1/3)+(1/2)+(1/4)))
    The geometric mean is: (X1 * X2 * X3 * Xn)^(1/n) - to write this in C I know it is pow (product, 1.0/n);

    If given a set of numbers such as:
    Number 1 = 5... then the sum is 5, product is 5, and inverse sum is 1/5
    Number 2 = 3... then the sum is 8, product 15, inv sum 1/5 + 1/3
    and so on...

    I have an idea of what to do but I'm not sure how to do it properly. I think it is something like below:
    Code:
    int num = 1;
    while (num!=0)
    {
    printf("Enter a number(0 to quit): ")
    scanf("%d", &num);
    if (num!= 0)
    { Enter numers
    }
    } (enter zero)
    do the execution of the equations
    If anyone could help me with the parts I am missing I would appreciate it.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You could store the numbers in a linked list, or an array (assuming you want to keep all the numbers).

    Such as,
    Code:
    #define MAX_EQUATIONS 32
    
    /* ... */
    
    int num = 0,
        i = 0,
        z = 0,
        product = 0,
        m = 0;
    int numbers[MAX_EQUATIONS] = {0};
    
    /* ... */
    
    for(m = 0; m < MAX_EQUATIONS; m++)
    {
        printf("Enter a number (0 to quit): ");
        scanf("&#37;d", &num);
        
        if(num != 0)
        {
            numbers[i] = num;
            ++i;
        }else{
            break;
        }
    }
    
    /* do equations with numbers from [0, i] */
    
    for(z = 0; z < i; z++)
    {
        product *= numbers[z];
        sum += numbers[z];
    }
    
    /* etc */
    of course that's purely a demonstration.
    Last edited by zacs7; 11-07-2007 at 12:05 AM.

  3. #3
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    here is untested code
    now you figure it out what should be the variable types
    Code:
       /*everytime you get a new number do this*/
        sum = sum + num;
        product = product*num;
        inverse_sum = inverse_sum + 1/((float)num);
        num_count = num_count + 1;
      /*num_count is the total number of numbers entered*/
    
       /* calculate the mean  as follows */
        printf("Average: &#37;f\n",sum/((float)num_count));
        printf("H mean : %f\n",num_count/inverse_sum);
        printf("G mean: %f\n", pow(product,1/(float)num_count);
    include math.h in your code & your compile command should have -lm..
    if your file is named as mean.c then the command will be like
    Code:
    $cc -o mean -lm mean.c
    THIS CODE HAS LIMITATIONS. I THINK YOU MIGHT BE KNOWING THEM. IF NOT KNOW THEM FIRST
    Last edited by gibsosmat; 11-07-2007 at 12:44 AM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't give away the solution, especially if this is homework - it only encourages people to beg for work to be done for them.

    > printf("G mean: &#37;f\n", powf((float)product,1/(float)num_count);
    Missing something here...? Also why would product need to be a double if you're going to cast it back to float anyway?

  5. #5
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    oops!!.. sorry..
    I changed it now..

    about the float: I didnt look into it much.. useless typechecking

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Thanks for the help. I have a better idea of what I need to do now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Can't figure out why this program won't work
    By Northstar in forum C Programming
    Replies: 6
    Last Post: 11-26-2007, 11:31 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21