How to calculate weighted average

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HEMH6
    New Member
    • Sep 2006
    • 2

    How to calculate weighted average

    Weihted Average

    Write a C program to calculate and print the weighted average of a list of N
    floating point number, using the formula

    Xave = F1X1 + F2X2+...+ FnXn

    where the F's are fractional weighting factors,

    i.e. 0<=F1<1, and F1+F2+...+Fn=1

    For example: F1=0.1 F2=0.2 F3=0.3 F4=0.4 for n=4 and the sum=1.0

    (a) Assume that N=4, and that the N numbers are already available in an array.
    Use as input the following array: float X[4]={1.2,2.4,3.6,4 .8};

    (b) Prompt the user to enter the four weights from the keyboard. the program
    should print an error messaage if the weights are out of range.

    (c) For the result, the program should print the data, weights and the weighted
    average.
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    Code:
    for(i = 0, i < 4, i++)
    {
      do
      {
        // get floating point number input
      }  while // input is not within weight range
      F[i] = input;
      sum += input;
    }
    if(sum != 1.0)
      // throw a fit
    else
    {
      for(i = 0; i < 4, i++)
        xAve += (F[i]*X[i]);
      xAve /= 4;
      // output xAve
    }

    Comment

    Working...